Sha256: 043622f0682d06a4dca77e288f3232e5548b6e81103e1276e47a6821d4110462

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'pact/mock_service/app'
require 'pact/consumer/server'
require 'pact/consumer/mock_service/set_location'

# Starts a new MockService on a new Thread. Called by the Control Server.

module Pact
  module MockService
    class Spawn

      def self.call consumer, provider, port, options
        new(consumer, provider, port, options).call
      end

      attr_reader :consumer, :provider, :port, :options

      def initialize consumer, provider, port, options
        @consumer = consumer
        @provider = provider
        @port = port
        @options = options
      end

      def call
        mock_service = build_app
        start_mock_service mock_service, port
        puts "Started mock service for #{provider} on #{port}"
        mock_service
      end

      private

      def build_app
        Pact::Consumer::SetLocation.new(mock_service, base_url)
      end

      def mock_service
        Pact::MockService.new(
          log_file: create_log_file,
          name: name,
          consumer: consumer,
          provider: provider,
          pact_dir: options[:pact_dir] || ".",
          cors_enabled: options[:cors_enabled]
        )
      end

      def start_mock_service app, port
        Pact::Server.new(app, port, ssl: options[:ssl]).boot
      end

      def create_log_file
        FileUtils::mkdir_p options[:log_dir]
        log = File.open(log_file_path, 'w')
        log.sync = true
        log
      end

      def log_file_name
        lower_case_name = name.downcase.gsub(/\s+/, '_')
        if lower_case_name.include?('_service')
          lower_case_name.gsub('_service', '_mock_service')
        else
          lower_case_name + '_mock_service'
        end
      end

      def log_file_path
        File.join(options[:log_dir], "#{log_file_name}.log")
      end

      def base_url
        options[:ssl] ? "https://localhost:#{port}" : "http://localhost:#{port}"
      end

      def name
        "#{provider} mock service"
      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/spawn.rb