Class: GrubY::Bot
- Inherits:
-
Object
- Object
- GrubY::Bot
- Defined in:
- lib/gruubY/bot.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #emit(event, payload = {}) ⇒ Object
-
#initialize(token, session: Session.new) ⇒ Bot
constructor
A new instance of Bot.
- #on(event, filter = nil, group: 0, &block) ⇒ Object
- #process_update(update) ⇒ Object
- #remove_handler(event, handler_id = nil, &block) ⇒ Object
- #use(&block) ⇒ Object
- #use_plugin(path) ⇒ Object
Constructor Details
#initialize(token, session: Session.new) ⇒ Bot
Returns a new instance of Bot.
13 14 15 16 17 18 |
# File 'lib/gruubY/bot.rb', line 13 def initialize(token, session: Session.new) @api = API.new(token) @dispatcher = Dispatcher.new @middleware = Middleware.new @session = session end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
11 12 13 |
# File 'lib/gruubY/bot.rb', line 11 def api @api end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
11 12 13 |
# File 'lib/gruubY/bot.rb', line 11 def session @session end |
Instance Method Details
#emit(event, payload = {}) ⇒ Object
51 52 53 54 |
# File 'lib/gruubY/bot.rb', line 51 def emit(event, payload = {}) ctx = Context.new(payload, @api, @session) @dispatcher.trigger(event, ctx) end |
#on(event, filter = nil, group: 0, &block) ⇒ Object
20 21 22 |
# File 'lib/gruubY/bot.rb', line 20 def on(event, filter=nil, group: 0, &block) @dispatcher.on(event, filter, group: group, &block) end |
#process_update(update) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gruubY/bot.rb', line 36 def process_update(update) Async.run do ctx = Context.new(update, @api, @session) @middleware.run(ctx) do @dispatcher.trigger(:raw_update, ctx) dispatch_update_events(update, ctx) end end rescue => e error_ctx = Context.new({ "error" => e }, @api, @session) @dispatcher.trigger(:error, error_ctx) warn "[BOT] process_update error: #{e.class}: #{e.}" end |
#remove_handler(event, handler_id = nil, &block) ⇒ Object
24 25 26 |
# File 'lib/gruubY/bot.rb', line 24 def remove_handler(event, handler_id = nil, &block) @dispatcher.off(event, handler_id, &block) end |
#use(&block) ⇒ Object
28 29 30 |
# File 'lib/gruubY/bot.rb', line 28 def use(&block) @middleware.use(&block) end |