Weather Service is a Ruby on Rails application that provides weather forecasts based on a given address. It utilizes the Open-Meteo API to fetch weather data and caches the results to improve performance.
- Address-based weather forecasting
- Caching of weather data to reduce API calls
- Error handling for unavailable zip codes or forecast data
- Uses Redis for caching to address immediate scaling
- Ruby on Rails 7
- HTTParty gem for making HTTP requests
- Geocoder gem for extracting zip codes from addresses
- Ensure you have Ruby on Rails 7 and Bundler installed.
- Clone the repository to your local machine.
- Navigate to the project directory and run
bundle installto install the required gems.
- Start the Rails server using
rails s. - Access the application through a web browser at
http://localhost:3000. - Use the endpoint
/weather_forecaststo get the weather forecast for the specified address.
app/services/weather_service.rb: Contains theWeatherServiceclass that handles the logic for fetching and caching weather data.app/controllers/weather_forecasts_controller.rb: Manages the web requests and interacts withWeatherServiceto get the weather data.app/views/weather_forecasts/index.html.erb: The view template that displays the weather forecast.
- The
WeatherForecastsControllerreceives a request with an address. - It initializes a
WeatherServiceinstance with the provided address. WeatherServiceextracts the zip code from the address using Geocoder.- It checks if the forecast for the zip code is cached.
- If cached, it returns the cached data.
- If not, it fetches the forecast from the Open-Meteo API, caches it, and then returns it.
- The controller then renders the view with the fetched forecast data.
- Weather data is cached based on the zip code to avoid unnecessary API calls.
- Cached data expires in 30 minutes, ensuring that users receive up-to-date forecasts.
- If the zip code cannot be extracted from the address, an error message is displayed.
- Any errors during the fetching of weather data are logged and an error message is shown to the user.
HTTParty: Used for making HTTP requests to the Open-Meteo API.Geocoder: Used for extracting the zip code from the provided address.
Contributions to improve Weather Service are welcome. Please follow the standard GitHub pull request process to submit your changes.
- Responsibilities:
- Fetches and caches weather data based on address or zip code.
- Extracts zip code from the given address.
- Fetches weather data from Open-Meteo API.
- Caches weather data to optimize performance.
- Key Methods:
initialize(address): Sets up the service with the specified address.fetch_forecast: Main method to retrieve the forecast, handling caching logic.extract_zip_code(address): Extracts the zip code from the address.fetch_and_cache_forecast(address, zip_code): Retrieves weather data from the API and caches it.fetch_weather_data(coordinates): Fetches weather data from the API using geographical coordinates.parse_forecast_response(response): Parses the API response into a structured format.
- Responsibilities:
- Interacts with the
WeatherServiceto get weather data. - Handles HTTP requests and responses.
- Manages error handling and logging.
- Interacts with the
- Key Methods:
index: The main action that responds to web requests, orchestrating the process of fetching the forecast and handling errors.
- Responsibilities:
- Displays the weather forecast to the user.
- Presents error messages when applicable.
- The
WeatherForecastsControllerreceives a user request, then usesWeatherServiceto fetch the corresponding weather data. WeatherServiceperforms the heavy lifting of geocoding the address, fetching the weather data from the Open-Meteo API, caching the results, and returning the formatted forecast data back to the controller.