Class: Pact::ConsumerContract::Message

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

Defined Under Namespace

Classes: Contents

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



18
19
20
21
22
23
24
# File 'lib/pact/consumer_contract/message.rb', line 18

def initialize attributes = {}
  @description = attributes[:description]
  @provider_state = attributes[:provider_state] || attributes[:providerState]
  @provider_states = attributes[:provider_states] || []
  @contents = attributes[:contents]
  @metadata = attributes[:metadata]
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



16
17
18
# File 'lib/pact/consumer_contract/message.rb', line 16

def contents
  @contents
end

#descriptionObject

Returns the value of attribute description.



16
17
18
# File 'lib/pact/consumer_contract/message.rb', line 16

def description
  @description
end

#metadataObject

Returns the value of attribute metadata.



16
17
18
# File 'lib/pact/consumer_contract/message.rb', line 16

def 
  @metadata
end

#provider_stateObject

Returns the value of attribute provider_state.



16
17
18
# File 'lib/pact/consumer_contract/message.rb', line 16

def provider_state
  @provider_state
end

#provider_statesObject

Returns the value of attribute provider_states.



16
17
18
# File 'lib/pact/consumer_contract/message.rb', line 16

def provider_states
  @provider_states
end

Class Method Details

.from_hash(hash, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pact/consumer_contract/message.rb', line 26

def self.from_hash hash, options = {}
  opts = options.dup
  unless opts[:pact_specification_version]
    opts[:pact_specification_version] = Pact::SpecificationVersion::NIL_VERSION
  end
  contents_matching_rules = hash['matchingRules'] && hash['matchingRules']['body']
  contents_hash = Pact::MatchingRules.merge(hash['contents'], contents_matching_rules, opts)
  contents = Pact::ConsumerContract::Message::Contents.from_hash(contents_hash)
   = hash['metaData'] || hash['metadata']
  provider_state = hash['providerStates'] && hash['providerStates'].first && hash['providerStates'].first['name']
  provider_states = parse_provider_states(hash['providerStates'])
  new(symbolize_keys(hash).merge(
    contents: contents,
    provider_state: provider_state,
    provider_states: provider_states,
    metadata: ))
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
# File 'lib/pact/consumer_contract/message.rb', line 92

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

#description_with_provider_state_quotedObject



113
114
115
# File 'lib/pact/consumer_contract/message.rb', line 113

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

#eq?(other) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/pact/consumer_contract/message.rb', line 109

def eq? other
  self == other
end

#http?Boolean

Returns:

  • (Boolean)


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

def http?
  false
end

#match_criterion(target, criterion) ⇒ Object



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

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

#matches_criteria?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
# File 'lib/pact/consumer_contract/message.rb', line 96

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

#message?Boolean

Returns:

  • (Boolean)


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

def message?
  true
end

#requestObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pact/consumer_contract/message.rb', line 53

def request
  @request ||= Pact::Consumer::Request::Actual.from_hash(
    path: '/',
    method: 'POST',
    query: nil,
    headers: { 'Content-Type' => 'application/json' },
    body: {
      description: description,
      providerStates: [{
        name: provider_state,
        params: {}
      }]
    }
  )
end

#responseObject

custom media type?



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

def response
  @response ||= Pact::Response.new(
    status: 200,
    headers: {'Content-Type' => 'application/json'},
    body: {
      contents: contents
    }
  )
end

#to_hashObject



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

def to_hash
  {
    description: description,
    provider_states: [{ name: provider_state }],
    contents: contents.to_hash,
    metadata: 
  }
end

#to_sObject



117
118
119
# File 'lib/pact/consumer_contract/message.rb', line 117

def to_s
  to_hash.to_s
end

#validate!Object

Raises:

  • (Pact::InvalidMessageError)


88
89
90
# File 'lib/pact/consumer_contract/message.rb', line 88

def validate!
  raise Pact::InvalidMessageError.new(self) unless description && contents
end