Class: MosEisley::Handler

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(t, n = nil, &block) ⇒ Handler

Returns a new instance of Handler.



64
65
66
67
68
69
# File 'lib/handler.rb', line 64

def initialize(t, n = nil, &block)
  @type = t
  @name = n
  @block = block
  @stopped = false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



62
63
64
# File 'lib/handler.rb', line 62

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



62
63
64
# File 'lib/handler.rb', line 62

def type
  @type
end

Class Method Details

.add(type, name = nil, &block) ⇒ Object

Call as often as necessary to add handlers with blocks; each call creates a MosEisley::Handler object

Parameters:

  • type (Symbol)

    :action | :command | :event | :menu

  • name (String) (defaults to: nil)


24
25
26
27
28
29
30
31
32
33
# 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_acksHash<String, Hash>

Example: => {response_type: ‘ephemeral’, text: nil}

Returns:

  • (Hash<String, Hash>)

    commands to acknowledge



37
38
39
# File 'lib/handler.rb', line 37

def self.command_acks
  @command_acks ||= {}
end

.handlersHash<Symbol, Array>

Returns containing all the handlers.

Returns:

  • (Hash<Symbol, Array>)

    containing all the handlers



42
43
44
# File 'lib/handler.rb', line 42

def self.handlers
  @handlers
end

.importObject

Import handlers from designated directory



8
9
10
11
# File 'lib/handler.rb', line 8

def self.import
  path = File.expand_path('./handlers')
  import_from_path(path)
end

.import_from_path(path) ⇒ Object

Import handlers from a directory

Parameters:

  • path (String)

    directory name



15
16
17
18
19
# 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

.run(type, event) ⇒ Object

Run the handlers, typically called by the server

Parameters:

  • event (Hash)

    from Slack Events API JSON data



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#run(event) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# 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

#stopObject



83
84
85
# File 'lib/handler.rb', line 83

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/handler.rb', line 87

def stopped?
  @stopped
end

#to_sObject



91
92
93
# File 'lib/handler.rb', line 91

def to_s
  "#<#{self.class}:#{self.object_id.to_s(16)}(#{name})>"
end