Class: Pechkin::Handler

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/pechkin/handler.rb', line 5

def channels
  @channels
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/pechkin/handler.rb', line 6

def logger
  @logger
end

#message_matcherObject (readonly)

Returns the value of attribute message_matcher.



5
6
7
# File 'lib/pechkin/handler.rb', line 5

def message_matcher
  @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.

Parameters:

  • channel_id (String)

    channel name from configuration. This name is obtained from directory structure we have in configuration directory.

  • msg_id (String)

    message name from configuration. This name is references yml file with message description

  • data (Object)

    data object to render via template. This is usualy deserialized json.

See Also:



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, message_config, text =
    prepare_message(channel_id, msg_id, data)
  chats = channel_config.chat_ids
  connector = channel_config.connector

  if message_allowed?(message_config, data)
    chats.map { |chat| connector.send_message(chat, text, message_config) }
  else
    logger.warn "#{channel_id}/#{msg_id}: " \
                "Skip sending message. Because it's not allowed"
    []
  end
end

#message?(channel_id, msg_id) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/pechkin/handler.rb', line 66

def message?(channel_id, msg_id)
  channels.key?(channel_id) && channels[channel_id].messages.key?(msg_id)
end

#preview(channel_id, msg_id, data) ⇒ Object

Executes message handling and renders template using connector logic

Parameters:

  • channel_id (String)

    channel name from configuration. This name is obtained from directory structure we have in configuration directory.

  • msg_id (String)

    message name from configuration. This name is references yml file with message description

  • data (Object)

    data object to render via template. This is usualy deserialized json.

See Also:



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, message_config, text =
    prepare_message(channel_id, msg_id, data)
  chats = channel_config.chat_ids
  connector = channel_config.connector

  if message_allowed?(message_config, data)
    connector.preview(chats, text, message_config)
  else
    puts "No message sent beacuse it's not allowed"
  end
end