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.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#message_matcher ⇒ Object
readonly
Returns the value of attribute message_matcher.
Instance Method Summary collapse
-
#handle(channel_id, msg_id, data) ⇒ Object
Handles message request.
-
#initialize(channels, stdout = $stdout, stderr = $stderr) ⇒ 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, stdout = $stdout, stderr = $stderr) ⇒ Handler
Returns a new instance of Handler.
8 9 10 11 12 13 14 15 |
# File 'lib/pechkin/handler.rb', line 8 def initialize(channels, stdout = $stdout, stderr = $stderr) @channels = channels # Create empty logger by default @logger = Logger.new(IO::NULL) @stdout = stdout @stderr = stderr @message_matcher = MessageMatcher.new(@logger) 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 |
#logger ⇒ Object
Returns the value of attribute logger.
6 7 8 |
# File 'lib/pechkin/handler.rb', line 6 def logger @logger end |
#message_matcher ⇒ Object (readonly)
Returns the value of attribute message_matcher.
5 6 7 |
# File 'lib/pechkin/handler.rb', line 5 def @message_matcher 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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pechkin/handler.rb', line 29 def handle(channel_id, msg_id, data) channel_config, , text = (channel_id, msg_id, data) chats = channel_config.chat_ids connector = channel_config.connector if (, data) chats.map { |chat| connector.(chat, text, ) } else logger.warn "#{channel_id}/#{msg_id}: " \ "Skip sending message. Because it's not allowed" [] end 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
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pechkin/handler.rb', line 53 def preview(channel_id, msg_id, data) channel_config, , text = (channel_id, msg_id, data) chats = channel_config.chat_ids connector = channel_config.connector if (, data) connector.preview(chats, text, ) else puts "No message sent beacuse it's not allowed" end end |