Skip to content

Commit 531ab3a

Browse files
committed
feat: allow different consumer contract parsers to be registered
1 parent cab8156 commit 531ab3a

File tree

10 files changed

+39
-310
lines changed

10 files changed

+39
-310
lines changed

lib/pact/consumer_contract/consumer_contract.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
require 'pact/consumer_contract/service_consumer'
1010
require 'pact/consumer_contract/service_provider'
1111
require 'pact/consumer_contract/interaction'
12-
require 'pact/consumer_contract/message'
1312
require 'pact/consumer_contract/pact_file'
13+
require 'pact/consumer_contract/http_consumer_contract_parser'
1414

1515
module Pact
16-
1716
class ConsumerContract
1817

1918
include SymbolizeKeys
@@ -30,21 +29,19 @@ def initialize(attributes = {})
3029
@provider = attributes[:provider]
3130
end
3231

32+
def self.add_parser consumer_contract_parser
33+
parsers << consumer_contract_parser
34+
end
35+
36+
def self.parsers
37+
@parsers ||= [Pact::HttpConsumerContractParser.new]
38+
end
39+
3340
def self.from_hash(hash)
34-
hash = symbolize_keys(hash)
35-
interactions = if hash[:interactions]
36-
hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
37-
elsif hash[:messages]
38-
hash[:messages].collect { |hash| Message.from_hash(hash)}
39-
else
40-
[] # or raise an error?
41+
parsers.each do | parser |
42+
return parser.call(hash) if parser.can_parse?(hash)
4143
end
42-
43-
new(
44-
:consumer => ServiceConsumer.from_hash(hash[:consumer]),
45-
:provider => ServiceProvider.from_hash(hash[:provider]),
46-
:interactions => interactions
47-
)
44+
raise Pact::Error.new("No consumer contract parser found for hash: #{hash}")
4845
end
4946

5047
def self.from_json string
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Pact
2+
class HttpConsumerContractParser
3+
include SymbolizeKeys
4+
5+
def call(hash)
6+
hash = symbolize_keys(hash)
7+
interactions = if hash[:interactions]
8+
hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
9+
elsif hash[:messages]
10+
hash[:messages].collect { |hash| Message.from_hash(hash)}
11+
else
12+
[] # or raise an error?
13+
end
14+
15+
ConsumerContract.new(
16+
:consumer => ServiceConsumer.from_hash(hash[:consumer]),
17+
:provider => ServiceProvider.from_hash(hash[:provider]),
18+
:interactions => interactions
19+
)
20+
end
21+
22+
def can_parse?(hash)
23+
hash.key?('interactions') || hash.key?(:interactions)
24+
end
25+
end
26+
end

lib/pact/consumer_contract/interaction.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ def http?
4040
true
4141
end
4242

43-
def message?
44-
false
45-
end
46-
4743
def validate!
4844
raise Pact::InvalidInteractionError.new(self) unless description && request && response
4945
end

lib/pact/consumer_contract/message.rb

Lines changed: 0 additions & 146 deletions
This file was deleted.

lib/pact/consumer_contract/message/content.rb

Lines changed: 0 additions & 61 deletions
This file was deleted.

lib/pact/errors.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ module Pact
22
class Error < ::StandardError
33
end
44

5-
class InvalidMessageError < Error
6-
end
7-
85
# Raised when the interaction is not defined correctly
96
class InvalidInteractionError < Error
107
def initialize(interaction)

spec/lib/pact/consumer_contract/consumer_contract_spec.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ module Pact
5252
end
5353
end
5454
end
55-
56-
context "with a Message contract" do
57-
let(:string) { '{"messages":[{"content": {"foo": "bar"}}], "consumer": {"name" : "Bob"} , "provider": {"name" : "Mary"}}' }
58-
59-
it "should create a Pact" do
60-
expect(loaded_pact).to be_instance_of ConsumerContract
61-
end
62-
63-
it "should have messages" do
64-
expect(loaded_pact.interactions).to be_instance_of Array
65-
expect(loaded_pact.interactions.first).to be_instance_of Pact::ConsumerContract::Message
66-
end
67-
68-
it "should have a consumer" do
69-
expect(loaded_pact.consumer).to be_instance_of Pact::ServiceConsumer
70-
end
71-
72-
it "should have a provider" do
73-
expect(loaded_pact.provider).to be_instance_of Pact::ServiceProvider
74-
end
75-
76-
end
7755
end
7856

7957
describe "find_interactions" do

spec/lib/pact/consumer_contract/message_spec_with_message_class.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

spec/lib/pact/consumer_contract/message_spec_with_message_module.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)