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.



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

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.



79
80
81
# File 'lib/handler.rb', line 79

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



79
80
81
# File 'lib/handler.rb', line 79

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_response | :command | :event | :menu

  • name (String) (defaults to: nil)

    required for type = :command_response, otherwise optional



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/handler.rb', line 24

def self.add(type, name = nil, &block)
  if type == :command_response && name.nil?
    raise ArgumentError.new('Name required for :command_response.')
  end
  @handlers ||= {
    action: [],
    command_response: {},
    command: [],
    event: [],
    menu: [],
  }
  h = MosEisley::Handler.new(type, name, &block)
  if type == :command_response
    @handlers[type][name] = h 
  else
    @handlers[type] << h
  end
  MosEisley.logger.debug("Added #{type} handler: #{h}")
end

.command_acksHash<String, Hash>

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

Returns:

  • (Hash<String, Hash>)

    commands to acknowledge



46
47
48
# File 'lib/handler.rb', line 46

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

.handlersHash<Symbol, Array>

Returns containing all the handlers.

Returns:

  • (Hash<Symbol, Array>)

    containing all the handlers



51
52
53
# File 'lib/handler.rb', line 51

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/handler.rb', line 57

def self.run(type, event)
  logger = MosEisley.logger
  response = nil
  if type == :command_response
    h = @handlers[type][event[:command]]
    if h
      response = h.run(event)
      logger.info("Done running #{type} handlers.")
    end
  else
    @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.")
  end
  response
end

Instance Method Details

#run(event) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/handler.rb', line 88

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



100
101
102
# File 'lib/handler.rb', line 100

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/handler.rb', line 104

def stopped?
  @stopped
end

#to_sObject



108
109
110
# File 'lib/handler.rb', line 108

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