Class: OutsideIn::Resource::QueryParams
- Inherits:
-
Object
- Object
- OutsideIn::Resource::QueryParams
- Defined in:
- lib/outside_in/resource/query_params.rb
Overview
A helper class for computing query parameters. These helpers know how to convert input values into query parameters and to add them to query URLs.
Instance Method Summary collapse
-
#initialize(simple = {}, negatable = {}) ⇒ OutsideIn::QueryParams
constructor
Creates and returns an instance based on various parameter names.
-
#parameterize(url, inputs) ⇒ String
Returns the provided URL with parameters attached to the query string.
Constructor Details
#initialize(simple = {}, negatable = {}) ⇒ OutsideIn::QueryParams
Creates and returns an instance based on various parameter names.
For each simple parameter name, a parameter with that name is added when the inputs hash contains a value for that key.
Negatable parameters are those which have “include” and “exclude” variants (e.g. category for locations, keyword for stories). An “include” parameter is added when the inputs hash contains a value for the negatable parameter’s name, just like for a simple parameter. An exclude parameter prefixed with “no-” is added when the inputs hash contains a value for the parameter’s name prefixed with “no-” or the name prefixed with “wo-” (the form used by the Thor tasks, which reserve the “no-” prefix for a different purpose).
24 25 26 27 |
# File 'lib/outside_in/resource/query_params.rb', line 24 def initialize(simple = {}, negatable = {}) @simple = simple @negatable = negatable end |
Instance Method Details
#parameterize(url, inputs) ⇒ String
Returns the provided URL with parameters attached to the query string.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/outside_in/resource/query_params.rb', line 35 def parameterize(url, inputs) params = [] @simple.each_pair do |input, api| nk = input.to_s params << "#{api}=#{inputs[nk]}" unless inputs[nk].nil? end @negatable.each_pair do |input, api| nk = input.to_s params.concat(inputs[nk].map {|s| "#{api}=#{URI.escape(s)}"}) unless inputs[nk].nil? ["wo-#{input}", "no-#{input}"].each do |nk| params.concat(inputs[nk].map {|s| "no-#{api}=#{URI.escape(s)}"}) unless inputs[nk].nil? end end if params.empty? url else sep = url =~ /\?/ ? '&' : '?' "#{url}#{sep}#{params.join('&')}" end end |