Class: Pact::MockService::App

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/mock_service/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pact/mock_service/app.rb', line 18

def initialize options = {}
  logger = Logger.from_options(options)
  @options = options
  stubbing = options[:stub_pactfile_paths] && options[:stub_pactfile_paths].any?
  @name = options.fetch(:name, "MockService")
  @session = Session.new(options.merge(logger: logger, warn_on_too_many_interactions: !stubbing))
  setup_stub(options[:stub_pactfile_paths]) if stubbing
  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

Instance Method Details

#call(env) ⇒ Object



33
34
35
# File 'lib/pact/mock_service/app.rb', line 33

def call env
  @app.call env
end

#setup_stub(stub_pactfile_paths) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/pact/mock_service/app.rb', line 41

def setup_stub stub_pactfile_paths
  interactions = stub_pactfile_paths.collect do | pactfile_path |
    $stdout.puts "INFO: Loading interactions from #{pactfile_path}"
    hash_interactions = JSON.parse(Pact::PactFile.read(pactfile_path, pactfile_options))['interactions']
    hash_interactions.collect { | hash | Interaction.from_hash(hash) }
  end.flatten
  @session.set_expected_interactions interactions
end

#shutdownObject



37
38
39
# File 'lib/pact/mock_service/app.rb', line 37

def shutdown
  write_pact_if_configured
end

#to_sObject



58
59
60
# File 'lib/pact/mock_service/app.rb', line 58

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

#write_pact_if_configuredObject



50
51
52
53
54
55
56
# File 'lib/pact/mock_service/app.rb', line 50

def write_pact_if_configured
  consumer_contract_writer = ConsumerContractWriter.new(@session.consumer_contract_details, StdoutLogger.new)
  if consumer_contract_writer.can_write? && !@session.pact_written?
    $stdout.puts "INFO: Writing pact before shutting down"
    consumer_contract_writer.write
  end
end