From a41369f68d4f06545adfff6a8ddfa29bafd49be0 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Fri, 14 Jun 2013 12:47:54 -0400 Subject: [PATCH 1/2] Expose link attributes http://tools.ietf.org/html/draft-kelly-json-hal-05#section-5 --- lib/hyperclient/link.rb | 6 ++++-- test/hyperclient/link_test.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 0bb08fa..faafce8 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -92,7 +92,9 @@ def inspect # This allows `api.links.posts.embedded` instead of # `api.links.posts.resource.embedded` def method_missing(method, *args, &block) - if resource.respond_to?(method) + if @link.key?(method.to_s) + @link[method.to_s] + elsif resource.respond_to?(method) resource.send(method, *args, &block) else super @@ -102,7 +104,7 @@ def method_missing(method, *args, &block) # Internal: Accessory method to allow the link respond to the # methods that will hit method_missing. def respond_to_missing?(method, include_private = false) - resource.respond_to?(method.to_s) + @link.key?(method.to_s) || resource.respond_to?(method.to_s) end end diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 0167617..8c5e307 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -8,6 +8,15 @@ module Hyperclient EntryPoint.new('http://api.example.org/') end + %w(type deprecation name profile title hreflang).each do |attr| + describe attr do + it "returns the #{attr} attribute" do + link = Link.new({attr => 'value'}, entry_point) + link.send(attr).must_equal 'value' + end + end + end + describe 'templated?' do it 'returns true if the link is templated' do link = Link.new({'templated' => true}, entry_point) From afcc1cfdda4102cb650ba797d35dd2fa52bca923 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Fri, 14 Jun 2013 13:08:39 -0400 Subject: [PATCH 2/2] Define methods for HAL properties Method missing causes unpredictable results if properties are not defined. --- lib/hyperclient/link.rb | 36 +++++++++++++++++++++++++++++++---- test/hyperclient/link_test.rb | 15 ++++++++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index faafce8..b32bbdb 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -45,6 +45,36 @@ def url @url ||= URITemplate.new(@link['href']).expand(@uri_variables) end + # Public: Returns the type property of the Link + def type + @link['type'] + end + + # Public: Returns the name property of the Link + def name + @link['name'] + end + + # Public: Returns the deprecation property of the Link + def deprecation + @link['deprecation'] + end + + # Public: Returns the profile property of the Link + def profile + @link['profile'] + end + + # Public: Returns the title property of the Link + def title + @link['title'] + end + + # Public: Returns the hreflang property of the Link + def hreflang + @link['hreflang'] + end + # Public: Returns the Resource which the Link is pointing to. def resource @resource ||=Resource.new(get.body, @entry_point) @@ -92,9 +122,7 @@ def inspect # This allows `api.links.posts.embedded` instead of # `api.links.posts.resource.embedded` def method_missing(method, *args, &block) - if @link.key?(method.to_s) - @link[method.to_s] - elsif resource.respond_to?(method) + if resource.respond_to?(method) resource.send(method, *args, &block) else super @@ -104,7 +132,7 @@ def method_missing(method, *args, &block) # Internal: Accessory method to allow the link respond to the # methods that will hit method_missing. def respond_to_missing?(method, include_private = false) - @link.key?(method.to_s) || resource.respond_to?(method.to_s) + resource.respond_to?(method.to_s) end end diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 8c5e307..4ef0ba8 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -8,11 +8,16 @@ module Hyperclient EntryPoint.new('http://api.example.org/') end - %w(type deprecation name profile title hreflang).each do |attr| - describe attr do - it "returns the #{attr} attribute" do - link = Link.new({attr => 'value'}, entry_point) - link.send(attr).must_equal 'value' + %w(type deprecation name profile title hreflang).each do |prop| + describe prop do + it "returns the property value" do + link = Link.new({prop => 'value'}, entry_point) + link.send(prop).must_equal 'value' + end + + it 'returns nil if the property is not present' do + link = Link.new({}, entry_point) + link.send(prop).must_equal nil end end end