Class: Qbot::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/qbot/base.rb

Direct Known Subclasses

Cron, Echo, Ping

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, &block) ⇒ Base

Returns a new instance of Base.



39
40
41
42
# File 'lib/qbot/base.rb', line 39

def initialize(pattern, &block)
  @pattern  = pattern
  @callback = block
end

Class Method Details

.cron(pattern, &block) ⇒ Object



21
22
23
# File 'lib/qbot/base.rb', line 21

def cron(pattern, &block)
  schedule(pattern, &block)
end

.on(pattern, &block) ⇒ Object



16
17
18
19
# File 'lib/qbot/base.rb', line 16

def on(pattern, &block)
  pattern = Regexp.new(pattern.to_s) unless Regexp === pattern
  Qbot.app.add(new(pattern, &block))
end

Instance Method Details

#cacheObject



60
61
62
# File 'lib/qbot/base.rb', line 60

def cache
  Qbot.app.storage.namespace(self.class.name)
end

#call(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/qbot/base.rb', line 44

def call(message)
  @message = message
  return unless @pattern =~ @message.text.to_s.strip

  begin
    Qbot.app.logger.debug("#{self.class} - Recieve message: '#{message.text}'")
    instance_exec($~, &@callback)
  rescue => e
    Qbot.app.logger.error("#{self.class} - Error: #{e}")
  end
end

#post(text, **options) ⇒ Object



56
57
58
# File 'lib/qbot/base.rb', line 56

def post(text, **options)
  Qbot.app.adapter.reply_to(@message, text, **options)
end