Sha256: 0d48f0c85b54c97f6f6fe668cb122025a477ea5d42667d6dccf7046b364ab207

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

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

module Pact
  class QueryHash

    include ActiveSupportSupport
    include SymbolizeKeys

    attr_reader :original_string

    def initialize(query, original_string = nil)
      @hash = query.nil? ? query : convert_to_hash_of_arrays(query)
      @original_string = original_string
    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(convert_to_hash_of_arrays(Query.parse_string(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

2 entries across 2 versions & 1 rubygems

Version Path
pact-support-1.16.6 lib/pact/consumer_contract/query_hash.rb
pact-support-1.16.5 lib/pact/consumer_contract/query_hash.rb