From 21c6d00ec29ff6f2f06ac81e8a7bd0a42300405b Mon Sep 17 00:00:00 2001 From: Oriol Gual Date: Sun, 26 Apr 2015 13:38:32 +0200 Subject: [PATCH 1/2] Improve documentation and style. --- lib/hyperclient/collection.rb | 13 +++++++++---- lib/hyperclient/curie.rb | 10 +++++----- lib/hyperclient/entry_point.rb | 18 +++++++++--------- lib/hyperclient/link.rb | 2 +- lib/hyperclient/link_collection.rb | 12 +++++++----- lib/hyperclient/resource_collection.rb | 2 +- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/hyperclient/collection.rb b/lib/hyperclient/collection.rb index c0873ab..b3f1c7c 100644 --- a/lib/hyperclient/collection.rb +++ b/lib/hyperclient/collection.rb @@ -1,5 +1,5 @@ module Hyperclient - # Public: A helper class to wrapp a collection of elements and provide + # Public: A helper class to wrap a collection of elements and provide # Hash-like access or via a method call. # # Examples @@ -25,8 +25,13 @@ def each(&block) @collection.each(&block) end - def include?(obj) - @collection.include?(obj) + # Public: Checks if this collection includes a given key. + # + # key - A String or Symbol to check for existance. + # + # Returns True/False. + def include?(key) + @collection.include?(key) end # Public: Returns a value from the collection for the given key. @@ -51,7 +56,7 @@ def [](name) @collection[name.to_s] end - # Public: Returns the wrapped collection as a hash. + # Public: Returns the wrapped collection as a Hash. # # Returns a Hash. def to_h diff --git a/lib/hyperclient/curie.rb b/lib/hyperclient/curie.rb index bc0e4cb..9afa295 100644 --- a/lib/hyperclient/curie.rb +++ b/lib/hyperclient/curie.rb @@ -7,8 +7,8 @@ module Hyperclient class Curie # Public: Initializes a new Curie. # - # curie - The String with the URI of the curie. - # entry_point - The EntryPoint object to inject the cofnigutation. + # curie_hash - The String with the URI of the curie. + # entry_point - The EntryPoint object to inject the configuration. def initialize(curie_hash, entry_point) @curie_hash = curie_hash @entry_point = entry_point @@ -22,12 +22,12 @@ def templated? !!@curie_hash['templated'] end - # Public: Returns the name property of the Curie + # Public: Returns the name property of the Curie. def name @curie_hash['name'] end - # Public: Returns the href property of the Curie + # Public: Returns the href property of the Curie. def href @curie_hash['href'] end @@ -38,7 +38,7 @@ def inspect # Public: Expands the Curie when is templated with the given variables. # - # rel - The rel to expand. + # rel - The String rel to expand. # # Returns a new expanded url. def expand(rel) diff --git a/lib/hyperclient/entry_point.rb b/lib/hyperclient/entry_point.rb index 6a1fd8b..b83f83d 100644 --- a/lib/hyperclient/entry_point.rb +++ b/lib/hyperclient/entry_point.rb @@ -43,9 +43,9 @@ def initialize(url, &_block) # Public: A Faraday connection to use as a HTTP client. # - # options - A Hash containing additional options. - # - # default - Set to true to reuse default Faraday connection options. + # options - A Hash containing additional options to pass to Farday. Use + # {default: false} if you want to skip using default Faraday options set by + # Hyperclient. # # Returns a Faraday::Connection. def connection(options = {}, &block) @@ -123,12 +123,12 @@ def faraday_block=(value) # # Returns a block. def default_faraday_block - lambda do |conn| - conn.use Faraday::Response::RaiseError - conn.use FaradayMiddleware::FollowRedirects - conn.request :hal_json - conn.response :hal_json, content_type: /\bjson$/ - conn.adapter :net_http + lambda do |connection| + connection.use Faraday::Response::RaiseError + connection.use FaradayMiddleware::FollowRedirects + connection.request :hal_json + connection.response :hal_json, content_type: /\bjson$/ + connection.adapter :net_http end end diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index ed1884f..6669342 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -10,7 +10,7 @@ class Link # # key - The key or name of the link. # link - The String with the URI of the link. - # entry_point - The EntryPoint object to inject the cofnigutation. + # entry_point - The EntryPoint object to inject the configuration. # uri_variables - The optional Hash with the variables to expand the link # if it is templated. def initialize(key, link, entry_point, uri_variables = nil) diff --git a/lib/hyperclient/link_collection.rb b/lib/hyperclient/link_collection.rb index d667742..cf98d67 100644 --- a/lib/hyperclient/link_collection.rb +++ b/lib/hyperclient/link_collection.rb @@ -13,8 +13,8 @@ module Hyperclient class LinkCollection < Collection # Public: Initializes a LinkCollection. # - # collection - The Hash with the links. - # curies - Link curies. + # collection - The Hash with the links. + # curies - The Hash with link curies. # entry_point - The EntryPoint object to inject the configuration. def initialize(collection, curies, entry_point) fail "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect) @@ -33,13 +33,15 @@ def initialize(collection, curies, entry_point) # Internal: Creates links from the response hash. # + # name - A String to identify the link's name. # link_or_links - A Hash or an Array of hashes with the links to build. - # entry_point - The EntryPoint object to inject the configuration. - # curies - Optional curies for templated links. + # curies - An Array of Curies for templated links. + # entry_point - The EntryPoint object to inject the configuration. # - # Returns a Link or an array of Links when given an Array. + # Returns a Link or an Array of Links when given an Array. def build_link(name, link_or_links, curies, entry_point) return unless link_or_links + if link_or_links.respond_to?(:to_ary) link_or_links.map do |link| build_link(name, link, curies, entry_point) diff --git a/lib/hyperclient/resource_collection.rb b/lib/hyperclient/resource_collection.rb index f93d2b8..f6f2b66 100644 --- a/lib/hyperclient/resource_collection.rb +++ b/lib/hyperclient/resource_collection.rb @@ -14,7 +14,7 @@ class ResourceCollection < Collection # Public: Initializes a ResourceCollection. # # collection - The Hash with the embedded resources. - # entry_point - The EntryPoint object to inject the cofnigutation. + # entry_point - The EntryPoint object to inject the configuration. # def initialize(collection, entry_point) @entry_point = entry_point From 753734ed35af23184ec892cbb6b9df7ed2be643f Mon Sep 17 00:00:00 2001 From: Oriol Gual Date: Sun, 26 Apr 2015 13:39:25 +0200 Subject: [PATCH 2/2] Remove duplication. --- lib/hyperclient/link.rb | 66 ++++++++++------------------------- test/hyperclient/link_test.rb | 6 ---- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 6669342..075b269 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -80,76 +80,37 @@ def _hreflang @link['hreflang'] end - # Public: Returns the Resource which the Link is pointing to. - def _get - @resource = begin - response = Futuroscope::Future.new do - _connection.get(_url) - end - Resource.new(response.body, @entry_point, response) - end - end - def _resource @resource || _get end - def _connection - @entry_point.connection + # Public: Returns the Resource which the Link is pointing to. + def _get + http_method(:get) end def _options - @resource = begin - response = Futuroscope::Future.new do - _connection.run_request(:options, _url, nil, nil) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:options) end def _head - @resource = begin - response = Futuroscope::Future.new do - _connection.head(_url) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:head) end def _delete - @resource = begin - response = Futuroscope::Future.new do - _connection.delete(_url) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:delete) end def _post(params = {}) - @resource = begin - response = Futuroscope::Future.new do - _connection.post(_url, params) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:post, params) end def _put(params = {}) - @resource = begin - response = Futuroscope::Future.new do - _connection.put(_url, params) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:put, params) end def _patch(params = {}) - @resource = begin - response = Futuroscope::Future.new do - _connection.patch(_url, params) - end - Resource.new(response.body, @entry_point, response) - end + http_method(:patch, params) end def inspect @@ -198,5 +159,14 @@ def to_ary def _uri_template @uri_template ||= URITemplate.new(@link['href']) end + + def http_method(method, body = nil) + @resource = begin + response = Futuroscope::Future.new do + @entry_point.connection.run_request(method, _url, body, nil) + end + Resource.new(response.body, @entry_point, response) + end + end end end diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 151defe..cd28f42 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -117,12 +117,6 @@ module Hyperclient end end - describe '_connection' do - it 'returns the entry point connection' do - Link.new('key', {}, entry_point)._connection.must_equal entry_point.connection - end - end - describe 'get' do it 'sends a GET request with the link url' do link = Link.new('key', { 'href' => '/productions/1' }, entry_point)