Module: Tickethub::Helpers
Constant Summary collapse
- DEFAULT_SEP =
Stolen from Rack:
/[&;] */n
- ESCAPE_RE =
/[^a-zA-Z0-9 .~_-]/
Instance Method Summary collapse
- #camelize(value) ⇒ Object
- #deep_merge(hash, other_hash) ⇒ Object
- #escape(s) ⇒ Object
- #from_param(qs, d = nil) ⇒ Object
- #to_param(value, prefix = nil) ⇒ Object
- #to_path(*params) ⇒ Object
- #to_url_param(value, prefix = nil) ⇒ Object
- #unescape(s, encoding = nil) ⇒ Object
- #uri_escape(s) ⇒ Object
Instance Method Details
#camelize(value) ⇒ Object
9 10 11 |
# File 'lib/tickethub/helpers.rb', line 9 def camelize(value) value.to_s.split('_').map {|w| w.capitalize }.join end |
#deep_merge(hash, other_hash) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/tickethub/helpers.rb', line 13 def deep_merge(hash, other_hash) hash.merge(other_hash) do |key, oldval, newval| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) newval = newval.to_hash if newval.respond_to?(:to_hash) oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? deep_merge(oldval, newval) : newval end end |
#escape(s) ⇒ Object
77 78 79 |
# File 'lib/tickethub/helpers.rb', line 77 def escape(s) URI.encode_www_form_component(s.to_s) end |
#from_param(qs, d = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/tickethub/helpers.rb', line 57 def from_param(param) Rack::Utils.parse_nested_query(param) (value || '').split('&').each do |res| key, value = res.split('=') @params[key] = value end end |
#to_param(value, prefix = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tickethub/helpers.rb', line 25 def to_param(value, prefix = nil) case value when Array value.map { |v| to_param(v, "#{prefix}[]") }.join("&") when Hash value.map { |k, v| to_param(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) }.join("&") else raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{escape(value)}" end end |
#to_path(*params) ⇒ Object
5 6 7 |
# File 'lib/tickethub/helpers.rb', line 5 def to_path(*params) params.map(&:to_s).reject(&:empty?) * '/' end |
#to_url_param(value, prefix = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tickethub/helpers.rb', line 41 def to_url_param(value, prefix = nil) case value when Array value.map { |v| to_url_param(v, "#{prefix}[]") }.join("&") when Hash value.map { |k, v| to_url_param(v, prefix ? "#{prefix}[#{uri_escape(k)}]" : uri_escape(k)) }.join("&") else raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{uri_escape(value)}" end end |
#unescape(s, encoding = nil) ⇒ Object
90 91 92 |
# File 'lib/tickethub/helpers.rb', line 90 def unescape(s, encoding = Encoding::UTF_8) URI.decode_www_form_component(s, encoding) end |
#uri_escape(s) ⇒ Object
83 84 85 86 87 |
# File 'lib/tickethub/helpers.rb', line 83 def uri_escape(s) s.to_s.gsub(ESCAPE_RE) {|match| '%' + match.unpack('H2' * match.bytesize).join('%').upcase }.tr(' ', '+') end |