Sha256: 52f64e925840acc246e997e2495263a01b9e4f6eab10e62cca89cb747b2b1ed0

Contents?: true

Size: 1.18 KB

Versions: 74

Compression:

Stored size: 1.18 KB

Contents

require 'pact/matching_rules'
require 'pact/term'

module Pact
  class ResponseDecorator

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

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

    def as_json options = {}
      include_matching_rules? ? with_matching_rules(attributes_hash) : attributes_hash
    end

    private

    attr_reader :response

    def attributes_hash
      hash = {}
      hash[:status]  = response.status  if response.specified?(:status)
      hash[:headers] = response.headers if response.specified?(:headers)
      hash[:body]    = response.body    if response.specified?(:body)
      hash
    end

    def include_matching_rules?
      pact_specification_version && !pact_specification_version.start_with?('1')
    end

    def with_matching_rules hash
      matching_rules = Pact::MatchingRules.extract hash
      example = Pact::Reification.from_term hash
      return example if matching_rules.empty?
      example.merge(matchingRules: matching_rules)
    end

    def pact_specification_version
      @decorator_options[:pact_specification_version]
    end
  end
end

Version data entries

74 entries across 74 versions & 2 rubygems

Version Path
pact-mock_service-3.2.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-3.2.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-3.1.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-3.1.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-3.0.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-3.0.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.12.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.11.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.10.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.10.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.9.8 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.9.3 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.9.2 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.9.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.9.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.8.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.8.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.7.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.7.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-2.6.4 lib/pact/consumer_contract/response_decorator.rb