Class: Pact::Consumer::MockService

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MockService

Returns a new instance of MockService.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pact/consumer/mock_service/app.rb', line 26

def initialize options = {}
  log_description = configure_logger options
  interaction_list = InteractionList.new

  @name = options.fetch(:name, "MockService")
  @handlers = [
    MissingInteractionsGet.new(@name, @logger, interaction_list),
    VerificationGet.new(@name, @logger, interaction_list, log_description),
    InteractionPost.new(@name, @logger, interaction_list),
    InteractionDelete.new(@name, @logger, interaction_list),
    LogGet.new(@name, @logger),
    InteractionReplay.new(@name, @logger, interaction_list)
  ]
end

Instance Method Details

#call(env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pact/consumer/mock_service/app.rb', line 58

def call env
  response = []
  begin
    relevant_handler = @handlers.detect { |handler| handler.match? env }
    response = relevant_handler.respond env
  rescue Exception => e
    @logger.error 'Error ocurred in mock service:'
    @logger.ap e, :error
    @logger.ap e.backtrace
    raise e
  end
  response
end

#configure_logger(options) ⇒ Object



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

def configure_logger options
  options = {log_file: STDOUT}.merge options
  log_stream = options[:log_file]
  @logger = Logger.new log_stream
  @logger.level = Pact.configuration.logger.level

  if log_stream.is_a? File
     File.absolute_path(log_stream).gsub(Dir.pwd + "/", '')
  else
    "standard out/err"
  end
end

#to_sObject



54
55
56
# File 'lib/pact/consumer/mock_service/app.rb', line 54

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