From db82cb117c95152213ef5b5161f737c7e2c9b9c8 Mon Sep 17 00:00:00 2001 From: Josh Cohen Date: Fri, 11 Mar 2016 22:33:08 -0500 Subject: [PATCH 1/2] curies: don't expand when the rel is already absolute (starts with http[s]:) --- lib/hyperclient/curie.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/hyperclient/curie.rb b/lib/hyperclient/curie.rb index 9afa295..6238bc7 100644 --- a/lib/hyperclient/curie.rb +++ b/lib/hyperclient/curie.rb @@ -40,8 +40,9 @@ def inspect # # rel - The String rel to expand. # - # Returns a new expanded url. + # Returns a new expanded url unless the url already starts with http[s]: def expand(rel) + return rel if /^http[s]?:/i.match(rel) return rel unless rel && templated? href.gsub('{rel}', rel) if href end From b0f2ed96bd37a8bac5da0112f62e718ec5574890 Mon Sep 17 00:00:00 2001 From: Josh Cohen Date: Sat, 12 Mar 2016 15:51:18 -0500 Subject: [PATCH 2/2] Allow the caller to set custom query parameters when fetching a link events_link=api._links['events'] events_link.query_params= { filter: "modified_date gt '2016-01-01'" } events=events_link['items'] --- lib/hyperclient/link.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 45a16f7..091bfbd 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -6,6 +6,7 @@ module Hyperclient # Internal: The Link is used to let a Resource interact with the API. # class Link + attr_accessor :query_params # Public: Initializes a new Link. # # key - The key or name of the link. @@ -18,6 +19,7 @@ def initialize(key, link, entry_point, uri_variables = nil) @link = link @entry_point = entry_point @uri_variables = uri_variables + @query_params={} end # Public: Indicates if the link is an URITemplate or a regular URI. @@ -165,10 +167,24 @@ def _uri_template @uri_template ||= URITemplate.new(@link['href']) end + + def add_query_params(old_url, new_params) + new_query_array= new_params.keys.map { |qi| [qi,new_params[qi]]} + uri=URI(old_url) + query_array=URI.decode_www_form(uri.query || []).concat(new_query_array) + uri.query=URI.encode_www_form(query_array) + return uri.to_s + end + def http_method(method, body = nil) + unless @query_params.empty? + new_url=add_query_params(_url, @query_params) + else + new_url=_url + end @resource = begin response = Futuroscope::Future.new do - @entry_point.connection.run_request(method, _url, body, nil) + @entry_point.connection.run_request(method, new_url, body, nil) end Resource.new(response.body, @entry_point, response) end