Skip to content

Commit

Permalink
Add measurements (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek authored Jun 10, 2020
1 parent 335f812 commit dbbd6d7
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### 0.2.1 (Next)

* Your contribution here.
* [#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).
* [#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).

### 0.2.0 (2020/05/17)

Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o
- [Get Station](#get-station)
- [Update Station](#update-station)
- [Delete Station](#delete-station)
- [Create Measurements](#create-measurements)
- [Get Measurements](#get-measurements)
- [Configuration](#configuration)
- [Units](#units)
- [Converting Temperature](#converting-temperature)
Expand Down Expand Up @@ -253,6 +255,42 @@ To delete a station, call the client method:
data = client.delete_station('5ed2118acca8ce0001f1aeg1') # => nil
```

#### Create Measurements

To create measurements, call the client method:
```ruby
client.create_measurements([
{
"station_id": -1,
"dt": 1479817340,
"temperature": 18.7,
"wind_speed": 1.2,
"wind_gust": 3.4,
"pressure": 1021,
"humidity": 87,
"rain_1h": 2,
"clouds": [
{
"condition": 'NSC'
}
]
}
]) # => nil
```

#### Get Measurements

To get measurements, call the client method with the required parameters:
```ruby
client.get_measurements(
station_id: '5ed21a12cca8ce0001f1aef1',
type: 'd',
limit: 100,
from: 1469817340,
to: 1591620047
) # => Array[OpenWeather::Models::Stations::Measurement]
```

## Configuration

You can configure client options, globally.
Expand Down
13 changes: 13 additions & 0 deletions lib/open_weather/endpoints/stations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def delete_station(id)
nil
end

def create_measurements(measurements, options = {})
post('measurements', options.merge(body: measurements))
nil
end

def get_measurements(options)
required_keys = %i[station_id type limit from to]
missing_keys = required_keys - options.keys
raise ArgumentError, "Missing params: #{missing_keys.join(', ')}" if missing_keys.any?

get('measurements', options).map { |m| OpenWeather::Models::Stations::Measurement.new(m) }
end

private

def validate_id(id)
Expand Down
5 changes: 5 additions & 0 deletions lib/open_weather/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
require_relative 'models/city'
require_relative 'models/one_call'
require_relative 'models/station'
require_relative 'models/stations/measurement'
require_relative 'models/stations/temp'
require_relative 'models/stations/humidity'
require_relative 'models/stations/pressure'
require_relative 'models/stations/precipitation'
12 changes: 12 additions & 0 deletions lib/open_weather/models/stations/humidity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Stations
class Humidity < Model
property 'average'
property 'weight'
end
end
end
end
27 changes: 27 additions & 0 deletions lib/open_weather/models/stations/measurement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Stations
class Measurement < Model
property 'station_id' # The internal ID of the station
property 'type' # Type of the aggregated data - minute, hour or day. Specifies the letters m, h or d respectively
property 'date' # Time of measurement
property 'temp'
property 'humidity'
property 'wind'
property 'precipitation'
property 'pressure'

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

self.temp = OpenWeather::Models::Stations::Temp.new(temp, options) if temp
self.humidity = OpenWeather::Models::Stations::Humidity.new(humidity, options) if humidity
self.pressure = OpenWeather::Models::Stations::Pressure.new(pressure, options) if pressure
self.precipitation = OpenWeather::Models::Stations::Precipitation.new(precipitation, options) if precipitation
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/open_weather/models/stations/precipitation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Stations
class Precipitation < Model
property 'rain'
end
end
end
end
14 changes: 14 additions & 0 deletions lib/open_weather/models/stations/pressure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Stations
class Pressure < Model
property 'min'
property 'max'
property 'average'
property 'weight'
end
end
end
end
14 changes: 14 additions & 0 deletions lib/open_weather/models/stations/temp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module OpenWeather
module Models
module Stations
class Temp < Model
temperature_property 'min'
temperature_property 'max'
temperature_property 'average'
property 'weight'
end
end
end
end
14 changes: 13 additions & 1 deletion lib/open_weather/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def delete(path, options = {})

private

#
# @param [Symbol] method - Faraday HTTP method.
# @param [String] path - URL to send.
# @param [Hash] options - :appid, :lang, :units, :endpoint, :body keys will configure the request.
# The rest will be converted to query params for GET/DELETE, or jsonified for POST/PUT.
#
# @return [Object] - the Faraday::Response#body.
#
def request(method, path, options)
options = options.dup
options[:appid] ||= api_key if api_key.present?
Expand All @@ -34,7 +42,11 @@ def request(method, path, options)
when :post, :put
request.path = path
request.params = { appid: options.delete(:appid) }
request.body = options.to_json unless options.empty?
if options.key?(:body)
request.body = options.delete(:body).to_json
elsif !options.empty?
request.body = options.to_json
end
end
request.options.merge!(options.delete(:request)) if options.key?(:request)
end
Expand Down

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

39 changes: 39 additions & 0 deletions spec/fixtures/open_weather/stations/create_measurement_success.yml

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

43 changes: 43 additions & 0 deletions spec/fixtures/open_weather/stations/get_measurement_success.yml

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

Loading

0 comments on commit dbbd6d7

Please sign in to comment.