Skip to content

Commit

Permalink
Fix link delegation returning nil for field with value false (#128)
Browse files Browse the repository at this point in the history
* Fix link delegation returning nil for field with value false

Whenever a resource includes a field that is `false`, trying to access
it through a `Link` returned `nil`, rather than `false`.

This is because `Link` confused the `false` response with the resource
not actually handling the value, and then tried to use the delegation as
if a collection method was being called on the resource.

Fixes #126

* Update CHANGELOG
  • Loading branch information
ivoanjo authored and dblock committed Aug 26, 2017
1 parent 49bff2d commit 7e7dad2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-12-21 20:00:37 +0000 using RuboCop version 0.42.0.
# on 2017-08-26 18:01:43 +0100 using RuboCop version 0.42.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -9,9 +9,9 @@
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 109
Max: 110

# Offense count: 89
# Offense count: 97
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [#122](https://github.com/codegram/hyperclient/pull/122): Improve error message when server returns invalid data - [@ivoanjo](https://github.com/ivoanjo).
* [#125](https://github.com/codegram/hyperclient/pull/125): Add table of contents to readme and add note asking users to add their projects to the wiki - [@ivoanjo](https://github.com/ivoanjo).
* [#127](https://github.com/codegram/hyperclient/pull/127): Minor fixes: Fix warnings, and pry-byebug to dev Gemfile and tweak rubocop execution - [@ivoanjo](https://github.com/ivoanjo).
* [#128](https://github.com/codegram/hyperclient/pull/128): Fix link delegation returning nil for field with value false - [@ivoanjo](https://github.com/ivoanjo).
* Your contribution here.

### 0.8.5 (July 5, 2017)
Expand Down
3 changes: 2 additions & 1 deletion lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def to_s
# Internal: Delegate the method further down the API if the resource cannot serve it.
def method_missing(method, *args, &block)
if _resource.respond_to?(method.to_s)
_resource.send(method, *args, &block) || delegate_method(method, *args, &block)
result = _resource.send(method, *args, &block)
result.nil? ? delegate_method(method, *args, &block) : result
else
super
end
Expand Down
10 changes: 10 additions & 0 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ module Hyperclient
resource.orders.first.id.must_equal 1
end

it 'can handle false values in the response' do
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/orders') { [200, {}, { 'any' => false }] }
end

resource.orders.any.must_equal false
end

it "doesn't delegate when link key doesn't match" do
resource = Resource.new({ '_links' => { 'foos' => { 'href' => '/orders' } } }, entry_point)

Expand Down

0 comments on commit 7e7dad2

Please sign in to comment.