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.
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(channel_id, msg_id, data) ⇒ Object
Executes message handling and renders template using connector logic.
Constructor Details
#initialize(channels) ⇒ Handler
Returns a new instance of Handler.
7 8 9 |
# File 'lib/pechkin/handler.rb', line 7 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 |
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 |
# 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 chats.map { |chat| connector.(chat, text, ) } end |
#message?(channel_id, msg_id) ⇒ Boolean
66 67 68 |
# File 'lib/pechkin/handler.rb', line 66 def (channel_id, msg_id) channels.key?(channel_id) && channels[channel_id]..key?(msg_id) end |
#preview(channel_id, msg_id, data) ⇒ Object
Executes message handling and renders template using connector logic
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pechkin/handler.rb', line 49 def preview(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 connector.preview(chats, text, ) end |