Class: Pact::HttpConsumerContractParser

Inherits:
Object
  • Object
show all
Includes:
SymbolizeKeys
Defined in:
lib/pact/consumer_contract/http_consumer_contract_parser.rb

Instance Method Summary collapse

Methods included from SymbolizeKeys

included, #symbolize_keys

Instance Method Details

#call(hash) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pact/consumer_contract/http_consumer_contract_parser.rb', line 7

def call(hash)
  hash = symbolize_keys(hash)
  v = pact_specification_version(hash)
  options = { pact_specification_version: v }

  if v.after? 3
    Pact.configuration.error_stream.puts "WARN: This code only knows how to parse v3 pacts, attempting to parse v#{options[:pact_specification_version]} pact using v3 code."
  end

  interactions = hash[:interactions].each_with_index.collect { |hash, index| Interaction.from_hash({ index: index }.merge(hash), options) }
  ConsumerContract.new(
    :consumer => ServiceConsumer.from_hash(hash[:consumer]),
    :provider => ServiceProvider.from_hash(hash[:provider]),
    :interactions => interactions
  )
end

#can_parse?(hash) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pact/consumer_contract/http_consumer_contract_parser.rb', line 33

def can_parse?(hash)
  hash.key?('interactions') || hash.key?(:interactions)
end

#pact_specification_version(hash) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pact/consumer_contract/http_consumer_contract_parser.rb', line 24

def pact_specification_version hash
  # TODO handle all 3 ways of defining this...
  # metadata.pactSpecificationVersion
  maybe_pact_specification_version_1 = hash[:metadata] && hash[:metadata]['pactSpecification'] && hash[:metadata]['pactSpecification']['version']
  maybe_pact_specification_version_2 = hash[:metadata] && hash[:metadata]['pactSpecificationVersion']
  pact_specification_version = maybe_pact_specification_version_1 || maybe_pact_specification_version_2
  pact_specification_version ? Pact::SpecificationVersion.new(pact_specification_version) : Pact::SpecificationVersion::NIL_VERSION
end