Sha256: 61454ea0f7e1779544af420144102d9c58d0feddaf79e96d2e62a72aab5d35ed

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'rack'
require 'uri'
require 'json'
require 'pact/mock_service/logger'
require 'pact/consumer/mock_service/cors_origin_header_middleware'
require 'pact/mock_service/request_handlers'
require 'pact/consumer/mock_service/error_handler'
require 'pact/mock_service/session'

module Pact
  module MockService

    def self.new *args
      App.new(*args)
    end

    class App

      def initialize options = {}
        @name = options.fetch(:name, "MockService")
        logger = Logger.from_options(options)
        @session = Session.new(options.merge(logger: logger))
        request_handlers = RequestHandlers.new(@name, logger, @session, options)
        @app = Rack::Builder.app do
          use Pact::Consumer::MockService::ErrorHandler, logger
          use Pact::Consumer::CorsOriginHeaderMiddleware, options[:cors_enabled]
          run request_handlers
        end
      end

      def call env
        @app.call env
      end

      def shutdown
        write_pact_if_configured
      end

      def write_pact_if_configured
        consumer_contract_writer = ConsumerContractWriter.new(@session.consumer_contract_details, StdoutLogger.new)
        consumer_contract_writer.write if consumer_contract_writer.can_write?
      end

      def to_s
        "#{@name} #{super.to_s}"
      end
    end

    # Can't write to a file in a TRAP, might deadlock
    class StdoutLogger
      def info message
        $stdout.puts "\n#{message}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-mock_service-0.3.0 lib/pact/mock_service/app.rb