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
25
26
# 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]
  @_id = attributes[:_id]
  @index = attributes[:index]
end

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



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

def _id
  @_id
end

#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

#indexObject

Returns the value of attribute index.



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

def index
  @index
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



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

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_name = parse_provider_state_name(hash['providerState'], hash['providerStates'])
  provider_states = parse_provider_states(provider_state_name, hash['providerStates'])
  new(symbolize_keys(hash).merge(
    contents: contents,
    provider_state: provider_state_name,
    provider_states: provider_states,
    metadata: ))
end

Instance Method Details

#==(other) ⇒ Object



96
97
98
# File 'lib/pact/consumer_contract/message.rb', line 96

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

#description_with_provider_state_quotedObject



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

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

#eq?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eq? other
  self == other
end

#http?Boolean

Returns:

  • (Boolean)


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

def http?
  false
end

#match_criterion(target, criterion) ⇒ Object



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

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

#matches_criteria?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/pact/consumer_contract/message.rb', line 100

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)


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

def message?
  true
end

#requestObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pact/consumer_contract/message.rb', line 56

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: {}
      }],
      metadata: 
    }
  )
end

#responseObject

custom media type?



74
75
76
77
78
79
80
81
82
# File 'lib/pact/consumer_contract/message.rb', line 74

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

#to_hashObject



47
48
49
50
51
52
53
54
# File 'lib/pact/consumer_contract/message.rb', line 47

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

#to_sObject



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

def to_s
  to_hash.to_s
end

#validate!Object

Raises:

  • (Pact::InvalidMessageError)


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

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