Sha256: 928bd74f13bef241b08a024ddef03d5ff25fee7fe5eb149b28dfbc8420d1e3c1

Contents?: true

Size: 1.4 KB

Versions: 27

Compression:

Stored size: 1.4 KB

Contents

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

module Pact
  class QueryHash

    include ActiveSupportSupport
    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)
      require 'pact/matchers' # avoid recursive loop between this file, pact/reification and pact/matchers
      Pact::Matchers.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

    def to_hash
      @hash
    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) }
      elsif Pact::ArrayLike === v
        hash[k.to_sym] = v
      else
        hash[k.to_sym] = [*v]
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
pact-support-1.16.3 lib/pact/consumer_contract/query_hash.rb
pact-support-1.16.2 lib/pact/consumer_contract/query_hash.rb
pact-support-1.17.0.pre.rc2 lib/pact/consumer_contract/query_hash.rb
pact-support-1.17.0.pre.rc1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.16.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.16.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.5 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.4 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.3 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.2 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.15.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.14.3 lib/pact/consumer_contract/query_hash.rb
pact-support-1.14.2 lib/pact/consumer_contract/query_hash.rb
pact-support-1.14.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.14.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.13.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.12.1 lib/pact/consumer_contract/query_hash.rb
pact-support-1.12.0 lib/pact/consumer_contract/query_hash.rb
pact-support-1.11.0 lib/pact/consumer_contract/query_hash.rb