Sha256: 3948387e1ccd4a5111333d5c7f3f90c127735343d82b5fdf458d4e3f51d02f32

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 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
      validate
      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, options).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 validate
      adapter.validate
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ellen-0.0.9 lib/ellen/robot.rb
ellen-0.0.8 lib/ellen/robot.rb