What's the Weather is a an app that returns the temperature and weather forecast from an address. It's written with Ruby on Rails and the only extra gems installed are:
- geocoder - for getting the latitude and longitude from the Address street and zipcode.
- httparty - for making api calls to the weather service to return forecast and temperature.
- pry - for looking at the guts.
- The action starts for What's the Weather when the user fills out an address form.
- Rails handles the
@address
object as normal. - The
AddressController
calls a service object which determines which adapter (currently,UsWeatherAdapter
) will get the weather data Address#create
takes the data from the service object to build aweather
instance for theaddress
- On successful save,
address
andweather
are then displayed on theAddress#show
page along with a form for a new address. - A 30 minute cookie is used to store recent addresses' zipcodes which points to their weather forecast and toggles a "recent" badge on the UI
- This app only uses
Address#new
,Address#show
andAddress#create
actions for the user facing pages. All the others are reserved for admin duties.
-
User fills out the form
-
The address
zipcode
andstreet
are used to get the address latitude and longitude.before_validation :geocode
-
The latitude and longitude are used to call an API to get the data needed to get weather area coordinates
HTTParty.get("https://api.weather.gov/points/#{lat_lang.first},#{lat_lang.last}", headers: { "Accept" => "application/json" })
-
The coordinates are used to get the weather data to populate a
weather
instanceHTTParty.get("https://api.weather.gov/gridpoints/#{grid["cwa"]}/#{grid["gridX"]},#{grid["gridY"]}/forecast", headers: { "Accept" => "application/json" })
-
The output is a hash:
{ temperature: Integer, forecast: String }
- An adapter is used that calls the weather.gov API which is US only, so future adapters can be added for international addresses.
- Rails scaffold was used to create the Address class. This is so the unused actions will be in place for implementing an adminstration area.
- Presenters are used to make messages to the UI as close to final form as possible to facilitate adding a frontend framework.
- Internationalization & Accessbility!
- Ruby version: 3.0.0
- Rails version: 7.0.4.2
- Bootstrap version: 5.3.0
- Some bogus addresses are generating
geocode lat/long
which results in aweather
association with adapter that results as anInteger
instead of aHash
. This causesAddress#create
to fail.