-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remaining stations endpoints #20
Changes from all commits
a90f274
de1bc07
2914c24
05c6695
536c430
0c91fb6
63463b2
bdbd91b
f9cfb45
6940096
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dblock what would you suggest to return here? Currently it the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
end | ||
|
||
private | ||
|
||
def validate_id(id) | ||
raise ArgumentError, 'Invalid ID' unless id&.is_a?(String) | ||
end | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -42,4 +42,56 @@ | |
) | ||
end | ||
end | ||
|
||
describe '#get_station' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I’ve been following this convention http://www.betterspecs.org/#describe (describe for the method under test, context for a scenario) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been schooled ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They’re aliases so all good either way! |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why, but it seems like the API requires
external_id
to be specified 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I could potentially raise an error if
id
was not defined, do you have a suggestion on how to define / where to keep errors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a parameter is missing, raise ArgumentError, generally I don't specialize errors unless they can happen on all APIs (e.g. incorrect HTTP proxy)