class MosEisley::Handler

Attributes

name[R]
type[R]

Public Class Methods

add(type, name = nil, &block) click to toggle source

Call as often as necessary to add handlers with blocks; each call creates a MosEisley::Handler object @param type [Symbol] :action | :command | :event | :menu @param name [String]

# File lib/handler.rb, line 24
def self.add(type, name = nil, &block)
  @handlers ||= {
    action: [],
    command: [],
    event: [],
    menu: [],
  }
  @handlers[type] << MosEisley::Handler.new(type, name, &block)
  MosEisley.logger.debug("Added #{type} handler: #{@handlers[type].last}")
end
command_acks() click to toggle source

Example: {'/command' => {response_type: 'ephemeral', text: nil}} @return [Hash<String, Hash>] commands to acknowledge

# File lib/handler.rb, line 37
def self.command_acks
  @command_acks ||= {}
end
handlers() click to toggle source

@return [Hash<Symbol, Array>] containing all the handlers

# File lib/handler.rb, line 42
def self.handlers
  @handlers
end
import() click to toggle source

Import handlers from designated directory

# File lib/handler.rb, line 8
def self.import
  path = File.expand_path('./handlers')
  import_from_path(path)
end
import_from_path(path) click to toggle source

Import handlers from a directory @param path [String] directory name

# File lib/handler.rb, line 15
def self.import_from_path(path)
  Dir.chdir(path) {
    Dir.foreach('.') { |f| load f unless File.directory?(f) }
  }
end
new(t, n = nil, &block) click to toggle source
# File lib/handler.rb, line 64
def initialize(t, n = nil, &block)
  @type = t
  @name = n
  @block = block
  @stopped = false
end
run(type, event) click to toggle source

Run the handlers, typically called by the server @param event [Hash] from Slack Events API JSON data

# File lib/handler.rb, line 48
def self.run(type, event)
  logger = MosEisley.logger
  response = nil
  @handlers[type].each do |h|
    response = h.run(event)
    if h.stopped?
      logger.debug('Handler stop was requested.')
      break
    end
  end
  logger.info("Done running #{type} handlers.")
  response
end

Public Instance Methods

run(event) click to toggle source
# File lib/handler.rb, line 71
def run(event)
  logger = MosEisley.logger
  logger.warn("No block to execute for #{@type} handler: #{self}") unless @block
  logger.debug("Running #{@type} handler: #{self}")
  @stopped = false
  @block.call(event, self)
rescue => e
  logger.error(e.message)
  logger.error(e.backtrace.join("\n"))
  {text: "Woops, encountered an error."}
end
stop() click to toggle source
# File lib/handler.rb, line 83
def stop
  @stopped = true
end
stopped?() click to toggle source
# File lib/handler.rb, line 87
def stopped?
  @stopped
end
to_s() click to toggle source
# File lib/handler.rb, line 91
def to_s
  "#<#{self.class}:#{self.object_id.to_s(16)}(#{name})>"
end