Skip to content

Commit

Permalink
feat: allow different consumer contract parsers to be registered
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 8, 2018
1 parent cab8156 commit 531ab3a
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 310 deletions.
27 changes: 12 additions & 15 deletions lib/pact/consumer_contract/consumer_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
require 'pact/consumer_contract/service_consumer'
require 'pact/consumer_contract/service_provider'
require 'pact/consumer_contract/interaction'
require 'pact/consumer_contract/message'
require 'pact/consumer_contract/pact_file'
require 'pact/consumer_contract/http_consumer_contract_parser'

module Pact

class ConsumerContract

include SymbolizeKeys
Expand All @@ -30,21 +29,19 @@ def initialize(attributes = {})
@provider = attributes[:provider]
end

def self.add_parser consumer_contract_parser
parsers << consumer_contract_parser
end

def self.parsers
@parsers ||= [Pact::HttpConsumerContractParser.new]
end

def self.from_hash(hash)
hash = symbolize_keys(hash)
interactions = if hash[:interactions]
hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
elsif hash[:messages]
hash[:messages].collect { |hash| Message.from_hash(hash)}
else
[] # or raise an error?
parsers.each do | parser |
return parser.call(hash) if parser.can_parse?(hash)
end

new(
:consumer => ServiceConsumer.from_hash(hash[:consumer]),
:provider => ServiceProvider.from_hash(hash[:provider]),
:interactions => interactions
)
raise Pact::Error.new("No consumer contract parser found for hash: #{hash}")
end

def self.from_json string
Expand Down
26 changes: 26 additions & 0 deletions lib/pact/consumer_contract/http_consumer_contract_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Pact
class HttpConsumerContractParser
include SymbolizeKeys

def call(hash)
hash = symbolize_keys(hash)
interactions = if hash[:interactions]
hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
elsif hash[:messages]
hash[:messages].collect { |hash| Message.from_hash(hash)}
else
[] # or raise an error?
end

ConsumerContract.new(
:consumer => ServiceConsumer.from_hash(hash[:consumer]),
:provider => ServiceProvider.from_hash(hash[:provider]),
:interactions => interactions
)
end

def can_parse?(hash)
hash.key?('interactions') || hash.key?(:interactions)
end
end
end
4 changes: 0 additions & 4 deletions lib/pact/consumer_contract/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ def http?
true
end

def message?
false
end

def validate!
raise Pact::InvalidInteractionError.new(self) unless description && request && response
end
Expand Down
146 changes: 0 additions & 146 deletions lib/pact/consumer_contract/message.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/pact/consumer_contract/message/content.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/pact/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module Pact
class Error < ::StandardError
end

class InvalidMessageError < Error
end

# Raised when the interaction is not defined correctly
class InvalidInteractionError < Error
def initialize(interaction)
Expand Down
22 changes: 0 additions & 22 deletions spec/lib/pact/consumer_contract/consumer_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,6 @@ module Pact
end
end
end

context "with a Message contract" do
let(:string) { '{"messages":[{"content": {"foo": "bar"}}], "consumer": {"name" : "Bob"} , "provider": {"name" : "Mary"}}' }

it "should create a Pact" do
expect(loaded_pact).to be_instance_of ConsumerContract
end

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

it "should have a consumer" do
expect(loaded_pact.consumer).to be_instance_of Pact::ServiceConsumer
end

it "should have a provider" do
expect(loaded_pact.provider).to be_instance_of Pact::ServiceProvider
end

end
end

describe "find_interactions" do
Expand Down
25 changes: 0 additions & 25 deletions spec/lib/pact/consumer_contract/message_spec_with_message_class.rb

This file was deleted.

This file was deleted.

Loading

0 comments on commit 531ab3a

Please sign in to comment.