Class: Pact::ConsumerContract

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupportSupport

#fix_all_the_things, #fix_regexp

Methods included from FileName

#file_name, #filenamify

Methods included from Logging

included, #logger

Methods included from SymbolizeKeys

included, #symbolize_keys

Constructor Details

#initialize(attributes = {}) ⇒ ConsumerContract

Returns a new instance of ConsumerContract.



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

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

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



58
59
60
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 58

def consumer
  @consumer
end

#interactionsObject

Returns the value of attribute interactions.



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

def interactions
  @interactions
end

#providerObject

Returns the value of attribute provider.



59
60
61
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 59

def provider
  @provider
end

Class Method Details

.from_hash(hash) ⇒ Object



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

def self.from_hash(hash)
  hash = symbolize_keys(hash)
  new({
    :interactions => hash[:interactions].collect { |hash| Interaction.from_hash(hash)},
    :consumer => ServiceConsumer.from_hash(hash[:consumer]),
    :provider => ServiceProvider.from_hash(hash[:provider])
  })
end

.from_json(string) ⇒ Object



97
98
99
100
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 97

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



102
103
104
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 102

def self.from_uri uri, options = {}
  from_json(Pact::PactFile.read(uri, options))
end

.maintain_backwards_compatiblity_with_producer_keys(string) ⇒ Object



106
107
108
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 106

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

Instance Method Details

#as_json(options = {}) ⇒ Object



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

def as_json(options = {})
  fix_all_the_things to_hash
end

#eachObject



124
125
126
127
128
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 124

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

#find_interaction(criteria) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 110

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

#find_interactions(criteria) ⇒ Object



120
121
122
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 120

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

#pact_file_nameObject



130
131
132
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 130

def pact_file_name
  file_name consumer.name, provider.name
end

#pactfile_pathObject



134
135
136
137
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 134

def pactfile_path
  raise 'You must first specify a consumer and service name' unless (consumer && consumer.name && provider && provider.name)
  @pactfile_path ||= File.join(Pact.configuration.pact_dir, pact_file_name)
end

#to_hashObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 67

def to_hash
  {
    provider: @provider.as_json,
    consumer: @consumer.as_json,
    interactions: @interactions.collect(&:as_json),
    metadata: {
      pact_gem: {
        version: Pact::VERSION
      }
    }
  }
end

#to_json(options = {}) ⇒ Object



84
85
86
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 84

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

#update_pactfileObject



139
140
141
142
143
144
# File 'lib/pact/consumer_contract/consumer_contract.rb', line 139

def update_pactfile
  logger.debug "Updating pact file for #{provider.name} at #{pactfile_path}"
  File.open(pactfile_path, 'w') do |f|
    f.write JSON.pretty_generate(self)
  end
end