Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed the national weather alerts response in the One Call API #25

Merged
merged 14 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [#21](https://github.com/dblock/open-weather-ruby-client/pull/21), [#20](https://github.com/dblock/open-weather-ruby-client/pull/20), [#19](https://github.com/dblock/open-weather-ruby-client/pull/19), [#18](https://github.com/dblock/open-weather-ruby-client/pull/18): Added support for Stations API - [@wasabigeek](https://github.com/wasabigeek).
* [#22](https://github.com/dblock/open-weather-ruby-client/pull/23): Removed API version from `Config#endpoint` - [@dblock](https://github.com/dblock).
* Exposed the national weather alerts response in the One Call API [@troya2](https://github.com/troya2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the format and danger will stop complaining. Missing a -, a period and a PR link in front.

* Your contribution here.

### 0.2.0 (2020/05/17)
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'
19 changes: 19 additions & 0 deletions lib/open_weather/models/one_call/alert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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'

def initialize(args = nil, options = {})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to override the constructor unless you're doing something extra here. Just remove it.

super args, options
end
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
5 changes: 4 additions & 1 deletion spec/fixtures/open_weather/one_call/lat_lon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ http_interactions:
rain","icon":"10d"}],"clouds":99,"rain":8.01,"uvi":10.95},{"dt":1589824800,"sunrise":1589800419,"sunset":1589850710,"temp":{"day":298.65,"min":289.07,"max":300.81,"night":289.49,"eve":297.92,"morn":289.07},"feels_like":{"day":298.14,"night":288.35,"eve":298.74,"morn":288.15},"pressure":1015,"humidity":56,"dew_point":289.47,"wind_speed":3.6,"wind_deg":21,"weather":[{"id":802,"main":"Clouds","description":"scattered
clouds","icon":"03d"}],"clouds":49,"uvi":10.91},{"dt":1589911200,"sunrise":1589886783,"sunset":1589937154,"temp":{"day":296.27,"min":286.83,"max":298.28,"night":288.9,"eve":297.35,"morn":286.83},"feels_like":{"day":295.57,"night":288.28,"eve":299.19,"morn":286.03},"pressure":1015,"humidity":56,"dew_point":287.09,"wind_speed":2.73,"wind_deg":40,"weather":[{"id":800,"main":"Clear","description":"clear
sky","icon":"01d"}],"clouds":0,"uvi":10.01},{"dt":1589997600,"sunrise":1589973148,"sunset":1590023597,"temp":{"day":298.21,"min":287.71,"max":299.71,"night":290.03,"eve":298.59,"morn":287.71},"feels_like":{"day":299.68,"night":289.89,"eve":301.62,"morn":287.74},"pressure":1014,"humidity":61,"dew_point":290.18,"wind_speed":1.3,"wind_deg":71,"weather":[{"id":800,"main":"Clear","description":"clear
sky","icon":"01d"}],"clouds":0,"uvi":10.16}]}'
sky","icon":"01d"}],"clouds":0,"uvi":10.16}],"alerts": [{"sender_name": "NWS Tulsa (Eastern Oklahoma)","event": "Heat Advisory","start": 1597341600,"end": 1597366800,"description": "...HEAT ADVISORY REMAINS IN EFFECT FROM 1 PM THIS AFTERNOON TO\n8 PM CDT THIS EVENING...\n* WHAT...Heat index values of 105 to 109 degrees expected.\n* WHERE...Creek, Okfuskee, Okmulgee, McIntosh, Pittsburg,\nLatimer, Pushmataha, and Choctaw Counties.\n* WHEN...From 1 PM to 8 PM CDT Thursday.\n* IMPACTS...The combination of hot temperatures and high\nhumidity will combine to create a dangerous situation in which\nheat illnesses are possible."}]}'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't hand-craft VCR-recorded data. You should record a new separate VCR K7 .yml that has alerts data and write a separate unit test for that.

http_version: null
recorded_at: Thu, 14 May 2020 03:09:08 GMT
recorded_with: VCR 5.1.0



17 changes: 17 additions & 0 deletions spec/open_weather/one_call/one_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@

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

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.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.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

# 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

# Alerts
dblock marked this conversation as resolved.
Show resolved Hide resolved
expect(data.alerts.first).to be_a OpenWeather::Models::OneCall::Alert
expect(data.alerts.first.sender_name).to eq 'NWS Tulsa (Eastern Oklahoma)'
expect(data.alerts.first.event).to eq 'Heat Advisory'
expect(data.alerts.first.start).to be_a Time
expect(data.alerts.first.end).to be_a Time
expect(data.alerts.first.start.to_s).to eq '2020-08-13 18:00:00 UTC'
expect(data.alerts.first.end.to_s).to eq '2020-08-14 01:00:00 UTC'
expect(data.alerts.first.description).to include '...HEAT ADVISORY REMAINS IN EFFECT FROM 1 PM THIS AFTERNOON'
end

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