Skip to content
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

Unexpected method was called when receive some JSON response #64

Open
ryz310 opened this issue Jun 6, 2019 · 0 comments
Open

Unexpected method was called when receive some JSON response #64

ryz310 opened this issue Jun 6, 2019 · 0 comments

Comments

@ryz310
Copy link

ryz310 commented Jun 6, 2019

Hi, I have a trouble. Please see below.

An API is expected to return following result as JSON.

{
  "age_range": {
    "max": 30,
    "min": 20
  }
}

A Sawyer::Agent instance returns result as Sawyer::Resource.

agent = Sawyer::Agent.new('https://example.com')
result = agent.call(:get, 'age_range')
result[:max] # => 30

However, if the API returns following result, above execution will unexpected result.

{
  "age_range": {
    "min": 20
  }
}
result = agent.call(:get, 'age_range')
result[:max] # => [:min, 20]

I think the cause is into the Sawyer::Resource implementation. Sawyer::Resouce#[] calls #method_missing and searches the key from inner fields. Ordinarily it will return nil if does not find the key.

However, :max is Enumerable module instance so it will happen that calls Enumerable#max method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant