Module: Authy::URL::ClassMethods
- Defined in:
- lib/authy/url_helpers.rb
Instance Method Summary collapse
- #clean_uri_params(uri_params, params) ⇒ Object
- #escape_for_url(field) ⇒ Object
- #escape_params(params) ⇒ Object
-
#escape_query(query, namespace = nil) ⇒ Object
Copied and extended from httpclient’s HTTP::Message#escape_query().
- #eval_uri(uri, params = {}) ⇒ Object
- #keys_to_verify(uri, params) ⇒ Object
- #params_from_array(left, values) ⇒ Object
- #to_param(left, right) ⇒ Object
- #validate_for_url(names, to_validate = {}) ⇒ Object
Instance Method Details
#clean_uri_params(uri_params, params) ⇒ Object
12 13 14 |
# File 'lib/authy/url_helpers.rb', line 12 def clean_uri_params(uri_params, params) params.reject { |k, v| uri_params.include? k} end |
#escape_for_url(field) ⇒ Object
33 34 35 |
# File 'lib/authy/url_helpers.rb', line 33 def escape_for_url(field) URI.escape(field.to_s.strip, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |
#escape_params(params) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/authy/url_helpers.rb', line 55 def escape_params(params) params.each do |attr, value| if value.kind_of?(String) params[attr] = HTTP::Message.escape(value) elsif value.kind_of?(Hash) escape_params(value) end end end |
#escape_query(query, namespace = nil) ⇒ Object
Copied and extended from httpclient’s HTTP::Message#escape_query()
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/authy/url_helpers.rb', line 66 def escape_query(query, namespace = nil) # :nodoc: pairs = [] query.each do |attr, value| left = namespace ? "#{namespace}[#{attr.to_s}]" : attr.to_s if values = Array.try_convert(value) pairs += params_from_array(left, values) elsif values = Hash.try_convert(value) pairs.push(escape_query(values, left.dup)) else if value.respond_to?(:read) value = value.read end pairs.push(to_param(left, value)) end end pairs.join('&') end |
#eval_uri(uri, params = {}) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/authy/url_helpers.rb', line 16 def eval_uri(uri, params = {}) uri.gsub(/:\w+/) do |s| param_name = s.gsub(":", "") HTTP::Message.escape(params[param_name].to_s) end end |
#keys_to_verify(uri, params) ⇒ Object
8 9 10 |
# File 'lib/authy/url_helpers.rb', line 8 def keys_to_verify(uri, params) uri.scan(/:(\w+)/).flatten end |
#params_from_array(left, values) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/authy/url_helpers.rb', line 41 def params_from_array(left, values) values.map do |value| if value.respond_to?(:read) value = value.read end if value.kind_of?(Hash) escape_query(value, left+"[]") else to_param(left + '[]', value) end end end |
#to_param(left, right) ⇒ Object
37 38 39 |
# File 'lib/authy/url_helpers.rb', line 37 def to_param(left, right) HTTP::Message.escape(left) + '=' + HTTP::Message.escape(right.to_s) end |
#validate_for_url(names, to_validate = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/authy/url_helpers.rb', line 23 def validate_for_url(names, to_validate = {}) names.each do |name| value = to_validate[name] if value.nil? or value.to_s.empty? or value.to_s.split(" ").size == 0 return [ false, "#{name} is blank." ] end end [ true, ""] end |