Class: Pact::InteractionV3Parser
- Inherits:
-
Object
- Object
- Pact::InteractionV3Parser
show all
- Includes:
- SymbolizeKeys
- Defined in:
- lib/pact/consumer_contract/interaction_v3_parser.rb
Class Method Summary
collapse
-
.call(hash, options) ⇒ Object
-
.look_up_matching_rules(key, matching_rules) ⇒ Object
-
.parse_metadata(metadata_hash) ⇒ Object
-
.parse_provider_states(provider_states) ⇒ Object
-
.parse_request(request_hash, options) ⇒ Object
-
.parse_request_with_non_string_body(request_hash, request_matching_rules, options) ⇒ Object
-
.parse_request_with_string_body(request_hash, request_matching_rules, options) ⇒ Object
-
.parse_response(response_hash, options) ⇒ Object
-
.parse_response_with_non_string_body(response_hash, response_matching_rules, options) ⇒ Object
-
.parse_response_with_string_body(response_hash, response_matching_rules, options) ⇒ Object
included, #symbolize_keys
Class Method Details
.call(hash, options) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 14
def self.call hash, options
request = parse_request(hash['request'], options)
response = parse_response(hash['response'], options)
provider_states = parse_provider_states(hash['providerStates'])
provider_state = provider_states.any? ? provider_states.first.name : nil
if provider_states && provider_states.size > 1
Pact.configuration.error_stream.puts("WARN: Currently only 1 provider state is supported. Ignoring ")
end
metadata = parse_metadata(hash['metadata'])
Interaction.new(symbolize_keys(hash).merge(request: request,
response: response,
provider_states: provider_states,
provider_state: provider_state,
metadata: metadata))
end
|
.look_up_matching_rules(key, matching_rules) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 82
def self.look_up_matching_rules(key, matching_rules)
if key == 'path'
matching_rules[key] ? { '$.' => matching_rules[key] } : nil
else
matching_rules[key]
end
end
|
78
79
80
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 78
def self.parse_metadata metadata_hash
symbolize_keys(metadata_hash)
end
|
.parse_provider_states(provider_states) ⇒ Object
72
73
74
75
76
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 72
def self.parse_provider_states provider_states
(provider_states || []).collect do | provider_state_hash |
Pact::ProviderState.new(provider_state_hash['name'], provider_state_hash['params'])
end
end
|
.parse_request(request_hash, options) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 30
def self.parse_request request_hash, options
request_matching_rules = request_hash['matchingRules'] || {}
if request_hash['body'].is_a?(String)
parse_request_with_string_body(request_hash, request_matching_rules['body'] || {}, options)
else
parse_request_with_non_string_body(request_hash, request_matching_rules, options)
end
end
|
.parse_request_with_non_string_body(request_hash, request_matching_rules, options) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 48
def self.parse_request_with_non_string_body request_hash, request_matching_rules, options
request_hash = request_hash.keys.each_with_object({}) do | key, new_hash |
new_hash[key] = Pact::MatchingRules.merge(request_hash[key], look_up_matching_rules(key, request_matching_rules), options)
end
Pact::Request::Expected.from_hash(request_hash)
end
|
.parse_request_with_string_body(request_hash, request_matching_rules, options) ⇒ Object
62
63
64
65
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 62
def self.parse_request_with_string_body request_hash, request_matching_rules, options
string_with_matching_rules = StringWithMatchingRules.new(request_hash['body'], options[:pact_specification_version], request_matching_rules)
Pact::Request::Expected.from_hash(request_hash.merge('body' => string_with_matching_rules))
end
|
.parse_response(response_hash, options) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 39
def self.parse_response response_hash, options
response_matching_rules = response_hash['matchingRules'] || {}
if response_hash['body'].is_a?(String)
parse_response_with_string_body(response_hash, response_matching_rules['body'] || {}, options)
else
parse_response_with_non_string_body(response_hash, response_matching_rules, options)
end
end
|
.parse_response_with_non_string_body(response_hash, response_matching_rules, options) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 55
def self.parse_response_with_non_string_body response_hash, response_matching_rules, options
response_hash = response_hash.keys.each_with_object({}) do | key, new_hash |
new_hash[key] = Pact::MatchingRules.merge(response_hash[key], look_up_matching_rules(key, response_matching_rules), options)
end
Pact::Response.from_hash(response_hash)
end
|
.parse_response_with_string_body(response_hash, response_matching_rules, options) ⇒ Object
67
68
69
70
|
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 67
def self.parse_response_with_string_body response_hash, response_matching_rules, options
string_with_matching_rules = StringWithMatchingRules.new(response_hash['body'], options[:pact_specification_version], response_matching_rules)
Pact::Response.from_hash(response_hash.merge('body' => string_with_matching_rules))
end
|