-
-
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 register stations #18
Conversation
Cool. Thanks!
To be consistent I think the client should have
Maybe, I wouldn't care enough. I would just call out that this library also now supports some 3.0 beta functions. If you use 2.5 endpoints for station it's going to give you an error anyway (write a test).
This seems unnecessary. I think 2.5 API never uses |
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.
The OO interface is not quite an OO interface, but almost there!
@@ -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 |
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.
Care to explain?
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.
A symbol-keyed hash was “ignored” by the model, I think because a Hashie::Dash property will account for whether it’s defined as a string or symbol (though there didn’t seem to be an issue retrieving the property via a method)
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.
it's possible that the model class needs a merge initializer instead. Either way make sure to have a spec for this and we can fix it later.
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.
Finding this hard to test on Model itself, would it be sufficient to test Station for now?
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.
Yes.
lib/open_weather/models/station.rb
Outdated
property 'updated_at' # timestamp when station was updated | ||
property 'rank' # rank of station | ||
|
||
def self.register!(attributes) |
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.
This becomes a method that can take additional options:
def register!(options = {})
OpenWeather::Client.new.register_station(attributes, options)
end
|
||
require 'spec_helper' | ||
|
||
RSpec.describe OpenWeather::Models::Station do |
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.
Probably shorter to use the same VCR here. The problem with the double is that it will continue working even if we break the API call.
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.
Curious, since that client method is unit-tested already, wouldn't it be sufficient to unit-test this as well (check that we call client with the right params)?
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.
It's sufficient, but implies behavior and IMHO hides bugs. I've debugged my fair share of double
's to avoid them, more often than to use them. That said no strong opinions.
include_context 'API client' | ||
|
||
it 'registers a station', vcr: { cassette_name: 'stations/register_success' } do | ||
client.endpoint = 'https://api.openweathermap.org/data/3.0' |
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.
Let's extend the context to take options like so:
include_context 'API client', endpoint: 'https://api.openweathermap.org/data/3.0'
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.
Does this work out-of-the-box? It seems like we can only pass a block https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-context#declare-a-shared-context,-include-it-with-%60include-context%60-and-extend-it-with-an-additional-block
Well, TIL
altitude: 150 | ||
) | ||
expect(data).to be_a(OpenWeather::Models::Station) | ||
expect(data).to have_attributes( |
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.
As long as have_attributes
starts failing if I add an extra attribute, this works, but it is inconsistent with other tests. Not sure if I care about the latter much, just thinking out loud.
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.
It does fail, but perhaps with a more cryptic error:
1) stations registers a station
Failure/Error:
expect(data).to have_attributes(
...
wazzoo: 'test'
)
expected {...} to respond to :wazzoo with 0 arguments
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.
So that's better, it lets us check all attributes. I wouldn't mind if all the other tests changed ;)
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.
So that's better, it lets us check all attributes. I wouldn't mind if all the other tests changed ;)
Can I open a separate PR for that 🤣
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.
Let's see the model change and I'm happy with this.
lib/open_weather/models/station.rb
Outdated
module OpenWeather | ||
module Models | ||
class Station < Model | ||
# internal identifier for the station |
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.
Be consistent, put the comment after the code line.
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.
My bad, I was trying to see if it could work with both 'id' and 'ID' and thought the line was too long
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.
You can probably alias it if you really want to.
before do | ||
OpenWeather::Config.reset | ||
OpenWeather::Config.endpoint = params[:endpoint] if params&.key?(:endpoint) |
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.
For a future PR, I'd like to make it future proof able to write OpenWeather::Client.configure(params)
here.
property 'rank' # rank of station | ||
|
||
def register! | ||
data = OpenWeather::Client.new.register_station(to_h) |
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.
For future update, we can save the extra instance construction with OpenWeather::Client.new.post('stations', options)
. I think that's OK to duplicate.
Good stuff, merged. Want to add the rest of station methods? |
Will slowly chip at them! |
Start support for stations (#10) by adding a
register_station
method to Client (https://openweathermap.org/stations#create_station).Opening for early feedback, some questions: