Module: Love::ResourceURI
- Included in:
- Client
- Defined in:
- lib/love.rb
Overview
Module to work with Tender REST URIs.
Instance Method Summary collapse
-
#append_query(base_uri, added_params = {}) ⇒ URI
Appends GET parameters to a URI instance.
-
#collection_uri(input) ⇒ URI
Returns a collection URI, based on an URI instance, a complete URI string or just a resource name.
-
#singleton_uri(input, kind) ⇒ URI
Returns a resource URI, based on an URI instance, a complete URI string or just a resource ID.
Instance Method Details
#append_query(base_uri, added_params = {}) ⇒ URI
Appends GET parameters to a URI instance. Duplicate parameters will be replaced with the new value.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/love.rb', line 107 def append_query(base_uri, added_params = {}) base_params = base_uri.query ? CGI.parse(base_uri.query) : {} get_params = base_params.merge(added_params.stringify_keys) base_uri.dup.tap do |uri| assignments = get_params.map do |k, v| case v when Array; v.map { |val| "#{::CGI.escape(k.to_s)}=#{::CGI.escape(val.to_s)}" }.join('&') else "#{::CGI.escape(k.to_s)}=#{::CGI.escape(v.to_s)}" end end uri.query = assignments.join('&') end end |
#collection_uri(input) ⇒ URI
Returns a collection URI, based on an URI instance, a complete URI string or just a resource name.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/love.rb', line 75 def collection_uri(input) case input.to_s when /^[\w-]+$/ ::URI.parse("https://api.tenderapp.com/#{site}/#{input}") when %r[^https?://api\.tenderapp\.com/#{site}/[\w-]+] ::URI.parse(input.to_s) else raise Love::Exception, "This does not appear to be a valid Tender category URI!" end end |
#singleton_uri(input, kind) ⇒ URI
Returns a resource URI, based on an URI instance, a complete URI string or just a resource ID.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/love.rb', line 91 def singleton_uri(input, kind) case input.to_s when /^\d+/ ::URI.parse("https://api.tenderapp.com/#{site}/#{kind}/#{input}") when %r[^https?://api\.tenderapp\.com/#{site}/#{kind}/\d+] ::URI.parse(input.to_s) else raise Love::Exception, "This does not appear to be a Tender #{kind} URI or ID!" end end |