Skip to content

Commit 7e7dad2

Browse files
ivoanjodblock
authored andcommitted
Fix link delegation returning nil for field with value false (#128)
* 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
1 parent 49bff2d commit 7e7dad2

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

.rubocop_todo.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2016-12-21 20:00:37 +0000 using RuboCop version 0.42.0.
3+
# on 2017-08-26 18:01:43 +0100 using RuboCop version 0.42.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -9,9 +9,9 @@
99
# Offense count: 1
1010
# Configuration parameters: CountComments.
1111
Metrics/ClassLength:
12-
Max: 109
12+
Max: 110
1313

14-
# Offense count: 89
14+
# Offense count: 97
1515
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
1616
# URISchemes: http, https
1717
Metrics/LineLength:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [#122](https://github.com/codegram/hyperclient/pull/122): Improve error message when server returns invalid data - [@ivoanjo](https://github.com/ivoanjo).
44
* [#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).
55
* [#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).
6+
* [#128](https://github.com/codegram/hyperclient/pull/128): Fix link delegation returning nil for field with value false - [@ivoanjo](https://github.com/ivoanjo).
67
* Your contribution here.
78

89
### 0.8.5 (July 5, 2017)

lib/hyperclient/link.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ def to_s
126126
# Internal: Delegate the method further down the API if the resource cannot serve it.
127127
def method_missing(method, *args, &block)
128128
if _resource.respond_to?(method.to_s)
129-
_resource.send(method, *args, &block) || delegate_method(method, *args, &block)
129+
result = _resource.send(method, *args, &block)
130+
result.nil? ? delegate_method(method, *args, &block) : result
130131
else
131132
super
132133
end

test/hyperclient/link_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ module Hyperclient
479479
resource.orders.first.id.must_equal 1
480480
end
481481

482+
it 'can handle false values in the response' do
483+
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
484+
485+
stub_request(entry_point.connection) do |stub|
486+
stub.get('http://api.example.org/orders') { [200, {}, { 'any' => false }] }
487+
end
488+
489+
resource.orders.any.must_equal false
490+
end
491+
482492
it "doesn't delegate when link key doesn't match" do
483493
resource = Resource.new({ '_links' => { 'foos' => { 'href' => '/orders' } } }, entry_point)
484494

0 commit comments

Comments
 (0)