Class: Qbot::Base

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

Direct Known Subclasses

Cron, Echo, Help, Ping

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, &block) ⇒ Base

Returns a new instance of Base.



47
48
49
50
# File 'lib/qbot/base.rb', line 47

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

.usage(text) ⇒ Object



25
26
27
# File 'lib/qbot/base.rb', line 25

def usage(text)
  on(/^#{prefix}help\s+#{name.downcase}\b/) { post(text) }
end

Instance Method Details

#cacheObject



63
64
65
# File 'lib/qbot/base.rb', line 63

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

#call(message) ⇒ Object



52
53
54
55
56
57
# File 'lib/qbot/base.rb', line 52

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

  instance_exec($~, &@callback)
end

#post(text, **options) ⇒ Object



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

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