Sha256: 4146e8d94e846453263374d4092c0e88b594f09f35a499f5059b86a376187718

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

# A simple module to encode hashes of param keys and values as expected by
# Rack in its nested queries parsing.
#
module RackQuery
  class << self
    def encode(hash)
      hash.flat_map do |hk, hv|
        encode_value(CGI.escape(hk.to_s), hv)
      end.join('&'.freeze)
    end

    private

    def encode_value(rack_param, val)
      if val.is_a? Array
        encode_array(rack_param, val)
      elsif val.is_a? Hash
        encode_hash(rack_param, val)
      else
        "#{rack_param}=#{CGI.escape(val.to_s)}"
      end
    end

    def encode_array(rack_param, val)
      rack_param = rack_param + '[]'
      val.flat_map do |v|
        encode_value(rack_param, v)
      end
    end

    def encode_hash(rack_param, val)
      val.flat_map do |k, v|
        encode_value(rack_param + "[#{CGI.escape(k.to_s)}]", v)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
3scale_client-2.10.0 lib/3scale/rack_query.rb