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_json_formatting, #fix_regexp, #remove_unicode

Constructor Details

#initialize(attributes = {}) ⇒ Interaction

Returns a new instance of Interaction.



15
16
17
18
19
20
# File 'lib/pact/consumer_contract/interaction.rb', line 15

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#provider_stateObject

Returns the value of attribute provider_state.



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

def provider_state
  @provider_state
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.from_hash(hash) ⇒ Object



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

def self.from_hash hash
  request_hash = Pact::MatchingRules.merge(hash['request'], hash['request']['matchingRules'])
  request = Pact::Request::Expected.from_hash(request_hash)
  response_hash = Pact::MatchingRules.merge(hash['response'], hash['response']['matchingRules'])
  response = Pact::Response.from_hash(response_hash)
  new(symbolize_keys(hash).merge(request: request, response: response))
end

Instance Method Details

#==(other) ⇒ Object



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

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

#description_with_provider_state_quotedObject



64
65
66
# File 'lib/pact/consumer_contract/interaction.rb', line 64

def description_with_provider_state_quoted
  provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
end

#eq?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eq? other
  self == other
end

#match_criterion(target, criterion) ⇒ Object



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

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

#matches_criteria?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#request_modifies_resource_without_checking_response_body?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pact/consumer_contract/interaction.rb', line 68

def request_modifies_resource_without_checking_response_body?
  request.modifies_resource? && response.body_allows_any_value?
end

#to_hashObject



30
31
32
33
34
35
36
37
# File 'lib/pact/consumer_contract/interaction.rb', line 30

def to_hash
  {
    description: description,
    provider_state: provider_state,
    request: request.to_hash,
    response: response.to_hash
  }
end

#to_sObject



72
73
74
# File 'lib/pact/consumer_contract/interaction.rb', line 72

def to_s
  to_hash.to_s
end

#validate!Object



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

def validate!
  raise Pact::InvalidInteractionError.new(self) unless description && request && response
end