Skip to content

Commit

Permalink
feat: update message classes to support pact-message
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 12, 2018
1 parent 93839cf commit 2e48892
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 88 deletions.
2 changes: 1 addition & 1 deletion lib/pact/consumer_contract/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'pact/errors'

module Pact
class Interaction
class Interaction
include ActiveSupportSupport
include SymbolizeKeys

Expand Down
178 changes: 98 additions & 80 deletions lib/pact/consumer_contract/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,118 @@
require 'pact/errors'
require 'pact/consumer/request'
require 'pact/consumer_contract/response'
require 'pact/consumer_contract/message/content'

module Pact
class Message
include ActiveSupportSupport
include SymbolizeKeys

attr_accessor :description, :content, :provider_state

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

def self.from_hash hash
content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules'])
content = Pact::Message::Content.new(content_hash)
new(symbolize_keys(hash).merge(content: content))
end

def to_hash
{
description: description,
provider_state: provider_state,
content: content.to_hash,
}
end


def request
@request ||= Pact::Consumer::Request::Actual.from_hash(
path: '/',
method: 'POST',
query: nil,
headers: {'Content-Type' => 'application/json'},
body: {
class ConsumerContract
class Message
include Pact::ActiveSupportSupport
include Pact::SymbolizeKeys

attr_accessor :description, :content, :provider_state

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

def self.from_hash hash
content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules'])
content = Pact::ConsumerContract::Message::Content.new(content_hash)
new(symbolize_keys(hash).merge(content: content))
end

def to_hash
{
description: description,
providerStates: [{
name: provider_state
}]
provider_state: provider_state,
content: content.to_hash,
}
)
end

# custom media type?
def response
@response ||= Pact::Response.new(
status: 200,
headers: {'Content-Type' => 'application/json'},
body: {
content: content
end

# todo move this proper decorator
def as_json
{
description: description,
providerState: provider_state,
content: content.as_json
}
)
end
end


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
}]
}
)
end

def http?
false
end
# custom media type?
def response
@response ||= Pact::Response.new(
status: 200,
headers: {'Content-Type' => 'application/json'},
body: {
content: content
}
)
end

def message?
true
end
def http?
false
end

def validate!
raise Pact::InvalidMessageError.new(self) unless description && content
end
def message?
true
end

def == other
other.is_a?(Message) && to_hash == other.to_hash
end
def validate!
raise Pact::InvalidMessageError.new(self) unless description && content
end

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

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

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

def eq? other
self == other
end

def eq? other
self == other
end
def description_with_provider_state_quoted
provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
end

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

def to_s
to_hash.to_s
end
end
module Pact::Message
def self.new *args
Pact::ConsumerContract::Message.new(*args)
end
end
26 changes: 20 additions & 6 deletions lib/pact/consumer_contract/message/content.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
module Pact
class Message
class Content < Hash
include ActiveSupportSupport
include SymbolizeKeys
class ConsumerContract
class Message
class Content
include ActiveSupportSupport
include SymbolizeKeys

def initialize hash
merge!(hash)
def initialize content
@content = content
end

def to_s
if @content.is_a?(Hash) || @content.is_a?(Array)
@content.to_json
else
@content.to_s
end
end

def as_json
@content
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact/consumer_contract/consumer_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Pact

it "should have messages" do
expect(loaded_pact.interactions).to be_instance_of Array
expect(loaded_pact.interactions.first).to be_instance_of Pact::Message
expect(loaded_pact.interactions.first).to be_instance_of Pact::ConsumerContract::Message
end

it "should have a consumer" do
Expand Down

0 comments on commit 2e48892

Please sign in to comment.