Sha256: 01253f81b5fed5bbca50f1380ac387b9c6dedd9dcb4fd5fd0939bcb192f3d840

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Ruboty
  class Robot
    DEFAULT_ENV = "development"
    DEFAULT_ROBOT_NAME = "ruboty"

    include Mem

    delegate :say, to: :adapter

    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    def run
      dotenv
      bundle
      setup
      remember
      handle
      adapt
    end

    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

    # @return [true] Because it needs to tell that an action is matched.
    undef :say
    def say(*args)
      adapter.say(*args)
      true
    end

    # ROBOT_NAME is deprecated.
    def name
      ENV["RUBOTY_NAME"] || ENV["ROBOT_NAME"] || DEFAULT_ROBOT_NAME
    end

    def brain
      Brains::Base.find_class.new
    end
    memoize :brain

    private

    def adapt
      adapter.run
    end

    def adapter
      AdapterBuilder.new(self).build
    end
    memoize :adapter

    def bundle
      Bundler.require(:default, env)
    rescue Bundler::GemfileNotFound
    end

    def env
      ENV["RUBOTY_ENV"] || DEFAULT_ENV
    end
    memoize :env

    def dotenv
      Dotenv.load if options[:dotenv]
    end

    def setup
      load(options[:load]) if options[:load]
    end

    def handlers
      Ruboty.handlers.map { |handler_class| handler_class.new(self) }
    end
    memoize :handlers

    def remember
      brain
    end

    def handle
      handlers
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruboty-1.2.0 lib/ruboty/robot.rb
ruboty-1.1.9 lib/ruboty/robot.rb
ruboty-1.1.8 lib/ruboty/robot.rb
ruboty-1.1.7 lib/ruboty/robot.rb
ruboty-1.1.6 lib/ruboty/robot.rb
ruboty-1.1.5 lib/ruboty/robot.rb