Class: MosEisley::Handler
- Inherits:
-
Object
- Object
- MosEisley::Handler
- Defined in:
- lib/handler.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.add(type, name = nil, &block) ⇒ Object
Call as often as necessary to add handlers with blocks; each call creates a MosEisley::Handler object.
-
.command_acks ⇒ Hash<String, Hash>
Example: => {response_type: ‘ephemeral’, text: nil}.
-
.handlers ⇒ Hash<Symbol, Array>
Containing all the handlers.
-
.import ⇒ Object
Import handlers from designated directory.
-
.import_from_path(path) ⇒ Object
Import handlers from a directory.
-
.run(type, event) ⇒ Object
Run the handlers, typically called by the server.
Instance Method Summary collapse
-
#initialize(t, n = nil, &block) ⇒ Handler
constructor
A new instance of Handler.
- #run(event) ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #to_s ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
62 63 64 |
# File 'lib/handler.rb', line 62 def name @name end |
#type ⇒ Object (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
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_acks ⇒ Hash<String, Hash>
Example: => {response_type: ‘ephemeral’, text: nil}
37 38 39 |
# File 'lib/handler.rb', line 37 def self.command_acks @command_acks ||= {} end |
.handlers ⇒ Hash<Symbol, Array>
Returns containing all the handlers.
42 43 44 |
# File 'lib/handler.rb', line 42 def self.handlers @handlers end |
.import ⇒ Object
Import handlers from designated directory
8 9 10 11 |
# File 'lib/handler.rb', line 8 def self.import path = File.('./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
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.) logger.error(e.backtrace.join("\n")) {text: "Woops, encountered an error."} end |
#stop ⇒ Object
83 84 85 |
# File 'lib/handler.rb', line 83 def stop @stopped = true end |
#stopped? ⇒ Boolean
87 88 89 |
# File 'lib/handler.rb', line 87 def stopped? @stopped end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/handler.rb', line 91 def to_s "#<#{self.class}:#{self.object_id.to_s(16)}(#{name})>" end |