class Pug::MessageHandler

Coordinates between messages provided from Bot to actions via the Controller

Public Class Methods

default(actions) click to toggle source

A convenice initializer for provided actions @param actions [Array<Interfaces::Action>] actions to coordinate @return [MessageHandler] A new instance of MessageHandler

# File lib/pug/message_handler.rb, line 10
def self.default(actions)
  controller = Action::Controller.new(actions)
  keyword_handler = KeywordHandler.new(actions)
  MessageHandler.new(controller, NumberParser.new, keyword_handler)
end
new(controller, parser, keyword_handler) click to toggle source

@param controller [Action::Controller] to control Action flow @param parser [NumberParser] to parse numeric text @param keyword_handler [KeywordHandler] to handle keywords

# File lib/pug/message_handler.rb, line 19
def initialize(controller, parser, keyword_handler)
  @controller = controller
  @parser = parser
  @keyword_handler = keyword_handler
end

Public Instance Methods

handle(message) click to toggle source

Parses and coordinates the given message with the Controller @param message [String] message to handle @return [Types::Result] result of handling message

# File lib/pug/message_handler.rb, line 28
def handle(message)
  return Results.missing_actions unless @controller.actions?
  return Results.unknown_input if message.to_s.empty?
  if @controller.running_action?
    run_action_with_inputs(message)
  elsif @keyword_handler.keyword?(message)
    handle_keyword(message)
  else
    parse_message_for_index(message)
  end
end

Private Instance Methods

can_start_action?(index) click to toggle source
# File lib/pug/message_handler.rb, line 42
def can_start_action?(index)
  @controller.can_start_action?(index)
end
handle_keyword(keyword) click to toggle source
# File lib/pug/message_handler.rb, line 70
def handle_keyword(keyword)
  response = @keyword_handler.run_command_for_keyword(keyword)
  return Results.unknown_input if response.nil?
  Types::Result.info(response)
end
parse_message_for_index(message) click to toggle source
# File lib/pug/message_handler.rb, line 50
def parse_message_for_index(message)
  index = @parser.number_from_text(message)
  return Results.unknown_input if index.nil?
  return Results.invalid_index(index) unless can_start_action?(index)

  @controller.start_action(index)
  request_input_if_needed(message)
end
request_input_if_needed(message) click to toggle source
# File lib/pug/message_handler.rb, line 59
def request_input_if_needed(message)
  input = @controller.action_input
  if input.required?
    Results.enter_inputs(input.action_name)
  else
    run_action_with_inputs(message)
  end
rescue RuntimeError => ex
  Types::Result.error(ex)
end
run_action_with_inputs(inputs = nil) click to toggle source
# File lib/pug/message_handler.rb, line 46
def run_action_with_inputs(inputs = nil)
  @controller.run_action(inputs || '')
end