Class: Pechkin::Handler
- Inherits:
-
Object
- Object
- Pechkin::Handler
- Defined in:
- lib/pechkin/handler.rb
Overview
Processes feeded data chunks and sends them via connectors to needed IM services. Can skip some requests acording to filters.
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#preview ⇒ Object
writeonly
Sets the attribute preview.
Instance Method Summary collapse
-
#handle(channel_id, msg_id, data) ⇒ Object
Handles message request.
-
#initialize(channels) ⇒ Handler
constructor
A new instance of Handler.
- #message?(channel_id, msg_id) ⇒ Boolean
- #preview? ⇒ Boolean
Constructor Details
#initialize(channels) ⇒ Handler
Returns a new instance of Handler.
8 9 10 |
# File 'lib/pechkin/handler.rb', line 8 def initialize(channels) @channels = channels end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
5 6 7 |
# File 'lib/pechkin/handler.rb', line 5 def channels @channels end |
#preview=(value) ⇒ Object (writeonly)
Sets the attribute preview
6 7 8 |
# File 'lib/pechkin/handler.rb', line 6 def preview=(value) @preview = value end |
Instance Method Details
#handle(channel_id, msg_id, data) ⇒ Object
Handles message request. Each request has three parameters: channel id, message id, and data object. By channel id we determine where to send data, by message id we determine how to transform this data to real message.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pechkin/handler.rb', line 23 def handle(channel_id, msg_id, data) channel_config = fetch_channel(channel_id) # Find message and try substitute values to message parameters. = substitute(data, (channel_config, msg_id)) data = (['variables'] || {}).merge(data) template = ['template'] text = '' text = template.render(data) unless template.nil? chats = channel_config.chat_ids connector = channel_config.connector if preview? connector.preview(chats, text, ) else chats.map { |chat| connector.(chat, text, ) } end end |
#message?(channel_id, msg_id) ⇒ Boolean
43 44 45 |
# File 'lib/pechkin/handler.rb', line 43 def (channel_id, msg_id) channels.key?(channel_id) && channels[channel_id]..key?(msg_id) end |
#preview? ⇒ Boolean
47 48 49 |
# File 'lib/pechkin/handler.rb', line 47 def preview? @preview end |