Skip to content

Commit

Permalink
Add remaining stations endpoints (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek authored Jun 3, 2020
1 parent 0a7168f commit 335f812
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 5 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +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).
* [#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
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o
- [Stations](#stations)
- [Register a Station](#register-a-station)
- [List Stations](#list-stations)
- [Get Station](#get-station)
- [Update Station](#update-station)
- [Delete Station](#delete-station)
- [Configuration](#configuration)
- [Units](#units)
- [Converting Temperature](#converting-temperature)
Expand Down Expand Up @@ -217,9 +220,37 @@ model.id # => '5ed2118acca8ce0001f1aeg1'

#### List Stations

To list all stations, you can call the client method:
To list all stations, call the client method:
```ruby
data = client.list_stations # => Array[OpenWeather::Models::Station]
client.list_stations # => Array[OpenWeather::Models::Station]
```

#### Get Station

To get a station, call the client method:
```ruby
client.get_station('5ed2118acca8ce0001f1aeg1') # => OpenWeather::Models::Station
```

#### Update Station

To update a station, call the client method:
```ruby
client.update_station('5ed2118acca8ce0001f1aeg1', external_id: 'SF_TEST002') # => OpenWeather::Models::Station
```
Alternatively, call `update!` on an instance of `Station`:
```ruby
model = OpenWeather::Models::Station.new(external_id: 'SF_TEST001', ...)
model.register!
model.update!(external_id: 'SF_TEST002')
model.external_id # => 'SF_TEST002'
```

#### Delete Station

To delete a station, call the client method:
```ruby
data = client.delete_station('5ed2118acca8ce0001f1aeg1') # => nil
```

## Configuration
Expand Down
25 changes: 25 additions & 0 deletions lib/open_weather/endpoints/stations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ def register_station(options = {})
def list_stations
get('stations').map { |data| OpenWeather::Models::Station.new(data) }
end

def get_station(id)
validate_id(id)

OpenWeather::Models::Station.new(get("stations/#{id}"))
end

def update_station(id, options = {})
validate_id(id)

OpenWeather::Models::Station.new(put("stations/#{id}", options))
end

def delete_station(id)
validate_id(id)

delete("stations/#{id}")
nil
end

private

def validate_id(id)
raise ArgumentError, 'Invalid ID' unless id&.is_a?(String)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/open_weather/models/station.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ def register!

self
end

def update!(attributes)
data = OpenWeather::Client.new.update_station(id, attributes)
update_attributes!(data)

self
end

def delete!
OpenWeather::Client.new.delete_station(id)
end
end
end
end
40 changes: 40 additions & 0 deletions spec/fixtures/open_weather/stations/delete_station_success.yml

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

44 changes: 44 additions & 0 deletions spec/fixtures/open_weather/stations/get_station_success.yml

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

44 changes: 44 additions & 0 deletions spec/fixtures/open_weather/stations/update_station_success.yml

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

38 changes: 38 additions & 0 deletions spec/lib/open_weather/models/station_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,42 @@
expect(result).to have_attributes(create_attributes.merge(id: '5ed21a12cca8ce0001f1aef1'))
end
end

describe '.update!' do
let(:update_attributes) do
{
external_id: 'SF_TEST002',
name: 'San Francisco Test Station 2'
}
end

before do
allow(OpenWeather::Client).to receive(:new).and_return(client)
end

it 'registers a station via the Client', vcr: { cassette_name: 'stations/update_station_success' } do
model = OpenWeather::Models::Station.new(id: '5ed21311cca8ce0001f1aef0')
result = model.update!(update_attributes)
expect(result.object_id).to eq(model.object_id)
expect(result).to have_attributes(update_attributes)
end
end

describe '.delete!' do
before do
allow(OpenWeather::Client).to receive(:new).and_return(client)
end

it 'deletes a station via the Client', vcr: { cassette_name: 'stations/delete_station_success' } do
id = '5ed21311cca8ce0001f1aef0'
expect(client)
.to receive(:delete_station)
.with(id)
.and_call_original

model = OpenWeather::Models::Station.new(id: id)
result = model.delete!
expect(result).to be_nil
end
end
end
54 changes: 53 additions & 1 deletion spec/open_weather/endpoints/stations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end

describe '#list_stations' do
it 'registers a station', vcr: { cassette_name: 'stations/list_stations_success' } do
it 'retrieves all stations', vcr: { cassette_name: 'stations/list_stations_success' } do
data = client.list_stations
expect(data).to be_a(Array)
expect(data.size).to eq(2)
Expand All @@ -42,4 +42,56 @@
)
end
end

describe '#get_station' do
it 'retrieves a station', vcr: { cassette_name: 'stations/get_station_success' } do
data = client.get_station('5ed21311cca8ce0001f1aef0')
expect(data).to be_a(OpenWeather::Models::Station)
expect(data).to have_attributes(
id: '5ed21311cca8ce0001f1aef0',
external_id: 'SF_TEST001',
name: 'San Francisco Test Station',
latitude: 37.76,
longitude: -122.43,
altitude: 150
)
end

context 'with invalid id' do
it 'raises error' do
expect { client.delete_station(nil) }.to raise_error ArgumentError
end
end
end

describe '#update_station' do
it 'updates a station', vcr: { cassette_name: 'stations/update_station_success' } do
update_attributes = {
external_id: 'SF_TEST002',
name: 'San Francisco Test Station 2'
}
data = client.update_station('5ed21311cca8ce0001f1aef0', update_attributes)
expect(data).to be_a(OpenWeather::Models::Station)
expect(data).to have_attributes(update_attributes)
end

context 'with invalid id' do
it 'raises error' do
expect { client.update_station(nil, {}) }.to raise_error ArgumentError
end
end
end

describe '#delete_station' do
it 'deletes a station', vcr: { cassette_name: 'stations/delete_station_success' } do
data = client.delete_station('5ed21311cca8ce0001f1aef0')
expect(data).to be_nil
end

context 'with invalid id' do
it 'raises error' do
expect { client.delete_station(nil) }.to raise_error ArgumentError
end
end
end
end

0 comments on commit 335f812

Please sign in to comment.