lib/hyperclient/link.rb in hyperclient-0.7.0 vs lib/hyperclient/link.rb in hyperclient-0.7.1

- old
+ new

@@ -8,11 +8,11 @@ class Link # Public: Initializes a new 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) @key = key @link = link @@ -78,80 +78,41 @@ # Public: Returns the hreflang property of the Link 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 "#<#{self.class.name}(#{@key}) #{@link}>" end @@ -160,24 +121,29 @@ _url end private - # Internal: Delegate the method to the API if it exists. - # - # This allows `api.posts` instead of `api.links.posts.embedded` + # Internal: Delegate the method further down the API if the resource cannot serve it. def method_missing(method, *args, &block) - if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) && delegate.respond_to?(method.to_s) - # named.named becomes named - delegate.send(method, *args, &block) - elsif _resource.respond_to?(method.to_s) - _resource.send(method, *args, &block) + if _resource.respond_to?(method.to_s) + _resource.send(method, *args, &block) || delegate_method(method, *args, &block) else super end end + # Internal: Delegate the method to the API if the resource cannot serve it. + # + # This allows `api.posts` instead of `api._links.posts.embedded.posts` + def delegate_method(method, *args, &block) + return unless @key && _resource.respond_to?(@key) + @delegate ||= _resource.send(@key) + return unless @delegate && @delegate.respond_to?(method.to_s) + @delegate.send(method, *args, &block) + end + # Internal: Accessory method to allow the link respond to the # methods that will hit method_missing. def respond_to_missing?(method, _include_private = false) if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) && delegate.respond_to?(method.to_s) true @@ -195,8 +161,17 @@ end # Internal: Memoization for a URITemplate instance 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