Sha256: 50bbf029bfe57783265d65fcfb9882f9439599e90690a8feeafd4017e3519d82

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'net/http'
require 'pact/reification'
require 'pact/request'
#require 'json/add/core'

module Pact
  module Consumer

    class Interaction

      attr_accessor :description, :request, :response, :producer_state

      def initialize options
        @description = options[:description]
        @request = options[:request]
        @response = options[:response]
        @producer_state = options[:producer_state]
      end

      def self.from_hash options
        new(:description => options['description'],
            :producer_state => options['producer_state'],
            :request => Pact::Request::Expected.from_hash(options['request']),
            :response => options['response']
          )
      end

      def as_json
        {
          :description => @description,
          :request => @request.as_json,
          :response => @response,
        }.tap{ | hash | hash[:producer_state] = @producer_state if @producer_state }
      end

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


      def to_json_with_generated_response
        as_json.tap { | hash | hash[:response] = Reification.from_term(response) }.to_json
      end
    end

    class InteractionBuilder

      attr_reader :interaction

      def initialize(description, producer_state)
        producer_state = producer_state.nil? ? nil : producer_state.to_s
        @interaction = Interaction.new(:description => description, :producer_state => producer_state)
      end

      def with(request_details)
        interaction.request = Request::Expected.from_hash(request_details)
        self
      end

      def will_respond_with(response)
        interaction.response = response
        @callback.call interaction
      end

      def on_interaction_fully_defined &block
        @callback = block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-0.1.28 lib/pact/consumer/interaction.rb