class Ruboty::Robot

Constants

DEFAULT_ENV
DEFAULT_ROBOT_NAME

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruboty/robot.rb, line 12
def initialize(options = {})
  @options = options
end

Public Instance Methods

brain() click to toggle source
# File lib/ruboty/robot.rb, line 48
def brain
  Brains::Base.find_class.new
end
name() click to toggle source

ROBOT_NAME is deprecated.

# File lib/ruboty/robot.rb, line 44
def name
  ENV["RUBOTY_NAME"] || ENV["ROBOT_NAME"] || DEFAULT_ROBOT_NAME
end
receive(attributes) click to toggle source
# File lib/ruboty/robot.rb, line 27
def receive(attributes)
  message = Message.new(attributes.merge(robot: self))
  unless handlers.inject(false) { |matched, handler| matched | handler.call(message) }
    handlers.each do |handler|
      handler.call(message, missing: true)
    end
  end
end
run() click to toggle source
# File lib/ruboty/robot.rb, line 16
def run
  daemon
  dotenv
  bundle
  setup
  pid
  remember
  handle
  adapt
end
say(*args) click to toggle source
# File lib/ruboty/robot.rb, line 38
def say(*args)
  adapter.say(*args)
  true
end

Private Instance Methods

adapt() click to toggle source
# File lib/ruboty/robot.rb, line 55
def adapt
  adapter.run
end
adapter() click to toggle source
# File lib/ruboty/robot.rb, line 59
def adapter
  AdapterBuilder.new(self).build
end
bundle() click to toggle source
# File lib/ruboty/robot.rb, line 64
def bundle
  ::Bundler.require(:default, env)
rescue ::Bundler::GemfileNotFound
end
daemon() click to toggle source
# File lib/ruboty/robot.rb, line 74
def daemon
  Process.daemon(true, false) if options[:daemon]
end
dotenv() click to toggle source
# File lib/ruboty/robot.rb, line 78
def dotenv
  Dotenv.load if options[:dotenv]
end
env() click to toggle source
# File lib/ruboty/robot.rb, line 69
def env
  ENV["RUBOTY_ENV"] || DEFAULT_ENV
end
handle() click to toggle source
# File lib/ruboty/robot.rb, line 95
def handle
  handlers
end
handlers() click to toggle source
# File lib/ruboty/robot.rb, line 86
def handlers
  Ruboty.handlers.map { |handler_class| handler_class.new(self) }
end
pid() click to toggle source
# File lib/ruboty/robot.rb, line 99
def pid
  path = options[:pid]
  if path
    File.open(path, "w") { |f| f.write(Process.pid) }
    at_exit { File.unlink(path) }
  end
end
remember() click to toggle source
# File lib/ruboty/robot.rb, line 91
def remember
  brain
end
setup() click to toggle source
# File lib/ruboty/robot.rb, line 82
def setup
  load(options[:load]) if options[:load]
end