Class: Slappy::Client

Inherits:
Object
  • Object
show all
Includes:
Debuggable
Defined in:
lib/slappy/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debuggable

included

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
10
# File 'lib/slappy/client.rb', line 7

def initialize
  Slack.configure { |slack| slack.token = config.token }
  @callbacks = {}
end

Instance Attribute Details

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/slappy/client.rb', line 5

def start_time
  @start_time
end

Instance Method Details

#clientObject



12
13
14
# File 'lib/slappy/client.rb', line 12

def client
  @client ||= Slack.realtime
end

#goodnight(&block) ⇒ Object



35
36
37
# File 'lib/slappy/client.rb', line 35

def goodnight(&block)
  register_callback(:goodnight, :goodnight, block)
end

#hear(pattern, options = {}, &block) ⇒ Object



39
40
41
# File 'lib/slappy/client.rb', line 39

def hear(pattern, options = {}, &block)
  register_callback(:hear, :message, Listener::TextListener.new(pattern, options, &block))
end

#hello(&block) ⇒ Object



31
32
33
# File 'lib/slappy/client.rb', line 31

def hello(&block)
  register_callback(:hello, :hello, block)
end

#monitor(type, options = {}, &block) ⇒ Object



50
51
52
# File 'lib/slappy/client.rb', line 50

def monitor(type, options = {}, &block)
  register_callback(:monitor, type.to_sym, Listener::TypeListener.new(type, options, &block))
end

#respond(pattern, options = {}, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/slappy/client.rb', line 43

def respond(pattern, options = {}, &block)
  bot_name = options[:bot_name] || config.robot.botname || config.robot.username

  pattern = "^#{bot_name}[[:blank:]]#{pattern}"
  register_callback(:respond, :message, Listener::TextListener.new(pattern, options, &block))
end

#say(text, options = {}) ⇒ Object



54
55
56
57
# File 'lib/slappy/client.rb', line 54

def say(text, options = {})
  options[:text] = text
  Messenger.new(options).message
end

#schedule(pattern, options = {}, &block) ⇒ Object



59
60
61
62
63
# File 'lib/slappy/client.rb', line 59

def schedule(pattern, options = {}, &block)
  @schedule ||= Schedule.new
  @schedule.register pattern, options, &block
  Debug.log "Add schedule event(#{@schedule.list.size}): #{pattern}"
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/slappy/client.rb', line 16

def start
  setup

  Debug.log 'Slappy start'

  begin
    client.start
  rescue StandardError => e
    @callbacks[:goodnight].each(&:call) if @callbacks[:goodnight]
    STDERR.puts e.backtrace.slice!(0) + ': ' + e.message
    STDERR.puts "\tfrom " + e.backtrace.join("\n\tfrom ")
    exit 1 if config.stop_with_error
  end
end