From 7e7dad25dbe6eace10ef13f0136705f190e6145b Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Sat, 26 Aug 2017 21:18:17 +0100 Subject: [PATCH] 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 --- .rubocop_todo.yml | 6 +++--- CHANGELOG.md | 1 + lib/hyperclient/link.rb | 3 ++- test/hyperclient/link_test.rb | 10 ++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5279cc9..7e49575 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index e7962df..2c4754c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 1fd1169..cc5b635 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -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 diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 823efa6..20e8872 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -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)