Sha256: 6f4f8234a9e487a6a91ecf88efbba639a41a469e43db36300687df9d00dd8fae

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

require 'cgi'
require 'pact/shared/active_support_support'
require 'pact/matchers'
require 'pact/symbolize_keys'

module Pact
  class QueryHash

    include ActiveSupportSupport
    include Pact::Matchers
    include SymbolizeKeys

    def initialize(query)
      @hash = query.nil? ? query : convert_to_hash_of_arrays(query)
    end

    def as_json(opts = {})
      @hash
    end

    def to_json(opts = {})
      as_json(opts).to_json(opts)
    end

    def eql?(other)
      self == other
    end

    def ==(other)
      QueryHash === other && other.query == query
    end

    # other will always be a QueryString, not a QueryHash, as it will have ben created
    # from the actual query string.
    def difference(other)
      diff(query, symbolize_keys(CGI::parse(other.query)), allow_unexpected_keys: false)
    end

    def query
      @hash
    end

    def to_s
      @hash.inspect
    end

    def empty?
      @hash && @hash.empty?
    end

    private

    def convert_to_hash_of_arrays(query)
      query.each_with_object({}) {|(k, v), hash| insert(hash, k, v) }
    end

    def insert(hash, k, v)
      if Hash === v
        v.each {|k2, v2| insert(hash, :"#{k}[#{k2}]", v2) }
      else
        hash[k.to_sym] = [*v]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pact-support-1.1.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.1.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.0.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.0.0 lib/pact/consumer_contract/query_hash.rb
pact-support-0.6.1 lib/pact/consumer_contract/query_hash.rb