Sha256: 8b506eb859e7543013f6f2283b6a3323b05bee6f74ab4f548c8ec5bed2a00c47

Contents?: true

Size: 1.62 KB

Versions: 22

Compression:

Stored size: 1.62 KB

Contents

require 'pact/shared/active_support_support'
require 'pact/consumer_contract/interaction_decorator'

module Pact
  class ConsumerContractDecorator

    include ActiveSupportSupport

    def initialize consumer_contract, decorator_options = {}
      @consumer_contract = consumer_contract
      @decorator_options = decorator_options
    end

    def as_json(options = {})
      fix_all_the_things(
        consumer: consumer_contract.consumer.as_json,
        provider: consumer_contract.provider.as_json,
        interactions: sorted_interactions.collect{ |i| InteractionDecorator.new(i, @decorator_options).as_json},
        metadata: {
          pactSpecification: {
            version: pact_specification_version
          }
        }
      )
    end

    def to_json(options = {})
      as_json.to_json(options)
    end

    private

    def sorted_interactions
      # Default order: chronological
      return consumer_contract.writable_interactions if Pact.configuration.pactfile_write_order == :chronological
      # We are supporting only chronological or alphabetical order
      raise NotImplementedError if Pact.configuration.pactfile_write_order != :alphabetical

      consumer_contract.writable_interactions.sort{|a, b| sortable_id(a) <=> sortable_id(b)}
    end

    def sortable_id interaction
      "#{interaction.description.downcase} #{interaction.response.status} #{(interaction.provider_state || '').downcase}"
    end

    attr_reader :consumer_contract

    def pact_specification_version
      version = @decorator_options.fetch(:pact_specification_version)
      "#{version[0]}.0.0" # Only care about the first digit
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
pact-mock_service-3.12.3 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.12.2 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.12.1 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.12.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.11.2 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.11.1 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.11.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.10.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
hs-pact-mock_service-3.9.2 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.9.1 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.9.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.8.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.7.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.6.2 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.6.1 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.6.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.5.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.4.0 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.3.1 lib/pact/consumer_contract/consumer_contract_decorator.rb
pact-mock_service-3.3.0 lib/pact/consumer_contract/consumer_contract_decorator.rb