Sha256: cffe532f120b98220d226e9865a2f11ee2736c96e61f51692dd7c886b713264e

Contents?: true

Size: 987 Bytes

Versions: 2

Compression:

Stored size: 987 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

2 entries across 2 versions & 1 rubygems

Version Path
redbooth-ruby-0.0.5 lib/redbooth-ruby/request/helpers.rb
redbooth-ruby-0.0.4 lib/redbooth-ruby/request/helpers.rb