This is a Ruby on Rails application that allows users to input addresses and then displays the current weather and weather forcast for those addresses. Addresses are created and stored in the application database. When a user chooses an address from the list of available addresses, the address details and the weather/forcast for that address are displayed. Addresses can be deleted from the system.
The purpose of this application is to show a working Ruby on Rails application that stores address data, retrieves additional weather data from an outside API, and displays the combined data to the user using standard server side rendered HTML. The application also includes code that allows a client to perform all the same operations using a REST API and receiving output in JSON format, so an outside client can render its own inteface (such as a React application). Tests are included at all levels to ensure the application is working correctly. The data coming back from the weather api is cached for 30 minutes.
This pull request shows all the added application code.
The following steps will install and application:
- Clone the application repository
$ git clone git@github.com:marktheblair/weatherapp.git- Install the all the gems needed to run the application
$ cd weatherapp
$ bundle- Create the database
$ rake db:migrate- Enable caching in the development environment
$ touch ./tmp/caching-dev.txt- Run the rails application
$ rails s-
Open browswer to http://localhost:3000, this will show the main page for the application.
-
NOTE: The test suite can be run at the command line using:
$ rspecThe application main page will show a list of addresses that are currently stored in the system. At this point, there will not be any addresses to show since this is the first time the application is run.
To use the application:
- At the top navigation, click on "New Address".
- An address input form will appear, fill out the fields with valid data, and press the "Create" button
- The browser will be redirected to the Address detail page, which shows the inputed data along with the weather for that address. This intial weather information is obtained directly from the Open Weather Api and cached for 30 minutes.
- Refresh the address detail page, and a "Cached" badge will appear indicating the weather data has been read from the cache for this page load.
- Click on "Addresses" at the top of the page, and the browser will return inital page, showing a list of addresses stored in the system. Clicking on an address will take you to the detail page for that address.
The application uses the Model-View-Controller design pattern as implemented by the Ruby on Rails platform. The following components are used:
- Address - model used to store addresses created by the user
- Addresses
- Addresses - receives user requests, uses models/views to generate HTML output
- Api::Addresses - receives user requests, uses models to generate JSON output
The application uses a Service class to interface with the Open Weather Api. The class returns plain ruby objects (Arrays and Hashes) to abstract away Open Weather Client, allowing for easily replacing it with another client in the future.
- WeatherService - retrieves weather data for a zipcode from the Open Weather Api.
Rspec is used to build the test suite.
- address_spec - unit tests for the Address model
- addresses_spec - tests for the Addresses Controller
- api/addresses_spec - tests for the Addresses Api Controller
- weather_service_spec - tests for the weather service
The VCR gem is used to mock the API output of the Open Weather API. This is to ensure that the tests can run without needing the Open Weather API every time it runs.
The open-weather-ruby-client is used to access the Open Weather Api version 2.5. This is the free version of their API. A key is needed to access the the Open Weather Api, and is included in this repository in the encrypted environment file.
SPECIAL NOTE: The rails master.key file has been included in the repository for easy decrypting of the Open Weather Api key. Normally, THIS SHOULD NOT BE DONE, but was done to make it easier to just run the application.
There is a small mixin in the lib directory for the open-weather-ruby-client gem. The gem did not implement access to the version 2.5 forcast api, so a mixin was needed to add that functionality. This allowed the application to use the free version of the api and get the forcast data needed.
- forcast.rb - code to add retrieving 2.5 forcast data from Open Weather api
This application primarily uses the following libraries:
- Ruby on Rails (7.1.3) - Main application platform
- Open Weather Ruby Client (0.4.0) - Client for retrieving weather data
- Rspec / Rspec-Rails (3.1.2 / 6.1.1) - Testing platform
- BootStrap (5.1.3) - CSS Styling for Server Side Rendering
- Rubocop (1.60.2) - Ruby Code Formatter and Analyzer
There are other gems and libraries in use, but these are the primary ones.


