Sha256: 7a1ca63926c07cbbf166546a77bd8a450728f6496d58e3a2899a731d4fe71018

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 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
      handle
      adapt
    end

    def receive(attributes)
      message = Message.new(attributes.merge(robot: self))
      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

    def handle
      handlers
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ellen-0.2.8 lib/ellen/robot.rb
ellen-0.2.7 lib/ellen/robot.rb
ellen-0.2.6 lib/ellen/robot.rb
ellen-0.2.5 lib/ellen/robot.rb
ellen-0.2.4 lib/ellen/robot.rb
ellen-0.2.3 lib/ellen/robot.rb
ellen-0.2.2 lib/ellen/robot.rb
ellen-0.2.1 lib/ellen/robot.rb
ellen-0.2.0 lib/ellen/robot.rb