Aschenblog: Thoughts on Code and Fabrication

Let It Snow With the Accuweather Gem

AccuWeather is one of the standard widgets on my Android phone. I found no API wrapper for it after searching rubygems.org and I thought it would be fun to write one after spending a little time reverse engineering the web API (related post).

First, find a location via the city search API:

1
2
3
require 'accuweather'

locations = Accuweather.city_search(name: 'vancouver')

This returns a list of locations that match the input. Each location provides several pieces of information including a city identifier:

1
2
3
4
5
6
7
vancouver = locations.first

vancouver.id        # => 'cityId:53286'
vancouver.city      # => 'Vancouver'
vancouver.state     # => 'Canada (British Columbia)'
vancouver.latitude  # => '49.2448'
vancouver.longitude # => '123.1154'

The gem caches over 175K cities around the world. If the location name is not in the cache, the gem will make a HTTP request to AccuWeather to try and find it.

The location identifier is used to get more information about current weather conditions:

1
2
3
4
5
6
current_weather = Accuweather.get_conditions(location_id: 'cityId:53286').current
current_weather.temperature    # => '41'
current_weather.weather_text   # => 'Partly Sunny'
current_weather.pressure       # => '30.35'
current_weather.humidity       # => '43%'
current_weather.cloud_cover    # => '40%'

A week of day and nighttime forecasts are available:

1
2
3
4
5
6
7
8
9
10
11
weather_forecast = Accuweather.get_conditions(location_id: 'cityId:53286').forecast
last_forecast_day = weather_forecast.last
last_forecast_day.date        # => "12/3/2015"
last_forecast_day.day_of_week # => "Thursday"
last_forecast_day.sunrise     # => "7:49 AM"
last_forecast_day.sunset      # => "4:16 PM"

# Get the dates, daytime high and nighttime low temperatures
weather_forecast.map(&:date)                             #  => ["11/27/2015", "11/28/2015", "11/29/2015", "11/30/2015", "12/1/2015", "12/2/2015", "12/3/2015"]
weather_forecast.map(&:daytime).map(&:high_temperature)  # => ["45", "45", "47", "44", "44", "48", "48"]
weather_forecast.map(&:nighttime).map(&:low_temperature) # => ["27", "28", "31", "32", "40", "42", "36"]

The API returns a weather_icon number for the current weather and forecast. This number appears to correspond to PNG file from AccuWeather.com. I downloaded the full set of PNG files and included them here, but found a handful of better looking weather webfonts out there (links below).

Related resources:

Accuweather gem links:

Stay warm out there and have fun!

Comments