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

Added support for one-call API. #14

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-05-01 09:30:27 -0400 using RuboCop version 0.81.0.
# on 2020-05-14 00:06:00 -0400 using RuboCop version 0.81.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -13,7 +13,7 @@ Naming/FileName:
Exclude:
- 'lib/open-weather-ruby-client.rb'

# Offense count: 38
# Offense count: 84
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.2.0 (Next)

* [#14](https://github.com/dblock/open-weather-ruby-client/pull/14): Added support for One Call API - [@dblock](https://github.com/dblock).
* [#12](https://github.com/dblock/open-weather-ruby-client/pull/12): Cache `Faraday::Connection` for persistent adapters - [@dblock](https://github.com/dblock).
* [#13](https://github.com/dblock/open-weather-ruby-client/pull/13): Require Faraday >= 1.0 - [@dblock](https://github.com/dblock).
* Your contribution here.
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o
- [Cities Within a Rectangle Zone](#cities-within-a-rectangle-zone)
- [Cities Within a Circle](#cities-within-a-circle)
- [Multiple Cities by Id](#multiple-cities-by-id)
- [One Call](#one-call)
- [Current and Forecast Weather](#current-and-forecast-weather)
- [Historical Weather](#historical-weather)
- [Configuration](#configuration)
- [Units](#units)
- [Language](#language)
Expand Down Expand Up @@ -149,6 +152,40 @@ data.first.name # 'Moscow'
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.

#### Current and Forecast Weather

```ruby
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.minutely # => Array[OpenWeather::Models::OneCall::MinutelyWeather]
data.hourly # => Array[OpenWeather::Models::OneCall::HourlyWeather]
data.daily # => Array[OpenWeather::Models::OneCall::DailyWeather]
```

Exclude minutely and hourly data.

```ruby
client.one_call(lat: 33.441792, lon: -94.037689, exclude: ['minutely', 'hourly'])
```

#### Historical Weather

```ruby
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]
```

## Configuration

You can configure client options, globally.
Expand Down
1 change: 1 addition & 0 deletions lib/open_weather/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Client
include Connection
include Request
include Endpoints::Current
include Endpoints::OneCall

attr_accessor(*Config::ATTRIBUTES)

Expand Down
1 change: 1 addition & 0 deletions lib/open_weather/endpoints.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative 'endpoints/current'
require_relative 'endpoints/one_call'
15 changes: 15 additions & 0 deletions lib/open_weather/endpoints/one_call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module OpenWeather
module Endpoints
module OneCall
def one_call(lat, lon = nil, options = {})
options = lat.is_a?(Hash) ? options.merge(lat) : options.merge(lat: lat, lon: lon)
options[:exclude] = options[:exclude].join(',') if options[:exclude].is_a?(Array)
options[:dt] = options[:dt].to_i if options[:dt].is_a?(Time)
path = options.key?(:dt) ? 'onecall/timemachine' : 'onecall'
OpenWeather::Models::OneCall::Weather.new(get(path, options))
end
end
end
end
1 change: 1 addition & 0 deletions lib/open_weather/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
require_relative 'models/rain'
require_relative 'models/snow'
require_relative 'models/list'
require_relative 'models/one_call'
2 changes: 1 addition & 1 deletion lib/open_weather/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module OpenWeather
module Models
class Model < Hashie::Trash
include Hashie::Extensions::IgnoreUndeclared
# include Hashie::Extensions::IgnoreUndeclared
end
end
end
9 changes: 9 additions & 0 deletions lib/open_weather/models/one_call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require_relative 'one_call/temp'
require_relative 'one_call/feels_like'
require_relative 'one_call/current_weather'
require_relative 'one_call/daily_weather'
require_relative 'one_call/hourly_weather'
require_relative 'one_call/minutely_weather'
require_relative 'one_call/weather'
27 changes: 27 additions & 0 deletions lib/open_weather/models/one_call/current_weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class CurrentWeather < Model
property 'dt', transform_with: ->(v) { Time.at(v).utc } # time of the forecasted data UTC
property 'sunrise', transform_with: ->(v) { Time.at(v).utc } # sunrise time, UTC
property 'sunset', transform_with: ->(v) { Time.at(v).utc } # sunset time, UTC
property 'temp' # temperature
property 'feels_like' # temperature, accounts for the human perception of weather
property 'pressure' # atmospheric pressure on the sea level, hPa
property 'humidity' # humidity, %
property 'dew_point' # atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form
property 'clouds' # cloudiness, %
property 'uvi' # UV index
property 'visibility' # average visibility, meters
property 'wind_speed' # wind speed
property 'wind_gust' # wind gust
property 'wind_deg' # wind direction, degrees (meteorological)
property 'rain', transform_with: ->(v) { OpenWeather::Models::Rain.new(v) }
property 'snow', transform_with: ->(v) { OpenWeather::Models::Snow.new(v) }
property 'weather', transform_with: ->(v) { v.map { |i| OpenWeather::Models::Weather.new(i) } }
end
end
end
end
26 changes: 26 additions & 0 deletions lib/open_weather/models/one_call/daily_weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class DailyWeather < Model
property 'dt', transform_with: ->(v) { Time.at(v).utc } # time of the forecasted data UTC
property 'sunrise', transform_with: ->(v) { Time.at(v).utc } # sunrise time, UTC
property 'sunset', transform_with: ->(v) { Time.at(v).utc } # sunset time, UTC
property 'temp', transform_with: ->(v) { OpenWeather::Models::OneCall::Temp.new(v) }
property 'feels_like', transform_with: ->(v) { OpenWeather::Models::OneCall::FeelsLike.new(v) }
property 'pressure' # atmospheric pressure on the sea level, hPa
property 'humidity' # humidity, %
property 'dew_point' # atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form
property 'wind_speed' # wind speed
property 'wind_gust' # wind gust
property 'wind_deg' # wind direction, degrees (meteorological)
property 'clouds' # cloudiness, %
property 'uvi' # UV index
property 'rain' # precipitation volume, mm
property 'snow' # snow volume, mm
property 'weather', transform_with: ->(v) { v.map { |i| OpenWeather::Models::Weather.new(i) } }
end
end
end
end
14 changes: 14 additions & 0 deletions lib/open_weather/models/one_call/feels_like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class FeelsLike < Model
property 'morn'
property 'day'
property 'eve'
property 'night'
end
end
end
end
24 changes: 24 additions & 0 deletions lib/open_weather/models/one_call/hourly_weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class HourlyWeather < Model
property 'dt', transform_with: ->(v) { Time.at(v).utc } # Time of the forecasted data UTC
property 'temp'
property 'feels_like'
property 'pressure' # atmospheric pressure on the sea level, hPa
property 'humidity' # humidity, %
property 'dew_point' # atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form
property 'clouds' # cloudiness, %
property 'visibility' # average visibility, meters
property 'wind_speed' # wind speed.
property 'wind_gust' # wind gust.
property 'wind_deg' # wind direction, degrees (meteorological)
property 'rain', transform_with: ->(v) { OpenWeather::Models::Rain.new(v) }
property 'snow', transform_with: ->(v) { OpenWeather::Models::Snow.new(v) }
property 'weather', transform_with: ->(v) { v.map { |i| OpenWeather::Models::Weather.new(i) } }
end
end
end
end
12 changes: 12 additions & 0 deletions lib/open_weather/models/one_call/minutely_weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class MinutelyWeather < Model
property 'dt', transform_with: ->(v) { Time.at(v).utc } # time of the forecasted data UTC
property 'precipitation'
end
end
end
end
16 changes: 16 additions & 0 deletions lib/open_weather/models/one_call/temp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class Temp < Model
property 'morn'
property 'day'
property 'eve'
property 'night'
property 'min'
property 'max'
end
end
end
end
17 changes: 17 additions & 0 deletions lib/open_weather/models/one_call/weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module OpenWeather
module Models
module OneCall
class Weather < Model
property 'lat' # geographical coordinates of the location (latitude)
property 'lon' # geographical coordinates of the location (longitude)
property 'timezone' # timezone name for the requested location
property 'current', transform_with: ->(v) { OpenWeather::Models::OneCall::CurrentWeather.new(v) }
property 'minutely', transform_with: ->(v) { v.map { |i| OpenWeather::Models::OneCall::MinutelyWeather.new(i) } }
property 'hourly', transform_with: ->(v) { v.map { |i| OpenWeather::Models::OneCall::HourlyWeather.new(i) } }
property 'daily', transform_with: ->(v) { v.map { |i| OpenWeather::Models::OneCall::DailyWeather.new(i) } }
end
end
end
end
1 change: 1 addition & 0 deletions lib/open_weather/models/sys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module OpenWeather
module Models
class Sys < Model
property 'timezone' # shift in seconds from UTC
property 'type' # internal parameter
property 'id' # internal parameter
property 'message' # internal parameter
Expand Down
45 changes: 45 additions & 0 deletions spec/fixtures/open_weather/one_call/error_out_of_range.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading