Class: Pact::InteractionV3Parser

Inherits:
Object
  • Object
show all
Includes:
SymbolizeKeys
Defined in:
lib/pact/consumer_contract/interaction_v3_parser.rb

Class Method Summary collapse

Methods included from SymbolizeKeys

included, #symbolize_keys

Class Method Details

.call(hash, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# 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
  Interaction.new(symbolize_keys(hash).merge(request: request, response: response, provider_states: provider_states, provider_state: provider_state))
end

.parse_provider_states(provider_states) ⇒ Object



67
68
69
70
71
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 67

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



25
26
27
28
29
30
31
32
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 25

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



43
44
45
46
47
48
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 43

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], request_matching_rules[key], options)
  end
  Pact::Request::Expected.from_hash(request_hash)
end

.parse_request_with_string_body(request_hash, request_matching_rules, options) ⇒ Object



57
58
59
60
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 57

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



34
35
36
37
38
39
40
41
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 34

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



50
51
52
53
54
55
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 50

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], response_matching_rules[key], options)
  end
  Pact::Response.from_hash(response_hash)
end

.parse_response_with_string_body(response_hash, response_matching_rules, options) ⇒ Object



62
63
64
65
# File 'lib/pact/consumer_contract/interaction_v3_parser.rb', line 62

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