Sha256: 664078c400b91ab5da8506ea165e122529b4b9f001a5c9f7c099292dcd966db5

Contents?: true

Size: 1.79 KB

Versions: 15

Compression:

Stored size: 1.79 KB

Contents

require 'pact/consumer_contract/request'
require 'pact/symbolize_keys'
require 'pact/consumer_contract/active_support_support'

module Pact
   class Interaction
    include ActiveSupportSupport
      include SymbolizeKeys

      attr_accessor :description, :request, :response, :provider_state

      def initialize attributes = {}
        @description = attributes[:description]
        @request = attributes[:request]
        @response = attributes[:response]
        @provider_state = attributes[:provider_state]
      end

      def self.from_hash hash
        request = Pact::Request::Expected.from_hash(hash['request'])
        new(symbolize_keys(hash).merge({request: request}))
      end

      def to_hash
        hash = { :description => @description }
        hash[:provider_state] = @provider_state if @provider_state #Easier to read when provider state at top
        hash.merge(:request => @request.as_json, :response => @response)
      end

      def as_json options = {}
        fix_all_the_things to_hash
      end

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

      def matches_criteria? criteria
        criteria.each do | key, value |
          unless match_criterion self.send(key.to_s), value
            return false
          end
        end
        true
      end

      def match_criterion target, criterion
        target == criterion || (criterion.is_a?(Regexp) && criterion.match(target))
      end

      def == other
        other.is_a?(Interaction) && as_json == other.as_json
      end

      def eq? other
        self == other
      end

      def description_with_provider_state_quoted
        provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
      end

      def to_s
        JSON.pretty_generate(self)
      end
   end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pact-1.0.39 lib/pact/consumer_contract/interaction.rb
pact-1.1.0.rc2 lib/pact/consumer_contract/interaction.rb
pact-1.0.38 lib/pact/consumer_contract/interaction.rb
pact-1.1.0.rc1 lib/pact/consumer_contract/interaction.rb
pact-1.0.37 lib/pact/consumer_contract/interaction.rb
pact-1.0.36 lib/pact/consumer_contract/interaction.rb
pact-1.0.35 lib/pact/consumer_contract/interaction.rb
pact-1.0.34 lib/pact/consumer_contract/interaction.rb
pact-1.0.33 lib/pact/consumer_contract/interaction.rb
pact-1.0.32 lib/pact/consumer_contract/interaction.rb
pact-1.0.31 lib/pact/consumer_contract/interaction.rb
pact-1.0.30 lib/pact/consumer_contract/interaction.rb
pact-1.0.29 lib/pact/consumer_contract/interaction.rb
pact-1.0.28 lib/pact/consumer_contract/interaction.rb
pact-1.0.27 lib/pact/consumer_contract/interaction.rb