class Pact::InteractionV3Parser
Public Class Methods
call(hash, options)
click to toggle source
# 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)
click to toggle source
# File lib/pact/consumer_contract/interaction_v3_parser.rb, line 82 def self.look_up_matching_rules(key, matching_rules) # The matching rules for the path operate on the object itself and don't have sub paths # Convert it into the format that Merge expects. if key == 'path' matching_rules[key] ? { '$.' => matching_rules[key] } : nil else matching_rules[key] end end
parse_metadata(metadata_hash)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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