Skip to content

Commit

Permalink
Merge pull request #8 from marcoelho97/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcoelho97 authored Nov 19, 2024
2 parents 83889d7 + 7735470 commit cc08483
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
81 changes: 66 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,78 @@
# README
Weather Info App
================

This README would normally document whatever steps are necessary to get the
application up and running.
This application retrieves historical weather data for a given location and date range using the Open-Meteo API. If the data is already stored in the database, it will fetch it from there; otherwise, it makes an HTTP request to the Open-Meteo API.

Things you may want to cover:
Features
--------

* Installed gems:
- httparty - Simpler http requests and automatic json parse
* API to fetch historical weather data based on location and date range.
* Uses the Open-Meteo API to retrieve weather information.
* Stores retrieved weather data in the database for faster future access.

* Ruby version
Prerequisites
-------------

* System dependencies
Make sure you have the following installed on your system:

* Configuration
1. **Ruby**: Version 3.x or higher.
2. **Rails**: Version 7.x or higher.
3. **SQLite3** (or another database if configured differently in the project).
4. **Git**: To clone the repository.


* Database creation
Setup Instructions
------------------

* Database initialization
### 1\. Clone the Repository

* How to run the test suite
```bash
git clone <This repository url>
cd backend-weather-app-openmeteo
```

* Services (job queues, cache servers, search engines, etc.)
### 2\. Install Dependencies

* Deployment instructions
```bash
bundle install
```

* ...
### 3\. Set Up the Database

```bash
rails db:create
rails db:migrate
```

### 4\. Start the Server
Preferably in a different port, as the frontend will be in port 3000.
The frontend is ready to receive from port 10524, so:

```bash
rails server -p 10524
```

API Endpoints
------------------
### GET /weather/historical?location=&start_date=&end_date=
Fetch historical weather data for a specific location and date range.

Response:
```json
[
{
"id": Numeric,
"location": String,
"date": Date,
"temperature": Float?,
"precipitation": Float?,
"created_at": Datetime,
"updated_at": Datetime
}
]
```

Notes
------------------
Due to time constraints, additional enhancements (e.g., more robust error handling, more features) didn't come to fruition:
- A route to return a list of locations with the name of the user's input location - To be able to choose the correct one in the frontend
16 changes: 14 additions & 2 deletions app/controllers/weather_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def historical

parsed_start_date = Date.parse(start_date).beginning_of_day
parsed_end_date = Date.parse(end_date).end_of_day

# Switch start and end date if end date comes before start date
if parsed_start_date > parsed_end_date
parsed_start_date, parsed_end_date = parsed_end_date, parsed_start_date
start_date, end_date = end_date, start_date
end

# Date range for the multiple records of dates
weather_data = HistoricalWeather.where(location: location, date: parsed_start_date..parsed_end_date).order(:date)
Expand Down Expand Up @@ -49,9 +55,15 @@ def historical
end

weather_response["daily"]["time"].each_with_index do |day, index|
HistoricalWeather.find_or_create_by(date: day) do |hw|

isTemperatureNumeric = weather_response["daily"]["temperature_2m_max"][index].is_a?(Numeric) && weather_response["daily"]["temperature_2m_min"][index].is_a?(Numeric)

HistoricalWeather.find_or_create_by(location: location, date: day) do |hw|
hw.location = location
hw.temperature = (weather_response["daily"]["temperature_2m_max"][index] + weather_response["daily"]["temperature_2m_min"][index]) / 2 # Average temperature
hw.temperature = isTemperatureNumeric ?
(weather_response["daily"]["temperature_2m_max"][index] + weather_response["daily"]["temperature_2m_min"][index]) / 2 # Average temperature
:
nil
hw.precipitation = weather_response["daily"]["precipitation_probability_max"][index]
end
end
Expand Down

0 comments on commit cc08483

Please sign in to comment.