class Slappy::Client

Attributes

start_time[R]

Public Class Methods

new() click to toggle source
# File lib/slappy/client.rb, line 7
def initialize
  Slack.configure { |slack| slack.token = config.token }
  @callbacks = {}
end

Public Instance Methods

client() click to toggle source
# File lib/slappy/client.rb, line 12
def client
  @client ||= Slack.realtime
end
goodnight(&block) click to toggle source
# File lib/slappy/client.rb, line 35
def goodnight(&block)
  register_callback(:goodnight, :goodnight, block)
end
hear(pattern, options = {}, &block) click to toggle source
# 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) click to toggle source
# File lib/slappy/client.rb, line 31
def hello(&block)
  register_callback(:hello, :hello, block)
end
monitor(type, options = {}, &block) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# File lib/slappy/client.rb, line 54
def say(text, options = {})
  options[:text] = text
  Messenger.new(options).message
end
schedule(pattern, options = {}, &block) click to toggle source
# 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
start() click to toggle source
# 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

Private Instance Methods

config() click to toggle source
# File lib/slappy/client.rb, line 109
def config
  Slappy.configuration
end
register_callback(name, type, callback) click to toggle source
# File lib/slappy/client.rb, line 77
def register_callback(name, type, callback)
  @callbacks[type] ||= []
  @callbacks[type].push callback
  Debug.log "Add #{name} event(#{@callbacks[type.to_sym].size}): #{type}"
end
register_event(event_name, listeners) click to toggle source
# File lib/slappy/client.rb, line 95
def register_event(event_name, listeners)
  client.on event_name do |data|
    listeners.each do |listener|
      case event_name
      when :hello
        listener.call
      else
        event = Event.new(data)
        listener.call(event)
      end
    end
  end
end
set_signal_trap() click to toggle source
# File lib/slappy/client.rb, line 83
def set_signal_trap
  [:TERM, :INT].each do |signal|
    Signal.trap(signal) do
      @callbacks[:goodnight].try(:each) do |callback|
        th = Thread.new { callback.call }
        th.join
      end
      EventMachine.stop
    end
  end
end
setup() click to toggle source
# File lib/slappy/client.rb, line 67
def setup
  @start_time = Time.now

  @callbacks.each do |event_name, listeners|
    register_event event_name, listeners
  end

  set_signal_trap
end