A Rails web application to retrieve weather forecast information for a given postal address.
Provide a US postal address in a valid USPS format and you get the weather forecast ( temperature, wind and rain) for the next few hours. If addresses from the same zipcode are queried within 30 minutes, we use the Rails cache to retrieve the forecast without hitting the REST APIs.
This app is build with Rails 7.0.8 and Ruby 3.2.0
Sample address you can use to test this app: 454 Bolinger Cmn, Fremont, CA 94539 4600 Silver Hill Rd, Washington, DC 20233
- Enable the Rails cache:
$ rails dev:cache
- Run the app:
$ rails s
-
Open
127.0.0.1:3000in the browser to use the app. -
Run unit tests:
$ bin/rails test
After Googling around a bit on how to retrieve weather info, I find two options:
- Use Ruby gems to pull from sources like OpenWeatherMap. These require an API key.
- Use REST API provided for free by National Weather Service (NWS).
Since the information we are pulling is pretty simple, option (2) seems like a lightweight solution.
However, the NWS API takes a (latitude, longitude) input, so we need a way to convert an address (or zipcode) into lat-long.
This seems to be called GeoCoding and the helpful NWS FAQ page points to a GeoCoding REST API maintained by the Census Bureau.
Once this was figured out it was pretty simple to build the app. The only complication was figuring out that the X and Y values in the Geocode response was actually (long, lat) and not (lat, long).
