Class: VkCozy::BotLabeler
- Inherits:
-
Object
- Object
- VkCozy::BotLabeler
- Defined in:
- lib/vk_cozy/framework/labeler/bot.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
Instance Method Summary collapse
- #add_handler(filter, func, type: '*') ⇒ Object
- #event_handler(filter, func) ⇒ Object
- #filter(event) ⇒ Object
- #get_handlers(type) ⇒ Object
- #handler(filter, func) ⇒ Object
-
#initialize(api) ⇒ BotLabeler
constructor
A new instance of BotLabeler.
- #message_handler(filter, func) ⇒ Object
Constructor Details
#initialize(api) ⇒ BotLabeler
Returns a new instance of BotLabeler.
33 34 35 36 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 33 def initialize(api) @api = api @handlers = {'*' => []} # * - обрабатывает события в не зависимости от их типа. end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
31 32 33 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 31 def api @api end |
Instance Method Details
#add_handler(filter, func, type: '*') ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 54 def add_handler(filter, func, type: '*') if func.is_a?(Symbol) func = method(func) end if @handlers[type].nil? @handlers[type] = [] end @handlers[type] << Bothandler.new(filter, func) end |
#event_handler(filter, func) ⇒ Object
66 67 68 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 66 def event_handler(filter, func) add_handler(filter, func, type: 'message_event') end |
#filter(event) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 46 def filter(event) for handler in get_handlers(event.type) + get_handlers('*') if handler.check(event) return true end end end |
#get_handlers(type) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 38 def get_handlers(type) if @handlers[type].nil? return [] else return @handlers[type] end end |
#handler(filter, func) ⇒ Object
74 75 76 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 74 def handler(filter, func) add_handler(filter, func) end |
#message_handler(filter, func) ⇒ Object
70 71 72 |
# File 'lib/vk_cozy/framework/labeler/bot.rb', line 70 def (filter, func) add_handler(filter, func, type: 'message_new') end |