Sha256: b475bd19dc78a64a4bc6fa09213bf6ca5a59f46f9ca5e2ad1f7cfd8db32eaa80

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Ellen
  class Robot
    DEFAULT_ROBOT_NAME = "ellen"

    include Mem

    delegate :say, to: :adapter

    attr_reader :options

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

    def run
      dotenv
      bundle
      setup
      remember
      adapt
    end

    def receive(attributes)
      message = Message.new(attributes)
      handlers.each do |handler|
        handler.call(message)
      end
    end

    def 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
    end

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

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

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

    def remember
      brain
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ellen-0.1.1 lib/ellen/robot.rb