Class: Pact::ConsumerContract

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

Constant Summary

Constants included from PactFile

PactFile::OPEN_TIMEOUT, PactFile::READ_TIMEOUT, PactFile::RETRY_LIMIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PactFile

#read, #render_pact, #save_pactfile_to_tmp

Methods included from Logging

included, #logger

Methods included from SymbolizeKeys

included, #symbolize_keys

Constructor Details

#initialize(attributes = {}) ⇒ ConsumerContract

Returns a new instance of ConsumerContract.



29
30
31
32
33
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 29

def initialize(attributes = {})
  @interactions = attributes[:interactions] || []
  @consumer = attributes[:consumer]
  @provider = attributes[:provider]
end

Instance Attribute Details

#consumerObject



26
27
28
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 26

def consumer
  @consumer
end

#interactionsObject



25
26
27
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 25

def interactions
  @interactions
end

#providerObject



27
28
29
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 27

def provider
  @provider
end

Class Method Details

.add_parser(consumer_contract_parser) ⇒ Object



35
36
37
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 35

def self.add_parser consumer_contract_parser
  parsers.unshift(consumer_contract_parser)
end

.from_hash(hash) ⇒ Object



43
44
45
46
47
48
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 43

def self.from_hash(hash)
  parsers.each do | parser |
    return parser.call(hash) if parser.can_parse?(hash)
  end
  raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact format: #{hash}")
end

.from_json(string) ⇒ Object



50
51
52
53
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 50

def self.from_json string
  deserialised_object = JSON.load(maintain_backwards_compatiblity_with_producer_keys(string))
  from_hash(deserialised_object)
end

.from_uri(uri, options = {}) ⇒ Object



55
56
57
58
59
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 55

def self.from_uri uri, options = {}
  from_json(Pact::PactFile.read(uri, options))
rescue UnrecognizePactFormatError
  raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact format. Please check that #{uri} is a valid pact file.")
end

.maintain_backwards_compatiblity_with_producer_keys(string) ⇒ Object



61
62
63
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 61

def self.maintain_backwards_compatiblity_with_producer_keys string
  string.gsub('"producer":', '"provider":').gsub('"producer_state":', '"provider_state":') if string
end

.parsersObject



39
40
41
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 39

def self.parsers
  @parsers ||= [Pact::HttpConsumerContractParser.new]
end

Instance Method Details

#eachObject



79
80
81
82
83
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 79

def each
  interactions.each do | interaction |
    yield interaction
  end
end

#find_interaction(criteria) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 65

def find_interaction criteria
  interactions = find_interactions criteria
  if interactions.size == 0
    raise Pact::Error.new("Could not find interaction matching #{criteria} in pact file between #{consumer.name} and #{provider.name}.")
  elsif interactions.size > 1
    raise Pact::Error.new("Found more than 1 interaction matching #{criteria} in pact file between #{consumer.name} and #{provider.name}.")
  end
  interactions.first
end

#find_interactions(criteria) ⇒ Object



75
76
77
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 75

def find_interactions criteria
  interactions.select{ | interaction| interaction.matches_criteria?(criteria)}
end

#writable_interactionsObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 85

def writable_interactions
  interactions.reject do |interaction|
    # For the sake of backwards compatibility, only reject interactions where 
    # write_to_pact is explicitly set to false
    interaction.respond_to?(:metadata) && 
      !interaction..nil? && 
      interaction..key?(:write_to_pact) &&
      interaction.[:write_to_pact] == false
  end
end