Class: Warbler::APIHelper
- Inherits:
-
Object
- Object
- Warbler::APIHelper
- Defined in:
- lib/warbler/api_helper.rb
Class Method Summary collapse
-
.append_url_with_query_parameters(query_builder, parameters) ⇒ Object
Appends the given set of parameters to the given query string.
-
.append_url_with_template_parameters(query_builder, parameters) ⇒ Object
Replaces template parameters in the given url.
-
.clean_url(url) ⇒ String
Validates and processes the given Url.
Class Method Details
.append_url_with_query_parameters(query_builder, parameters) ⇒ Object
Appends the given set of parameters to the given query string
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/warbler/api_helper.rb', line 40 def self.append_url_with_query_parameters(query_builder, parameters) # perform parameter validation raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.is_a? String # return if there are no parameters to replace if parameters.nil? then abort('no parameters to append') end #remove any nil values parameters = parameters.reject {|key, value| value.nil?} # does the query string already has parameters has_params = query_builder.include? '?' separator = if has_params then '&' else '?' end # append query with separator and parameters query_builder << separator << URI.unescape(URI.encode_www_form(parameters)) end |
.append_url_with_template_parameters(query_builder, parameters) ⇒ Object
Replaces template parameters in the given url
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/warbler/api_helper.rb', line 9 def self.append_url_with_template_parameters(query_builder, parameters) # perform parameter validation raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.is_a? String # return if there are no parameters to replace if parameters.nil? then abort('no parameters to append') end # iterate and append parameters parameters.map do |key, value| replace_value = '' if value.nil? replace_value = '' elsif value.is_a? Enumerable replace_value = value.join("/") else replace_value = value.to_s end # find the template parameter and replace it with its value query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value) end query_builder end |
.clean_url(url) ⇒ String
Validates and processes the given Url
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/warbler/api_helper.rb', line 63 def self.clean_url(url) # perform parameter validation raise ArgumentError, 'Invalid Url.' unless url.is_a? String # ensure that the urls are absolute matches = url.match(/^(https?:\/\/[^\/]+)/) raise ArgumentError, 'Invalid Url format.' if matches.nil? # get the http protocol match protocol = matches[1] # remove redundant forward slashes query = url[protocol.length..-1].gsub(/\/\/+/, '/') # return process url return protocol + query; end |