Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Custom query params #99

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/hyperclient/curie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down