lib/paid/util.rb in paid-1.0.1 vs lib/paid/util.rb in paid-1.0.2

- old
+ new

@@ -1,48 +1,8 @@ module Paid module Util - def self.query_string(params) - if params && params.any? - return query_array(params).join('&') - else - return "" - end - end - - # Three major use cases (and nesting of them needs to be supported): - # { :a => { :b => "bvalue" } } => ["a[b]=bvalue"] - # { :a => [1, 2] } => ["a[]=1", "a[]=2"] - # { :a => "value" } => ["a=value"] - def self.query_array(params, key_prefix=nil) - ret = [] - params.each do |key, value| - if params.is_a?(Array) - value = key - key = '' - end - key_suffix = escape(key) - full_key = key_prefix ? "#{key_prefix}[#{key_suffix}]" : key_suffix - - if value.is_a?(Hash) || value.is_a?(Array) - # Handles the following cases: - # { :a => { :b => "bvalue" } } => ["a[b]=bvalue"] - # { :a => [1, 2] } => ["a[]=1", "a[]=2"] - ret += query_array(value, full_key) - else - # Handles the base case with just key and value: - # { :a => "value" } => ["a=value"] - ret << "#{full_key}=#{escape(value)}" - end - end - ret - end - - def self.escape(val) - URI.escape(val.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) - end - def self.symbolize_keys(obj) if obj.is_a?(Hash) ret = {} obj.each do |key, value| ret[(key.to_sym rescue key) || key] = symbolize_keys(value) @@ -67,9 +27,23 @@ else begin json.dup rescue json + end + end + end + + def self.constantize(str, prefix=false) + str = str.to_s + begin + str.split('::').reduce(Module, :const_get) + rescue NameError => e + if prefix + raise e + else + p = "#{self.name}".split("::").first + constantize("#{p}::#{str}", true) end end end end