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.



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

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



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

def call env
  @app.call env
end

#setup_stub(stub_pactfile_paths) ⇒ Object



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

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))['interactions']
    hash_interactions.collect { | hash | Interaction.from_hash(hash) }
  end.flatten
  @session.set_expected_interactions interactions
end

#shutdownObject



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

def shutdown
  write_pact_if_configured
end

#to_sObject



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

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

#write_pact_if_configuredObject



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

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