Skip to content

Commit

Permalink
Exposed the national weather alerts response in the One Call API (#25)
Browse files Browse the repository at this point in the history
* Added alerts to the one_call API response.

* Update change log and readme.

* Added alerts to the one_call spec

Added national alerts section to VCR fixture.
Added check for each alert field value.

* Fixed formatting in CHANGELOG and removed superfluous Alert constructor.

* Rerecorded the lat_lon spec with a location that has current alerts and updated expectations to match.

* Added alerts to the one_call API response.

* Update change log and readme.

* Added alerts to the one_call spec

Added national alerts section to VCR fixture.
Added check for each alert field value.

* Fixed formatting in CHANGELOG and removed superfluous Alert constructor.

* Rerecorded the lat_lon spec with a location that has current alerts and updated expectations to match.

* Removed extra change long entry

* DRY'd up alert spec a bit.
  • Loading branch information
troya2 authored Jan 2, 2024
1 parent aeac490 commit d82ac09
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.4.1 (Next)

* [#25](https://github.com/dblock/open-weather-ruby-client/pull/25): Exposed the national weather alerts response in the One Call API - [@troya2](https://github.com/troya2).
* Your contribution here.

### 0.4.0 (2023/08/13)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ data.main.temp # => 285.15

### One Call

[One Call API](https://openweathermap.org/api/one-call-api) provides current weather, minute forecast for 1 hour, hourly forecast for 48 hours, daily forecast for 7 days, and historical weather data for 5 previous days for any geographical coordinate.
[One Call API](https://openweathermap.org/api/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](lib/open_weather/models/one_call) for all available models and properties.

Expand All @@ -182,6 +182,7 @@ data.current # => OpenWeather::Models::OneCall::CurrentWeather
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.
Expand Down
1 change: 1 addition & 0 deletions lib/open_weather/models/one_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
require_relative 'one_call/hourly_weather'
require_relative 'one_call/minutely_weather'
require_relative 'one_call/weather'
require_relative 'one_call/alert'
15 changes: 15 additions & 0 deletions lib/open_weather/models/one_call/alert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class Alert < Model
property 'sender_name'
property 'event'
property 'start', transform_with: ->(v) { Time.at(v).utc } # UTC
property 'end', transform_with: ->(v) { Time.at(v).utc } # UTC
property 'description'
end
end
end
end
2 changes: 2 additions & 0 deletions lib/open_weather/models/one_call/weather.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Weather < Model
property 'minutely' # minute forecast weather
property 'hourly' # hourly forecast weather
property 'daily' # daily forecast weather
property 'alerts' # weather alerts for the location

def initialize(args = nil, options = {})
super args, options
Expand All @@ -19,6 +20,7 @@ def initialize(args = nil, options = {})
self.minutely = minutely.map { |i| OpenWeather::Models::OneCall::MinutelyWeather.new(i, options) } if minutely
self.hourly = hourly.map { |i| OpenWeather::Models::OneCall::HourlyWeather.new(i, options) } if hourly
self.daily = daily.map { |i| OpenWeather::Models::OneCall::DailyWeather.new(i, options) } if daily
self.alerts = alerts.map { |i| OpenWeather::Models::OneCall::Alert.new(i, options) } if alerts
end
end
end
Expand Down
150 changes: 84 additions & 66 deletions spec/fixtures/open_weather/one_call/lat_lon.yml

Large diffs are not rendered by default.

33 changes: 26 additions & 7 deletions spec/open_weather/one_call/one_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@
include_context 'API client'

it 'lat, lon', vcr: { cassette_name: 'one_call/lat_lon' } do
data = client.one_call(lat: 33.441792, lon: -94.037689)
data = client.one_call(lat: 33.5312, lon: -111.9426)

expect(data).to be_a OpenWeather::Models::OneCall::Weather
expect(data.lat).to eq 33.44
expect(data.lon).to eq(-94.04)
expect(data.timezone).to eq 'America/Chicago'
expect(data.lat).to eq 33.5312
expect(data.lon).to eq(-111.9426)
expect(data.timezone).to eq 'America/Phoenix'
expect(data.current).to be_a OpenWeather::Models::OneCall::CurrentWeather

# Minutely
expect(data.minutely).to be_a Array
expect(data.minutely.size).to eq 61
expect(data.minutely.size).to eq 60
expect(data.minutely.first).to be_a OpenWeather::Models::OneCall::MinutelyWeather
expect(data.minutely.first.precipitation).to eq 0

# Hourly
expect(data.hourly).to be_a Array
expect(data.hourly.size).to eq 48
expect(data.hourly.first).to be_a OpenWeather::Models::OneCall::HourlyWeather
expect(data.hourly.first.temp).to eq 295.55
expect(data.hourly.first.temp).to eq 289.2

# Daily
expect(data.daily).to be_a Array
expect(data.daily.size).to eq 8
expect(data.daily.first).to be_a OpenWeather::Models::OneCall::DailyWeather
expect(data.daily.first.temp.night).to eq 293.67
expect(data.daily.first.temp.night).to eq 286.56

# Alerts
data.alerts.first.tap do |alert|
expect(alert).to be_a OpenWeather::Models::OneCall::Alert
expect(alert.sender_name).to eq 'NWS Phoenix (Central Arizona and California Desert)'
expect(alert.event).to eq 'Air Quality Alert'
expect(alert.start).to be_a Time
expect(alert.end).to be_a Time
expect(alert.start.to_s).to eq '2023-12-29 17:31:00 UTC'
expect(alert.end.to_s).to eq '2024-01-02 04:00:00 UTC'
expect(alert.description).to include '...PM-2.5 HIGH POLLUTION ADVISORY FOR MARICOPA COUNTY'
end
end

it 'lat, lon, excluding minutely and hourly', vcr: { cassette_name: 'one_call/lat_lon_exclude_minutely_hourly' } do
Expand Down

0 comments on commit d82ac09

Please sign in to comment.