From defa749added4601013e8d4b8e1708412891c38f Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 15:04:43 +0800 Subject: [PATCH 01/22] Add register_stations placeholder and spec --- lib/open_weather/client.rb | 1 + lib/open_weather/endpoints.rb | 1 + lib/open_weather/endpoints/stations.rb | 10 ++++++++ spec/open_weather/endpoints/stations_spec.rb | 24 ++++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 lib/open_weather/endpoints/stations.rb create mode 100644 spec/open_weather/endpoints/stations_spec.rb diff --git a/lib/open_weather/client.rb b/lib/open_weather/client.rb index faddfaf..4058f97 100644 --- a/lib/open_weather/client.rb +++ b/lib/open_weather/client.rb @@ -6,6 +6,7 @@ class Client include Request include Endpoints::Current include Endpoints::OneCall + include Endpoints::Stations attr_accessor(*Config::ATTRIBUTES) diff --git a/lib/open_weather/endpoints.rb b/lib/open_weather/endpoints.rb index 4aef6fa..def9fcc 100644 --- a/lib/open_weather/endpoints.rb +++ b/lib/open_weather/endpoints.rb @@ -2,3 +2,4 @@ require_relative 'endpoints/current' require_relative 'endpoints/one_call' +require_relative 'endpoints/stations' diff --git a/lib/open_weather/endpoints/stations.rb b/lib/open_weather/endpoints/stations.rb new file mode 100644 index 0000000..a3a0a48 --- /dev/null +++ b/lib/open_weather/endpoints/stations.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module OpenWeather + module Endpoints + module Stations + def register_station(external_id:, name:, lat:, lon:, altitude:) + end + end + end +end diff --git a/spec/open_weather/endpoints/stations_spec.rb b/spec/open_weather/endpoints/stations_spec.rb new file mode 100644 index 0000000..77cd344 --- /dev/null +++ b/spec/open_weather/endpoints/stations_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'stations' do + include_context 'API client' + + it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do + data = client.register_station( + external_id: 'SF_TEST001', + name: 'San Francisco Test Station', + lat: 37.76, + lon: -122.43, + altitude: 150 + ) + expect(data).to have_attributes( + external_id: 'SF_TEST001', + name: 'San Francisco Test Station', + lat: 37.76, + lon: -122.43, + altitude: 150 + ) + end +end From ca684c5099e1e4da732e9b40ed2c066208b0a120 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 16:04:09 +0800 Subject: [PATCH 02/22] Add Station model --- lib/open_weather/endpoints/stations.rb | 3 ++- lib/open_weather/models.rb | 1 + lib/open_weather/models/station.rb | 17 +++++++++++++++++ spec/open_weather/endpoints/stations_spec.rb | 10 ++++++---- 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 lib/open_weather/models/station.rb diff --git a/lib/open_weather/endpoints/stations.rb b/lib/open_weather/endpoints/stations.rb index a3a0a48..c121150 100644 --- a/lib/open_weather/endpoints/stations.rb +++ b/lib/open_weather/endpoints/stations.rb @@ -3,7 +3,8 @@ module OpenWeather module Endpoints module Stations - def register_station(external_id:, name:, lat:, lon:, altitude:) + def register_station(options = {}) + OpenWeather::Models::Station.new(post('stations', options)) end end end diff --git a/lib/open_weather/models.rb b/lib/open_weather/models.rb index b495b2f..4b78fec 100644 --- a/lib/open_weather/models.rb +++ b/lib/open_weather/models.rb @@ -13,3 +13,4 @@ require_relative 'models/list' require_relative 'models/city' require_relative 'models/one_call' +require_relative 'models/station' diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb new file mode 100644 index 0000000..5ea6f4a --- /dev/null +++ b/lib/open_weather/models/station.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module OpenWeather + module Models + class Station < Model + property 'id' # internal identifier for the station + property 'external_id' # external identifier for the station + property 'name' # name of the station + property 'latitude' # geographical coordinates of the location (latitude) + property 'longitude' # geographical coordinates of the location (longitude) + property 'altitude' # height of station above sea level + property 'created_at' # timestamp when station was created + property 'updated_at' # timestamp when station was updated + property 'rank' # rank of station + end + end +end diff --git a/spec/open_weather/endpoints/stations_spec.rb b/spec/open_weather/endpoints/stations_spec.rb index 77cd344..f159cd1 100644 --- a/spec/open_weather/endpoints/stations_spec.rb +++ b/spec/open_weather/endpoints/stations_spec.rb @@ -6,18 +6,20 @@ include_context 'API client' it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do + client.endpoint = 'https://api.openweathermap.org/data/3.0' data = client.register_station( external_id: 'SF_TEST001', name: 'San Francisco Test Station', - lat: 37.76, - lon: -122.43, + latitude: 37.76, + longitude: -122.43, altitude: 150 ) + expect(data).to be_a(OpenWeather::Models::Station) expect(data).to have_attributes( external_id: 'SF_TEST001', name: 'San Francisco Test Station', - lat: 37.76, - lon: -122.43, + latitude: 37.76, + longitude: -122.43, altitude: 150 ) end From 71c09806565d103c564a332558fe4d3def17b687 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 16:38:24 +0800 Subject: [PATCH 03/22] Allow json request --- lib/open_weather/endpoints/stations.rb | 4 +- lib/open_weather/request.rb | 8 +++- .../stations/register_success.yml | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/open_weather/stations/register_success.yml diff --git a/lib/open_weather/endpoints/stations.rb b/lib/open_weather/endpoints/stations.rb index c121150..0d990d6 100644 --- a/lib/open_weather/endpoints/stations.rb +++ b/lib/open_weather/endpoints/stations.rb @@ -4,7 +4,9 @@ module OpenWeather module Endpoints module Stations def register_station(options = {}) - OpenWeather::Models::Station.new(post('stations', options)) + OpenWeather::Models::Station.new( + post('stations', options.merge(use_json: true)) + ) end end end diff --git a/lib/open_weather/request.rb b/lib/open_weather/request.rb index fc6ff83..6361e17 100644 --- a/lib/open_weather/request.rb +++ b/lib/open_weather/request.rb @@ -33,7 +33,13 @@ def request(method, path, options) request.url(path, options) when :post, :put request.path = path - request.body = options unless options.empty? + if options.delete(:use_json) + request.params = { appid: options.delete(:appid) } + request.headers['Content-Type'] = 'application/json' + request.body = options.to_json unless options.empty? + else + request.body = options unless options.empty? + end end request.options.merge!(options.delete(:request)) if options.key?(:request) end diff --git a/spec/fixtures/open_weather/stations/register_success.yml b/spec/fixtures/open_weather/stations/register_success.yml new file mode 100644 index 0000000..7795e87 --- /dev/null +++ b/spec/fixtures/open_weather/stations/register_success.yml @@ -0,0 +1,44 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.openweathermap.org/data/3.0/stations?appid=api-key + body: + encoding: UTF-8 + string: '{"external_id":"SF_TEST001","name":"San Francisco Test Station","latitude":37.76,"longitude":-122.43,"altitude":150}' + headers: + Accept: + - application/json; charset=utf-8 + User-Agent: + - OpenWeather Ruby Client/0.2.1 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 201 + message: Created + headers: + Server: + - openresty + Date: + - Sat, 30 May 2020 08:32:18 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '303' + Connection: + - keep-alive + Access-Control-Allow-Origin: + - "*" + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, PUT, DELETE + body: + encoding: UTF-8 + string: '{"ID":"5ed21a12cca8ce0001f1aef1","updated_at":"2020-05-30T08:32:18.956523908Z","created_at":"2020-05-30T08:32:18.956523832Z","user_id":"example_user_id","external_id":"SF_TEST001","name":"San + Francisco Test Station","latitude":37.76,"longitude":-122.43,"altitude":150,"rank":10,"source_type":5}' + recorded_at: Sat, 30 May 2020 08:32:19 GMT +recorded_with: VCR 6.0.0 From 675503db6c441dec6793454f9f04080fcf323603 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 20:52:16 +0800 Subject: [PATCH 04/22] Make all requests default to JSON --- lib/open_weather/connection.rb | 5 ++++- lib/open_weather/request.rb | 9 ++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/open_weather/connection.rb b/lib/open_weather/connection.rb index 59b044b..cdfaa80 100644 --- a/lib/open_weather/connection.rb +++ b/lib/open_weather/connection.rb @@ -11,7 +11,10 @@ def headers def connection @connection ||= begin options = { - headers: headers.merge('Accept' => 'application/json; charset=utf-8') + headers: headers.merge( + 'Accept' => 'application/json; charset=utf-8', + 'Content-Type' => 'application/json' + ) } options[:headers]['User-Agent'] = user_agent if user_agent diff --git a/lib/open_weather/request.rb b/lib/open_weather/request.rb index 6361e17..bb6f65c 100644 --- a/lib/open_weather/request.rb +++ b/lib/open_weather/request.rb @@ -33,13 +33,8 @@ def request(method, path, options) request.url(path, options) when :post, :put request.path = path - if options.delete(:use_json) - request.params = { appid: options.delete(:appid) } - request.headers['Content-Type'] = 'application/json' - request.body = options.to_json unless options.empty? - else - request.body = options unless options.empty? - end + request.params = { appid: options.delete(:appid) } + request.body = options.to_json unless options.empty? end request.options.merge!(options.delete(:request)) if options.key?(:request) end From 09239ee0918fc831c3656d35055a9f6c6f36c07f Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 20:53:46 +0800 Subject: [PATCH 05/22] Remove options from Stations#register_station --- lib/open_weather/endpoints/stations.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/open_weather/endpoints/stations.rb b/lib/open_weather/endpoints/stations.rb index 0d990d6..c121150 100644 --- a/lib/open_weather/endpoints/stations.rb +++ b/lib/open_weather/endpoints/stations.rb @@ -4,9 +4,7 @@ module OpenWeather module Endpoints module Stations def register_station(options = {}) - OpenWeather::Models::Station.new( - post('stations', options.merge(use_json: true)) - ) + OpenWeather::Models::Station.new(post('stations', options)) end end end From 86ecc7c2488059f99bcf255fcd7180b5d1e31698 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 21:38:49 +0800 Subject: [PATCH 06/22] Translate ID from API response --- lib/open_weather/models/station.rb | 3 ++- spec/open_weather/endpoints/stations_spec.rb | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index 5ea6f4a..039477f 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -3,7 +3,8 @@ module OpenWeather module Models class Station < Model - property 'id' # internal identifier for the station + # internal identifier for the station + property 'id', from: 'ID' property 'external_id' # external identifier for the station property 'name' # name of the station property 'latitude' # geographical coordinates of the location (latitude) diff --git a/spec/open_weather/endpoints/stations_spec.rb b/spec/open_weather/endpoints/stations_spec.rb index f159cd1..3deac59 100644 --- a/spec/open_weather/endpoints/stations_spec.rb +++ b/spec/open_weather/endpoints/stations_spec.rb @@ -16,6 +16,7 @@ ) expect(data).to be_a(OpenWeather::Models::Station) expect(data).to have_attributes( + id: '5ed21a12cca8ce0001f1aef1', external_id: 'SF_TEST001', name: 'San Francisco Test Station', latitude: 37.76, From e301134ecc6f03392e052066b0428cf899b5db74 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 21:39:07 +0800 Subject: [PATCH 07/22] Start on #create! method --- lib/open_weather/models/station.rb | 8 +++++++ spec/lib/open_weather/models/station_spec.rb | 24 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 spec/lib/open_weather/models/station_spec.rb diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index 039477f..a536125 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -13,6 +13,14 @@ class Station < Model property 'created_at' # timestamp when station was created property 'updated_at' # timestamp when station was updated property 'rank' # rank of station + + def initialize(args = nil, options = {}) + super args.transform_keys(&:to_s), options + end + + def create! + OpenWeather::Client.new.register_station(to_h) + end end end end diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb new file mode 100644 index 0000000..8a66d9a --- /dev/null +++ b/spec/lib/open_weather/models/station_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe OpenWeather::Models::Station do + describe '#create!' do + it 'registers a station via the Client' do + create_attributes = { + external_id: 'SF_TEST001', + name: 'San Francisco Test Station', + latitude: 37.76, + longitude: -122.43, + altitude: 150 + } + client = instance_double(OpenWeather::Client) + expect(OpenWeather::Client).to receive(:new).and_return(client) + expect(client) + .to receive(:register_station) + .with(create_attributes.transform_keys(&:to_s)) + + described_class.new(create_attributes).create! + end + end +end From ed74b9d8593b32ac2e19017da16e6bc3cf78f162 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 21:50:33 +0800 Subject: [PATCH 08/22] Update Station model after a create! --- lib/open_weather/models/station.rb | 3 ++- spec/lib/open_weather/models/station_spec.rb | 23 ++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index a536125..88418db 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -19,7 +19,8 @@ def initialize(args = nil, options = {}) end def create! - OpenWeather::Client.new.register_station(to_h) + data = OpenWeather::Client.new.register_station(to_h) + update_attributes!(data) end end end diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb index 8a66d9a..276714e 100644 --- a/spec/lib/open_weather/models/station_spec.rb +++ b/spec/lib/open_weather/models/station_spec.rb @@ -4,21 +4,36 @@ RSpec.describe OpenWeather::Models::Station do describe '#create!' do - it 'registers a station via the Client' do - create_attributes = { + let(:create_attributes) do + { external_id: 'SF_TEST001', name: 'San Francisco Test Station', latitude: 37.76, longitude: -122.43, altitude: 150 } - client = instance_double(OpenWeather::Client) - expect(OpenWeather::Client).to receive(:new).and_return(client) + end + let(:client) { instance_double(OpenWeather::Client) } + + before do + allow(OpenWeather::Client).to receive(:new).and_return(client) + end + + it 'registers a station via the Client' do expect(client) .to receive(:register_station) .with(create_attributes.transform_keys(&:to_s)) described_class.new(create_attributes).create! end + + it 'sets the internal id' do + allow(client) + .to receive(:register_station) + .and_return('ID' => 'internal_id') + model = described_class.new(create_attributes) + model.create! + expect(model.id).to eq('internal_id') + end end end From 339a5f1f7089aa5f24b57288d0d606bc5645a467 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 21:54:27 +0800 Subject: [PATCH 09/22] refactor: Move hash transformation to Model --- lib/open_weather/models/model.rb | 3 ++- lib/open_weather/models/station.rb | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/open_weather/models/model.rb b/lib/open_weather/models/model.rb index a25c59c..1693120 100644 --- a/lib/open_weather/models/model.rb +++ b/lib/open_weather/models/model.rb @@ -10,7 +10,8 @@ class Model < Hashie::Trash attr_reader :options def initialize(args = nil, options = {}) - super args + transformed_args = args.respond_to?(:transform_keys) ? args.transform_keys(&:to_s) : args + super transformed_args @options = { units: OpenWeather.config.units }.merge(options || {}) end diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index 88418db..ac1a9fb 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -14,10 +14,6 @@ class Station < Model property 'updated_at' # timestamp when station was updated property 'rank' # rank of station - def initialize(args = nil, options = {}) - super args.transform_keys(&:to_s), options - end - def create! data = OpenWeather::Client.new.register_station(to_h) update_attributes!(data) From 354dcad21f15dba64eb3bbeaf462bfabf15d5014 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 22:01:48 +0800 Subject: [PATCH 10/22] Rename method to register! --- lib/open_weather/models/station.rb | 2 +- spec/lib/open_weather/models/station_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index ac1a9fb..ee48e63 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -14,7 +14,7 @@ class Station < Model property 'updated_at' # timestamp when station was updated property 'rank' # rank of station - def create! + def register! data = OpenWeather::Client.new.register_station(to_h) update_attributes!(data) end diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb index 276714e..c59297b 100644 --- a/spec/lib/open_weather/models/station_spec.rb +++ b/spec/lib/open_weather/models/station_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' RSpec.describe OpenWeather::Models::Station do - describe '#create!' do + describe '#register!' do let(:create_attributes) do { external_id: 'SF_TEST001', @@ -24,7 +24,7 @@ .to receive(:register_station) .with(create_attributes.transform_keys(&:to_s)) - described_class.new(create_attributes).create! + described_class.new(create_attributes).register! end it 'sets the internal id' do @@ -32,7 +32,7 @@ .to receive(:register_station) .and_return('ID' => 'internal_id') model = described_class.new(create_attributes) - model.create! + model.register! expect(model.id).to eq('internal_id') end end From 9ea28cc262e53100fbd2842d61c6414f7b35b678 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 22:15:15 +0800 Subject: [PATCH 11/22] Make register! a class method instead --- lib/open_weather/models/station.rb | 5 ++--- spec/lib/open_weather/models/station_spec.rb | 22 ++++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index ee48e63..0c10fc6 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -14,9 +14,8 @@ class Station < Model property 'updated_at' # timestamp when station was updated property 'rank' # rank of station - def register! - data = OpenWeather::Client.new.register_station(to_h) - update_attributes!(data) + def self.register!(attributes) + OpenWeather::Client.new.register_station(attributes) end end end diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb index c59297b..bfe7133 100644 --- a/spec/lib/open_weather/models/station_spec.rb +++ b/spec/lib/open_weather/models/station_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' RSpec.describe OpenWeather::Models::Station do - describe '#register!' do + describe '.register!' do let(:create_attributes) do { external_id: 'SF_TEST001', @@ -13,7 +13,10 @@ altitude: 150 } end - let(:client) { instance_double(OpenWeather::Client) } + let(:client) do + instance_double(OpenWeather::Client, + register_station: described_class.new(create_attributes)) + end before do allow(OpenWeather::Client).to receive(:new).and_return(client) @@ -22,18 +25,15 @@ it 'registers a station via the Client' do expect(client) .to receive(:register_station) - .with(create_attributes.transform_keys(&:to_s)) + .with(create_attributes) - described_class.new(create_attributes).register! + described_class.register!(create_attributes) end - it 'sets the internal id' do - allow(client) - .to receive(:register_station) - .and_return('ID' => 'internal_id') - model = described_class.new(create_attributes) - model.register! - expect(model.id).to eq('internal_id') + it 'returns an instance of the Station' do + model = described_class.register!(create_attributes) + expect(model).to be_a(described_class) + expect(model).to have_attributes(create_attributes) end end end From a8f93a831604133bc2986efc15412aed959bcd95 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 22:16:35 +0800 Subject: [PATCH 12/22] Update README to include stations API --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 22a54f5..5fc2f16 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o - [One Call](#one-call) - [Current and Forecast Weather](#current-and-forecast-weather) - [Historical Weather](#historical-weather) + - [Stations](#stations) - [Configuration](#configuration) - [Units](#units) - [Converting Temperature](#converting-temperature) @@ -193,6 +194,25 @@ data.current # => OpenWeather::Models::OneCall::CurrentWeather data.hourly # => Array[OpenWeather::Models::OneCall::HourlyWeather] ``` +### Stations + +The [Stations API](https://openweathermap.org/stations) lets your manage personal weather stations and measurements. + +#### Register a Station + +To register a station, you can either call the client method: +```ruby +data = client.register_station(external_id: 'SF_TEST001', ...) # => OpenWeather::Models::Station +data.id # => '5ed2118acca8ce0001f1aeg1' +data.external_id # => 'SF_TEST001' +``` +Or and call the `register!` on the `Station` class: +```ruby +data = OpenWeather::Models::Station.register!(external_id: 'SF_TEST001', ...) +data.id # => '5ed2118acca8ce0001f1aeg1' +data.external_id # => 'SF_TEST001' +``` + ## Configuration You can configure client options, globally. From 6033cc922613ed431c562442e525282c62e00db3 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 22:18:27 +0800 Subject: [PATCH 13/22] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fb311d..6277433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 0.2.1 (Next) * Your contribution here. +* [#18](https://github.com/dblock/open-weather-ruby-client/pull/18): Add register_station endpoint - [@wasabigeek](https://github.com/wasabigeek). ### 0.2.0 (2020/05/17) From 1211ec44f021fc588bb9a745e0650198d5af382b Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Sat, 30 May 2020 22:51:32 +0800 Subject: [PATCH 14/22] doc: Update TOC in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5fc2f16..0cb1928 100644 --- a/README.md +++ b/README.md @@ -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) - [Stations](#stations) + - [Register a Station](#register-a-station) - [Configuration](#configuration) - [Units](#units) - [Converting Temperature](#converting-temperature) From 703771b70a840b912b4416b118ff7877bfbeb618 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Sun, 31 May 2020 20:33:21 +0800 Subject: [PATCH 15/22] Fix typo in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0cb1928..243800f 100644 --- a/README.md +++ b/README.md @@ -201,13 +201,13 @@ The [Stations API](https://openweathermap.org/stations) lets your manage persona #### Register a Station -To register a station, you can either call the client method: +To register a station, you can call the client method: ```ruby data = client.register_station(external_id: 'SF_TEST001', ...) # => OpenWeather::Models::Station data.id # => '5ed2118acca8ce0001f1aeg1' data.external_id # => 'SF_TEST001' ``` -Or and call the `register!` on the `Station` class: +Alternatively, call `register!` on the `Station` class: ```ruby data = OpenWeather::Models::Station.register!(external_id: 'SF_TEST001', ...) data.id # => '5ed2118acca8ce0001f1aeg1' From 7f7acc753d88631c9bdd3e17687bcd1a8166ce86 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 20:03:31 +0800 Subject: [PATCH 16/22] refactor: Let shared_context take params --- spec/support/shared/api_client.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/support/shared/api_client.rb b/spec/support/shared/api_client.rb index 643e863..7d45479 100644 --- a/spec/support/shared/api_client.rb +++ b/spec/support/shared/api_client.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true -RSpec.shared_context 'API client', shared_context: :metadata do +RSpec.shared_context 'API client', shared_context: :metadata do |params| before do OpenWeather::Config.reset + OpenWeather::Config.endpoint = params[:endpoint] if params&.key?(:endpoint) end let(:client) { OpenWeather::Client.new(api_key: ENV['OPEN_WEATHER_API_KEY'] || 'api-key') } end From d1ae475a57db6fadf90717c79cdc0dbd4ca50e8a Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 20:05:46 +0800 Subject: [PATCH 17/22] refactor: Use VCR in spec --- spec/lib/open_weather/models/station_spec.rb | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb index bfe7133..07b2e36 100644 --- a/spec/lib/open_weather/models/station_spec.rb +++ b/spec/lib/open_weather/models/station_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe OpenWeather::Models::Station do + include_context 'API client', endpoint: 'https://api.openweathermap.org/data/3.0' + describe '.register!' do let(:create_attributes) do { @@ -13,24 +15,12 @@ altitude: 150 } end - let(:client) do - instance_double(OpenWeather::Client, - register_station: described_class.new(create_attributes)) - end before do allow(OpenWeather::Client).to receive(:new).and_return(client) end - it 'registers a station via the Client' do - expect(client) - .to receive(:register_station) - .with(create_attributes) - - described_class.register!(create_attributes) - end - - it 'returns an instance of the Station' do + it 'registers a station via the Client', vcr: { cassette_name: 'stations/register_success' } do model = described_class.register!(create_attributes) expect(model).to be_a(described_class) expect(model).to have_attributes(create_attributes) From c1d0b8715b924c6fb73f357043089eb93c19d68b Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 20:09:33 +0800 Subject: [PATCH 18/22] feat: Make register! an instance method --- lib/open_weather/models/station.rb | 7 +++++-- spec/lib/open_weather/models/station_spec.rb | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index 0c10fc6..97cb394 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -14,8 +14,11 @@ class Station < Model property 'updated_at' # timestamp when station was updated property 'rank' # rank of station - def self.register!(attributes) - OpenWeather::Client.new.register_station(attributes) + def register! + data = OpenWeather::Client.new.register_station(to_h) + update_attributes!(data) + + self end end end diff --git a/spec/lib/open_weather/models/station_spec.rb b/spec/lib/open_weather/models/station_spec.rb index 07b2e36..167c95a 100644 --- a/spec/lib/open_weather/models/station_spec.rb +++ b/spec/lib/open_weather/models/station_spec.rb @@ -21,9 +21,10 @@ end it 'registers a station via the Client', vcr: { cassette_name: 'stations/register_success' } do - model = described_class.register!(create_attributes) - expect(model).to be_a(described_class) - expect(model).to have_attributes(create_attributes) + model = described_class.new(create_attributes) + result = model.register! + expect(result.object_id).to eq(model.object_id) + expect(result).to have_attributes(create_attributes.merge(id: '5ed21a12cca8ce0001f1aef1')) end end end From 728b116014901268a033720aab0b82fe76c43216 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 20:10:31 +0800 Subject: [PATCH 19/22] refactor: Use params for include_context --- spec/open_weather/endpoints/stations_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/open_weather/endpoints/stations_spec.rb b/spec/open_weather/endpoints/stations_spec.rb index 3deac59..67c5bed 100644 --- a/spec/open_weather/endpoints/stations_spec.rb +++ b/spec/open_weather/endpoints/stations_spec.rb @@ -3,10 +3,9 @@ require 'spec_helper' RSpec.describe 'stations' do - include_context 'API client' + include_context 'API client', endpoint: 'https://api.openweathermap.org/data/3.0' it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do - client.endpoint = 'https://api.openweathermap.org/data/3.0' data = client.register_station( external_id: 'SF_TEST001', name: 'San Francisco Test Station', From 14623af8c0cb59077a9fac87b469ff494ea4ff4c Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 20:46:44 +0800 Subject: [PATCH 20/22] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 243800f..e049bee 100644 --- a/README.md +++ b/README.md @@ -209,9 +209,9 @@ data.external_id # => 'SF_TEST001' ``` Alternatively, call `register!` on the `Station` class: ```ruby -data = OpenWeather::Models::Station.register!(external_id: 'SF_TEST001', ...) -data.id # => '5ed2118acca8ce0001f1aeg1' -data.external_id # => 'SF_TEST001' +model = OpenWeather::Models::Station.new(external_id: 'SF_TEST001', ...) +model.register! +model.id # => '5ed2118acca8ce0001f1aeg1' ``` ## Configuration From ba32947a6126da74314d704220bbbe341929c14c Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 21:08:13 +0800 Subject: [PATCH 21/22] Make comment more consistent --- lib/open_weather/models/station.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/open_weather/models/station.rb b/lib/open_weather/models/station.rb index 97cb394..4f3babc 100644 --- a/lib/open_weather/models/station.rb +++ b/lib/open_weather/models/station.rb @@ -3,8 +3,7 @@ module OpenWeather module Models class Station < Model - # internal identifier for the station - property 'id', from: 'ID' + property 'id', from: 'ID' # internal identifier for the station property 'external_id' # external identifier for the station property 'name' # name of the station property 'latitude' # geographical coordinates of the location (latitude) From 8e7e080fbfc8756295c99489fe36a7206e7094d3 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Mon, 1 Jun 2020 21:12:00 +0800 Subject: [PATCH 22/22] Fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e049bee..7e6ba7f 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ data = client.register_station(external_id: 'SF_TEST001', ...) # => OpenWeather: data.id # => '5ed2118acca8ce0001f1aeg1' data.external_id # => 'SF_TEST001' ``` -Alternatively, call `register!` on the `Station` class: +Alternatively, call `register!` on an instance of `Station`: ```ruby model = OpenWeather::Models::Station.new(external_id: 'SF_TEST001', ...) model.register!