class Pug::Bot
Coordinates the Client with the provided Actions
Public Class Methods
new(client, actions)
click to toggle source
@param client [Interfaces::Client] used to interact with User @param actions [Array<Interfaces::Action>] available actions
# File lib/pug/bot.rb, line 19 def initialize(client, actions) @client = client @handler = MessageHandler.default(actions) end
run()
click to toggle source
Convenience method used to setup the Bot
with the user defined Configuration
@return [Bot] A new instance of Bot
. @see Configuration
# File lib/pug/bot.rb, line 10 def self.run config = Pug.configuration config.validate client = Clients::Factory.client_for_config(config) Bot.new(client, config.actions).start end
Public Instance Methods
start()
click to toggle source
Starts the handling of all messages received via the Client @return [void]
# File lib/pug/bot.rb, line 26 def start @client.listen do |message| handle(message) end end
Private Instance Methods
handle(message)
click to toggle source
# File lib/pug/bot.rb, line 34 def handle(message) result = @handler.handle(message) output = output_from_result(result) @client.send_message(output) unless output.to_s.empty? return if result.type != Types::Result::SUCCESS action_name = result.value.action_name @client.send_message(Strings.finished_running(action_name)) end
output_from_result(result)
click to toggle source
# File lib/pug/bot.rb, line 43 def output_from_result(result) if result.type == Types::Result::SUCCESS result.value.value elsif result.type == Types::Result::INFO result.value else result.error end end