Class: Pact::MockService::App
- Inherits:
-
Object
- Object
- Pact::MockService::App
- Defined in:
- lib/pact/mock_service/app.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(options = {}) ⇒ App
constructor
A new instance of App.
- #setup_stub(stub_pactfile_paths) ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
- #write_pact_if_configured ⇒ Object
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 = {} logger = Logger.() @options = stubbing = [:stub_pactfile_paths] && [:stub_pactfile_paths].any? @name = .fetch(:name, "MockService") @session = Session.new(.merge(logger: logger, warn_on_too_many_interactions: !stubbing)) setup_stub([:stub_pactfile_paths]) if stubbing request_handlers = RequestHandlers.new(@name, logger, @session, ) @app = Rack::Builder.app do use Pact::Consumer::MockService::ErrorHandler, logger use Pact::Consumer::CorsOriginHeaderMiddleware, [: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, ))['interactions'] hash_interactions.collect { | hash | Interaction.from_hash(hash) } end.flatten @session.set_expected_interactions interactions end |
#shutdown ⇒ Object
37 38 39 |
# File 'lib/pact/mock_service/app.rb', line 37 def shutdown write_pact_if_configured end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/pact/mock_service/app.rb', line 58 def to_s "#{@name} #{super.to_s}" end |
#write_pact_if_configured ⇒ Object
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 |