Sha256: 2ed661f496538d89862c8d3c798f38df86851cebc7a9106909563e6055d3c8bf

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

# A simple module to encode hashes of param keys and values as expected by
# Rack in its nested queries parsing.
#
module ThreeScale
  class Client
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
3scale_client-2.11.0 lib/3scale/client/rack_query.rb