Sha256: 7b71dbe26a3b6d3a80fd525be64827733da7f85755c4cbda142eabae3945fa71

Contents?: true

Size: 990 Bytes

Versions: 8

Compression:

Stored size: 990 Bytes

Contents

module RedboothRuby
  module Request
    module Helpers
      def flatten_hash_keys(old_hash, new_hash = {}, keys = nil)
        old_hash.each do |key, value|
          key = key.to_s
          if value.is_a?(Hash)
            all_keys_formatted = keys + "[#{key}]"
            flatten_hash_keys(value, new_hash, all_keys_formatted)
          else
            new_hash[key] = value
          end
        end
        new_hash
      end

      def normalize_params(params, key = nil)
        params = flatten_hash_keys(params) if params.is_a?(Hash)
        result = {}
        params.each do |name, value|
          case value
          when Hash
            result[name.to_s] = normalize_params(value)
          when Array
            value.each_with_index do |item_value, index|
              result["#{ name.to_s }[#{ index }]"] = item_value.to_s
            end
          else
            result[name.to_s] = value.to_s
          end
        end
        result
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
redbooth-ruby-0.2.3 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.2.2 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.2.1 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.2.0 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.1.4 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.1.3 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.1.1 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.1.0 lib/redbooth-ruby/request/helpers.rb