OpenWeather Ruby Client
A Ruby client for the OpenWeather API v2.5 and v3.0.
Unlike other clients, including open-weather, provides a rich first class interface to OpenWeather models, structured timestamps, built-in metrics conversion for temperature and wind speed, offers more consistent error handling, and is implemented with thorough test coverage using actual OpenWeather data.
Table of Contents
Installation
Add to Gemfile.
gem 'open-weather-ruby-client'
Run bundle install.
Usage
Use an access token obtained from API Keys on the OpenWeather website after registration.
client = OpenWeather::Client.new(
api_key: "1a2b3c4d5a6b7c8d9a8b7c6d5a4b3c2d1"
)
Current Weather
Returns current weather.
data = client.current_weather(city: 'London') # => OpenWeather::Models::City::Weather
data.name # => 'London'
data.dt # => Time
data.main.feels_like # => 277.73
data.main.humidity # => 81
data.main.pressure # => 1005
data.main.temp # => 282.57
data.main.temp_max # => 283.15, degrees Kelvin
data.main.temp_max_c # => 10, degrees Celsius
data.main.temp_max_f # => 50.0, degrees Fahrenheit
data.main.temp_min # => 281.48
Returns the current weather in metric units and Russian metadata.
data = client.current_weather(city: 'Moscow', units: 'metric', lang: 'ru') # => OpenWeather::Models::City::Weather
data.name # => 'Москва'
data.main.temp # => 12
Returns weather by city, optional state (in the US) and optional ISO 3166 country code.
Names that cannot be resolved will cause the API call to raise a Faraday::ResourceNotFound error.
client.current_city('Sydney')
client.current_city('London, UK')
client.current_city('London', 'UK')
client.current_city('Albany')
client.current_city('Albany, New York')
client.current_city('Albany, New York', 'US')
client.current_city('Albany, NY', 'US')
client.current_city('Albany', 'New York', 'US')
client.current_city('Albany', 'NY', 'US')
client.current_city('Albany', 'NY') # 2-letter state abbreviation w/o country will raise Faraday::ResourceNotFound
client.current_weather(city: 'Albany', state: 'NY', country: 'US')
Returns weather by city ID.
client.current_city_id(2643743) # => weather in London
client.current_weather(id: 2643743) # => weather in London
Returns weather by latitude and longitude.
client.current_geo(51.51, -0.13) # => weather in London
client.current_weather(lat: 51.51, lon: -0.13) # => weather in London
Returns weather by zip code with an optional country code (defaults to US).
client.current_zip(10018, 'US') # => weather in New York, 10018
client.current_weather(zip: 10018, country: 'US') # => weather in New York, 10018
See OpenWeather::Models::City::Weather and related OpenWeather::Models for all available properties.
Current Weather for Several Cities
Collection APIs return OpenWeather::Models::List, which includes multiple instances of OpenWeather::Models::City::Weather.
Cities Within a Rectangle Zone
data = client.current_cities_geo_box(12, 32, 15, 37, 10) # => OpenWeather::Models::List
data.first.name # 'Birkirkara'
data.main.temp # => 16.23
You can optionally name parameters.
client.current_cities_geo_box(lon_left: 12, lat_bottom: 32, lon_right: 15, lat_top: 37, zoom: 10) # => OpenWeather::Models::List
You can use server clustering of points with cluster: true.
client.current_cities_geo_box(12, 32, 15, 37, 10, cluster: true) # => OpenWeather::Models::List
Cities Within a Circle
data = client.current_cities_geo_circle(55.5, 37.5, 10) # => OpenWeather::Models::List
data.first.name # 'Shcherbinka'
data.main.temp # => 276.86
You can optionally name parameters.
client.current_cities_geo_circle(lat: 55.5, lon: 37.5, cnt: 7) # => OpenWeather::Models::List
Multiple Cities by Id
data = client.current_cities_id(524901, 703448, 2643743) # => OpenWeather::Models::List
data.first.name # 'Moscow'
data.main.temp # => 285.15
One Call
One Call API provides current weather, minute forecast for 1 hour, hourly forecast for 48 hours, daily forecast for 7 days, historical weather data for 5 previous days for any geographical coordinate, and national weather alerts.
See OpenWeather::Models::OneCall for all available models and properties.
Current and Forecast Weather
data = client.one_call(lat: 33.441792, lon: -94.037689) # => OpenWeather::Models::OneCall::Weather
data.lat # => 33.44
data.lon # => -94.04
data.timezone # => 'America/Chicago'
data.current # => OpenWeather::Models::OneCall::CurrentWeather
data.data # => Array]OpenWeather::Models::OneCall::CurrentWeather] (on base plan)
data.minutely # => Array[OpenWeather::Models::OneCall::MinutelyWeather]
data.hourly # => Array[OpenWeather::Models::OneCall::HourlyWeather]
data.daily # => Array[OpenWeather::Models::OneCall::DailyWeather]
data.alerts # => Array[OpenWeather::Models::OneCall::Alert]
Exclude minutely and hourly data.
client.one_call(lat: 33.441792, lon: -94.037689, exclude: ['minutely', 'hourly'])
Historical Weather
data = client.one_call(lat: 33.441792, lon: -94.037689, dt: Time.now - 24 * 60 * 60) # => OpenWeather::Models::OneCall::Weather
data.lat # => 33.44
data.lon # => -94.04
data.timezone # => 'America/Chicago'
data.current # => OpenWeather::Models::OneCall::CurrentWeather
data.hourly # => Array[OpenWeather::Models::OneCall::HourlyWeather]
Hourly Forecast (Pro)
The Hourly Forecast API provides hourly weather forecast for 4 days. Note: This API requires a paid api-key from OpenWeather.org.
data = client.client.hourly(lat: 33.5312, lon: -111.9426, appid: "<your api key>") # => OpenWeather::Models::Forecast::Hourly
data.cnt # => 96 (number of entries)
data.city # => OpenWeather::Models::Forecast::City
data.list.first # => OpenWeather::Models::Forecast::Forecast
data.list.first.dt # => Time
data.list.first.main # => OpenWeather::Models::Forecast::Main
data.list.first.weather # => Array[OpenWeather::Models::Forecast::Weather]
data.list.first.clouds # => OpenWeather::Models::Forecast::Clouds or nil
data.list.first.wind # => OpenWeather::Models::Forecast::Wind or nil
data.list.first.visibility # => 10000
data.list.first.pop # => 0.1 (probability of precipitation from 0.0 to 1.0 (0% to 100%))
data.list.first.rain # => OpenWeather::Models::Forecast::Rain or nil
data.list.first.snow # => OpenWeather::Models::Forecast::Snow or nil
data.list.first.sys # => OpenWeather::Models::Forecast::Sys or nil
data.list.first.dt_txt # => String (Time of data forecasted, ISO, UTC)
30 Day Forecast (Pro)
The 30 Day Forecast API provides daily weather forecast for 30 days. Note: This API requires a paid api-key from OpenWeather.org.
data = client.client.forecast(lat: 33.5312, lon: -111.9426, appid: "<your api key>") # => OpenWeather::Models::Forecast::ThirtyDay::ThirtyDay
data.cnt # => 30 (number of entries - sometimes this is 29)
data.city # => OpenWeather::Models::Forecast::City
data.list.first # => OpenWeather::Models::Forecast::ThirtyDay::Forecast
data.list.first.dt # => Time - time of data forcasted, UTC
data.list.first.sunrise # => Time - Sunrise time, UTC
data.list.first.sunset # => Time - Sunset time, UTC
data.list.first.temp # => OpenWeather::Models::Forecast::ThirtyDay::Temp
data.list.first.feels_like # => OpenWeather::Models::OneCall::FeelsLike
data.list.first.pressure # => int - Atmospheric pressure on the sea level, hPa
data.list.first.humidity # => int - Humidity, % (e.g. integer 24 means 24% cloudiness)
data.list.first.weather # => Array[OpenWeather::Models::Weather]
data.list.first.speed # => double - Wind speed. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour
data.list.first.deg # => int - Wind direction, degrees (meteorological)
data.list.first.clouds # => int - Cloudiness, % (e.g. integer 78 means 78% cloudiness)
data.list.first.rain # => double or nil - Precipitation volume, mm. Please note that only mm as units of measurement are available for this parameter
data.list.first.snow # => double or nil - Snow volume, mm. Please note that only mm as units of measurement are available for this parameter
5 Day Forecast
The 5 Day Forecast API provides weather forecast for 5 days with data every 3 hours by geographic coordinates.
data = client.client.five_day_forecast(lat: 33.5312, lon: -111.9426, appid: "<your api key>") # => OpenWeather::Models::Forecast::FiveDay
data.cnt # => 40 (number of entries)
data.list.first # => OpenWeather::Models::Forecast::FiveDay::Forecast
data.list.first.dt # => Time
data.list.first.main # => OpenWeather::Models::Main
data.list.first.weather # => Array[OpenWeather::Models::Weather]
data.list.first.clouds # => OpenWeather::Models::Clouds or nil
data.list.first.wind # => OpenWeather::Models::Wind or nil
data.list.first.visibility # => 10000
data.list.first.pop # => 0.1 (probability of precipitation from 0.0 to 1.0 (0% to 100%))
data.list.first.rain # => OpenWeather::Models::Rain or nil
data.list.first.snow # => OpenWeather::Models::Snow or nil
data.list.first.sys # => OpenWeather::Models::Sys or nil
data.list.first.dt_txt # => String (Time of data forecasted, ISO, UTC)
Stations
The Stations API lets your manage personal weather stations and measurements.
Register a Station
To register a station, you can call the client method:
data = client.register_station(external_id: 'SF_TEST001', ...) # => OpenWeather::Models::Station
data.id # => '5ed2118acca8ce0001f1aeg1'
data.external_id # => 'SF_TEST001'
Alternatively, call register! on an instance of Station:
model = OpenWeather::Models::Station.new(external_id: 'SF_TEST001', ...)
model.register!
model.id # => '5ed2118acca8ce0001f1aeg1'
List Stations
To list all stations, call the client method:
client.list_stations # => Array[OpenWeather::Models::Station]
Get Station
To get a station, call the client method:
client.get_station('5ed2118acca8ce0001f1aeg1') # => OpenWeather::Models::Station
Update Station
To update a station, call the client method:
client.update_station('5ed2118acca8ce0001f1aeg1', external_id: 'SF_TEST002') # => OpenWeather::Models::Station
Alternatively, call update! on an instance of Station:
model = OpenWeather::Models::Station.new(external_id: 'SF_TEST001', ...)
model.register!
model.update!(external_id: 'SF_TEST002')
model.external_id # => 'SF_TEST002'
Delete Station
To delete a station, call the client method:
data = client.delete_station('5ed2118acca8ce0001f1aeg1') # => nil
Create Measurements
To create measurements, call the client method: