Sha256: 49b001dba972b3c9df136f158e5f93eb5e0c0b2c4145c5450609f4391c924b9c

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'net/http'
require 'uri'
require 'pact'

module Pact
  module Messages
    module ContractRepository
      module_function

      def get_message_contract(provider_name, consumer_name, provider_state = nil)
        contract = get_contract(provider_name, consumer_name)
        fail "Contract between #{provider_name} and #{consumer_name} not found" unless contract
        find_response_for_provider_state(contract.interactions, provider_state)
      end

      def get_message_sample(provider_name, consumer_name, provider_state = nil)
        Pact::Reification.from_term(get_message_contract(provider_name, consumer_name, provider_state))
      end

      class << self
        private

        def get_contract(provider_name, consumer_name)
          # search for it locally, if not found look on the pack broker
          Pact::Messages.consumer_world.find_contract(provider_name, consumer_name) ||
            Pact::ConsumerContract.from_uri(pact_broker_url(provider_name, consumer_name))
        end

        def pact_broker_url(provider, consumer)
          url = URI.encode("/pacts/provider/#{provider}/consumer/#{consumer}/")
          URI(ENV.fetch('PACT_BROKER_URL') + url + 'latest')
        end

        def find_response_for_provider_state(interactions, state)
          interaction = interactions.find { |i| i.provider_state == state }
          unless interaction
            fail "State #{state} not found among interaction states #{interactions.map(&:provider_state)}"
          end

          interaction.response[:body]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-messages-0.1.0 lib/pact/messages/contract_repository.rb