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-0.10.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.10.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.9.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.8.2 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.8.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.8.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.7.2 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.7.1 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.7.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.6.0 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.5.5 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.5.4 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.5.3 lib/pact/consumer_contract/response_decorator.rb
pact-mock_service-0.5.2 lib/pact/consumer_contract/response_decorator.rb