lib/hyperclient/link.rb in hyperclient-0.2.0 vs lib/hyperclient/link.rb in hyperclient-0.3.0

- old
+ new

@@ -1,18 +1,12 @@ -require 'hyperclient/http' require 'hyperclient/resource' require 'uri_template' module Hyperclient # Internal: The Link is used to let a Resource interact with the API. # class Link - extend Forwardable - # Public: Delegate all HTTP methods (get, post, put, delete, options and - # head) to the http connection. - def_delegators :http, :get, :post, :put, :delete, :options, :head - # Public: Initializes a new Link. # # link - The String with the URI of the link. # entry_point - The EntryPoint object to inject the cofnigutation. # uri_variables - The optional Hash with the variables to expand the link @@ -21,19 +15,14 @@ @link = link @entry_point = entry_point @uri_variables = uri_variables end - # Public: Returns the Resource which the Link is pointing to. - def resource - @resource ||=Resource.new(http.get, @entry_point) - end - # Public: Indicates if the link is an URITemplate or a regular URI. # # Returns true if it is templated. - # Returns false if it nos templated. + # Returns false if it not templated. def templated? !!@link['templated'] end # Public: Expands the Link when is templated with the given variables. @@ -54,15 +43,51 @@ raise MissingURITemplateVariablesException if @uri_variables == nil @url ||= URITemplate.new(@link['href']).expand(@uri_variables) end - private - # Internal: Returns the HTTP client used to interact with the API. - def http - @http ||= HTTP.new(url, @entry_point.config) + # Public: Returns the Resource which the Link is pointing to. + def resource + @resource ||=Resource.new(get.body, @entry_point) end + def connection + @entry_point.connection + end + + def get + connection.get(url) + end + + def options + connection.run_request(:options, url, nil, nil) + end + + def head + connection.head(url) + end + + def delete + connection.delete(url) + end + + def post(params) + connection.post(url, params) + end + + def put(params) + connection.put(url, params) + end + + def patch(params) + connection.patch(url, params) + end + + def inspect + "#<#{self.class.name} #{@link}>" + end + + private # Internal: Delegate the method to the API if it exists. # # This allows `api.links.posts.embedded` instead of # `api.links.posts.resource.embedded` def method_missing(method, *args, &block)