Class: Pact::Interaction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SymbolizeKeys

included, #symbolize_keys

Methods included from ActiveSupportSupport

#fix_all_the_things, #fix_regexp

Constructor Details

#initialize(attributes = {}) ⇒ Interaction

Returns a new instance of Interaction.



12
13
14
15
16
17
# File 'lib/pact/consumer_contract/interaction.rb', line 12

def initialize attributes = {}
  @description = attributes[:description]
  @request = attributes[:request]
  @response = attributes[:response]
  @provider_state = attributes[:provider_state]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/pact/consumer_contract/interaction.rb', line 10

def description
  @description
end

#provider_stateObject

Returns the value of attribute provider_state.



10
11
12
# File 'lib/pact/consumer_contract/interaction.rb', line 10

def provider_state
  @provider_state
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/pact/consumer_contract/interaction.rb', line 10

def request
  @request
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/pact/consumer_contract/interaction.rb', line 10

def response
  @response
end

Class Method Details

.from_hash(hash) ⇒ Object



19
20
21
22
# File 'lib/pact/consumer_contract/interaction.rb', line 19

def self.from_hash hash
  request = Pact::Request::Expected.from_hash(hash['request'])
  new(symbolize_keys(hash).merge({request: request}))
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  other.is_a?(Interaction) && as_json == other.as_json
end

#as_json(options = {}) ⇒ Object



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

def as_json options = {}
  fix_all_the_things to_hash
end

#eq?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eq? other
  self == other
end

#match_criterion(target, criterion) ⇒ Object



47
48
49
# File 'lib/pact/consumer_contract/interaction.rb', line 47

def match_criterion target, criterion
  target == criterion || (criterion.is_a?(Regexp) && criterion.match(target))
end

#matches_criteria?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/pact/consumer_contract/interaction.rb', line 38

def matches_criteria? criteria
  criteria.each do | key, value |
    unless match_criterion self.send(key.to_s), value
      return false
    end
  end
  true
end

#to_hashObject



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

def to_hash
  hash = { :description => @description }
  hash[:provider_state] = @provider_state if @provider_state #Easier to read when provider state at top
  hash.merge(:request => @request.as_json, :response => @response)
end

#to_json(options = {}) ⇒ Object



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

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

#to_sObject



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

def to_s
  JSON.pretty_generate(self)
end