Class: Pinch::APIHelper
- Inherits:
-
Object
- Object
- Pinch::APIHelper
- Defined in:
- lib/pinch/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.
-
.form_encode(obj, instance_name) ⇒ Hash
Form encodes an object.
-
.form_encode_parameters(form_parameters) ⇒ Hash
Form encodes a hash of parameters.
Class Method Details
.append_url_with_query_parameters(query_builder, parameters) ⇒ Object
Appends the given set of parameters to the given query string
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pinch/api_helper.rb', line 38 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? query_builder else # remove any nil values parameters = parameters.reject { |_key, value| value.nil? } # does the query string already has parameters has_params = query_builder.include? '?' separator = has_params ? '&' : '?' # append query with separator and parameters query_builder << separator << URI.encode_www_form(parameters) end end |
.append_url_with_template_parameters(query_builder, parameters) ⇒ Object
Replaces template parameters in the given url
8 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 |
# File 'lib/pinch/api_helper.rb', line 8 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? query_builder else # iterate and append parameters parameters.each 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 end query_builder end |
.clean_url(url) ⇒ String
Validates and processes the given Url
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pinch/api_helper.rb', line 61 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(%r{^(https?:\/\/[^\/]+)}) raise ArgumentError, 'Invalid Url format.' if matches.nil? # get the http protocol match protocol = matches[1] # check if parameters exist index = url.index('?') # remove redundant forward slashes query = url[protocol.length...(index != nil ? index : url.length)] query.gsub!(%r{\/\/+}, '/') # get the parameters parameters = index != nil ? url[url.index('?')...url.length] : "" # return processed url protocol + query + parameters end |
.form_encode(obj, instance_name) ⇒ Hash
Form encodes an object.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pinch/api_helper.rb', line 101 def self.form_encode(obj, instance_name) retval = Hash.new # If this is a structure, resolve it's field names. if obj.respond_to? :key_map obj = obj.key_map end # Create a form encoded hash for this object. if obj == nil nil elsif obj.is_a? Array obj.each_with_index do |value, index| retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]")) end elsif obj.is_a? Hash obj.each do |key, value| retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]")) end else retval[instance_name] = obj end retval end |
.form_encode_parameters(form_parameters) ⇒ Hash
Form encodes a hash of parameters.
89 90 91 92 93 94 95 |
# File 'lib/pinch/api_helper.rb', line 89 def self.form_encode_parameters(form_parameters) encoded = Hash.new form_parameters.each do |key, value| encoded.merge!(APIHelper.form_encode value, key) end encoded end |