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



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

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.



74
75
76
# File 'lib/handler.rb', line 74

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



74
75
76
# File 'lib/handler.rb', line 74

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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: [],
    nonslack: [],
  }
  h = MosEisley::Handler.new(type, name, &block)
  if type == :command_response
    @handlers[type][name] = h 
  else
    @handlers[type] << h
  end
  MosEisley.logger.debug("Added handler: #{h}")
end

.handlersHash<Symbol, Array>



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

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



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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/handler.rb', line 52

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/handler.rb', line 83

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



95
96
97
# File 'lib/handler.rb', line 95

def stop
  @stopped = true
end

#stopped?Boolean



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

def stopped?
  @stopped
end

#to_sObject



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

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