Skip to content

Commit

Permalink
Merge pull request #42 from alliedcode/feature/30-day-forecast
Browse files Browse the repository at this point in the history
Added support for the thirty day forecast in the pro 2.5 API
  • Loading branch information
troya2 authored Jul 7, 2024
2 parents 2274c1a + 99e637c commit 5df3bea
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.5.1 (Next)

* [#40](https://github.com/dblock/open-weather-ruby-client/pull/40): Fixing rubocop violations and improved .rubocop.yml - [@troya2](https://github.com/troya2).
* [#42](https://github.com/dblock/open-weather-ruby-client/pull/42): Added support for the thirty day forecast in the pro 2.5 API - [@troya2](https://github.com/troya2).
* Your contribution here.

### 0.5.0 (2024/07/03)
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o
- [Current and Forecast Weather](#current-and-forecast-weather)
- [Historical Weather](#historical-weather)
- [Hourly Forecast (Pro)](#hourly-forecast-pro)
- [30 Day Forecast (Pro)](#30-day-forecast-pro)
- [Stations](#stations)
- [Register a Station](#register-a-station)
- [List Stations](#list-stations)
Expand Down Expand Up @@ -221,9 +222,10 @@ data.hourly # => Array[OpenWeather::Models::OneCall::HourlyWeather]
The [Hourly Forecast API](https://openweathermap.org/api/hourly-forecast) provides hourly weather forecast for 4 days. Note: This API requires a paid api-key from [OpenWeather.org](https://openweathermap.org/full-price#current).

```ruby
data = client.client.hourly(lat: 33.5312, lon: -111.9426) # => OpenWeather::Models::Forecast::Hourly
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
Expand All @@ -238,6 +240,31 @@ 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](https://openweathermap.org/api/forecast30) provides daily weather forecast for 30 days. Note: This API requires a paid api-key from [OpenWeather.org](https://openweathermap.org/full-price#current).

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

### Stations

The [Stations API](https://openweathermap.org/stations) lets your manage personal weather stations and measurements.
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 @@ -6,6 +6,7 @@ class Client
include Request
include Endpoints::Current
include Endpoints::Hourly
include Endpoints::ThirtyDayForecast
include Endpoints::OneCall
include Endpoints::Stations

Expand Down
1 change: 1 addition & 0 deletions lib/open_weather/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require_relative 'endpoints/one_call'
require_relative 'endpoints/stations'
require_relative 'endpoints/hourly'
require_relative 'endpoints/thirty_day_forecast'
16 changes: 16 additions & 0 deletions lib/open_weather/endpoints/thirty_day_forecast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module OpenWeather
module Endpoints
module ThirtyDayForecast
def thirty_day_forecast(lat, lon = nil, options = {})
# default to the pro endpoint if not specified
endpoint = options.delete(:endpoint) || pro_endpoint
options = options.merge(endpoint: endpoint)

options = lat.is_a?(Hash) ? options.merge(lat) : options.merge(lat: lat, lon: lon)
OpenWeather::Models::Forecast::ThirtyDay::ThirtyDay.new(get('2.5/forecast/climate', options), options)
end
end
end
end
1 change: 1 addition & 0 deletions lib/open_weather/models/forecast.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative 'forecast/hourly'
require_relative 'forecast/thirty_day'
require_relative 'forecast/forecast'
require_relative 'forecast/city'
1 change: 1 addition & 0 deletions lib/open_weather/models/forecast/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class City < Model
property 'timezone' # shift in seconds from 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 'population' # City population
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/open_weather/models/forecast/thirty_day.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative 'thirty_day/thirty_day'
require_relative 'thirty_day/forecast'
require_relative 'thirty_day/temp'
33 changes: 33 additions & 0 deletions lib/open_weather/models/forecast/thirty_day/forecast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Forecast
module ThirtyDay
class Forecast < Model
property 'dt', transform_with: ->(v) { Time.at(v).utc } # time of data forcasted, 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' # Array of OpenWeather::Models::Forecast::ThityDay::Temp
property 'feels_like' # OpenWeather::Models::OneCall::FeelsLike
property 'pressure' # Atmospheric pressure on the sea level, hPa
property 'humidity' # Humidity, % (e.g. integer 24 means 24% cloudiness)
property 'weather' # Array of OpenWeather::Models::Weather
property 'speed' # Wind speed. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour
property 'deg' # Wind direction, degrees (meteorological)
property 'clouds' # Cloudiness, % (e.g. integer 78 means 78% cloudiness)
property 'rain' # Precipitation volume, mm. Unit will only be in mm
property 'snow' # Snow volume, mm. Unit will only be in mm

def initialize(args = nil, options = {})
super args, options

self.temp = Temp.new(temp, options) if temp
self.feels_like = OpenWeather::Models::OneCall::FeelsLike.new(feels_like, options) if feels_like
self.weather = weather.map { |w| OpenWeather::Models::Weather.new(w, options) } if weather
end
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/open_weather/models/forecast/thirty_day/temp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Forecast
module ThirtyDay
class Temp < Model
temperature_property 'day' # Day temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
temperature_property 'min' # Min daily temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
temperature_property 'max' # Max daily temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
temperature_property 'night' # Night temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
temperature_property 'eve' # Evening temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
temperature_property 'morn' # Morning temperature. Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/open_weather/models/forecast/thirty_day/thirty_day.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Forecast
module ThirtyDay
class ThirtyDay < Model
include Enumerable

property 'cod' # Internal parameter
property 'message' # Internal parameter
property 'city'
property 'cnt' # Number of items in list
property 'list' # List of ??? objects

def initialize(args = nil, options = {})
super args, options

self.list = list.map { |forecast| Forecast.new(forecast, options) } if list
self.city = OpenWeather::Models::Forecast::City.new(city, options) if city
end
end
end
end
end
end
Loading

0 comments on commit 5df3bea

Please sign in to comment.