Module: SynapseClient::Util
- Defined in:
- lib/synapse_client/util.rb
Class Method Summary collapse
- .flatten_params(params, parent_key = nil) ⇒ Object
- .flatten_params_array(value, calculated_key) ⇒ Object
- .symbolize_names(object) ⇒ Object
- .url_encode(key) ⇒ Object
Class Method Details
.flatten_params(params, parent_key = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/synapse_client/util.rb', line 9 def self.flatten_params(params, parent_key=nil) result = [] params.each do |key, value| calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key) if value.is_a?(Hash) result += flatten_params(value, calculated_key) elsif value.is_a?(Array) result += flatten_params_array(value, calculated_key) else result << [calculated_key, value] end end result end |
.flatten_params_array(value, calculated_key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/synapse_client/util.rb', line 24 def self.flatten_params_array(value, calculated_key) result = [] value.each do |elem| if elem.is_a?(Hash) result += flatten_params(elem, calculated_key) elsif elem.is_a?(Array) result += flatten_params_array(elem, calculated_key) else result << ["#{calculated_key}[]", elem] end end result end |
.symbolize_names(object) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/synapse_client/util.rb', line 38 def self.symbolize_names(object) case object when Hash new_hash = Map.new object.each do |key, value| key = (key.to_sym rescue key) || key new_hash[key] = symbolize_names(value) end new_hash when Array object.map { |value| symbolize_names(value) } else object end end |
.url_encode(key) ⇒ Object
5 6 7 |
# File 'lib/synapse_client/util.rb', line 5 def self.url_encode(key) URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |