diff --git a/CHANGELOG.md b/CHANGELOG.md index 6277433..5930bf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 0.2.1 (Next) * Your contribution here. +* [#19](https://github.com/dblock/open-weather-ruby-client/pull/19): Add list_stations endpoint - [@wasabigeek](https://github.com/wasabigeek). * [#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) diff --git a/README.md b/README.md index 7e6ba7f..be73bc6 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o - [Historical Weather](#historical-weather) - [Stations](#stations) - [Register a Station](#register-a-station) + - [List Stations](#list-stations) - [Configuration](#configuration) - [Units](#units) - [Converting Temperature](#converting-temperature) @@ -214,6 +215,13 @@ model.register! model.id # => '5ed2118acca8ce0001f1aeg1' ``` +#### List Stations + +To list all stations, you can call the client method: +```ruby +data = client.list_stations # => Array[OpenWeather::Models::Station] +``` + ## Configuration You can configure client options, globally. diff --git a/lib/open_weather/endpoints/stations.rb b/lib/open_weather/endpoints/stations.rb index c121150..32744c6 100644 --- a/lib/open_weather/endpoints/stations.rb +++ b/lib/open_weather/endpoints/stations.rb @@ -6,6 +6,10 @@ module Stations def register_station(options = {}) OpenWeather::Models::Station.new(post('stations', options)) end + + def list_stations + get('stations').map { |data| OpenWeather::Models::Station.new(data) } + end end end end diff --git a/spec/fixtures/open_weather/stations/list_stations_success.yml b/spec/fixtures/open_weather/stations/list_stations_success.yml new file mode 100644 index 0000000..18ff3bd --- /dev/null +++ b/spec/fixtures/open_weather/stations/list_stations_success.yml @@ -0,0 +1,45 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.openweathermap.org/data/3.0/stations?appid=api-key + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - application/json; charset=utf-8 + Content-Type: + - application/json + User-Agent: + - OpenWeather Ruby Client/0.2.1 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Server: + - openresty + Date: + - Mon, 01 Jun 2020 14:47:28 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '1190' + 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":"5ed21311cca8ce0001f1aef0","created_at":"2020-05-30T08:02:25.3Z","updated_at":"2020-05-30T08:02:25.3Z","external_id":"SF_TEST001","name":"San + Francisco Test Station","longitude":-122.43,"latitude":37.76,"altitude":150,"rank":10},{"id":"5ed21a12cca8ce0001f1aef1","created_at":"2020-05-30T08:32:18.956Z","updated_at":"2020-05-30T08:32:18.956Z","external_id":"SF_TEST001","name":"San + Francisco Test Station","longitude":-122.43,"latitude":37.76,"altitude":150,"rank":10}]' + recorded_at: Mon, 01 Jun 2020 14:47:28 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/open_weather/endpoints/stations_spec.rb b/spec/open_weather/endpoints/stations_spec.rb index 67c5bed..8ec4db8 100644 --- a/spec/open_weather/endpoints/stations_spec.rb +++ b/spec/open_weather/endpoints/stations_spec.rb @@ -2,25 +2,44 @@ require 'spec_helper' -RSpec.describe 'stations' do +RSpec.describe OpenWeather::Endpoints::Stations do include_context 'API client', endpoint: 'https://api.openweathermap.org/data/3.0' - it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do - data = client.register_station( - external_id: 'SF_TEST001', - name: 'San Francisco Test Station', - latitude: 37.76, - longitude: -122.43, - altitude: 150 - ) - 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, - longitude: -122.43, - altitude: 150 - ) + describe '#register_station' do + it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do + data = client.register_station( + external_id: 'SF_TEST001', + name: 'San Francisco Test Station', + latitude: 37.76, + longitude: -122.43, + altitude: 150 + ) + 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, + longitude: -122.43, + altitude: 150 + ) + end + end + + describe '#list_stations' do + it 'registers a station', vcr: { cassette_name: 'stations/list_stations_success' } do + data = client.list_stations + expect(data).to be_a(Array) + expect(data.size).to eq(2) + expect(data.first).to be_a(OpenWeather::Models::Station) + expect(data.first).to have_attributes( + id: '5ed21311cca8ce0001f1aef0', + external_id: 'SF_TEST001', + name: 'San Francisco Test Station', + latitude: 37.76, + longitude: -122.43, + altitude: 150 + ) + end end end