class Pechkin::Handler

Processes feeded data chunks and sends them via connectors to needed IM services. Can skip some requests acording to filters.

Attributes

channels[R]
logger[RW]
message_matcher[R]

Public Class Methods

new(channels, stdout = STDOUT, stderr = STDERR) click to toggle source
# 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

Public Instance Methods

handle(channel_id, msg_id, data) click to toggle source

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.

@param channel_id [String] channel name from configuration. This name is

obtained from directory structure we have in configuration directory.

@param msg_id [String] message name from configuration. This name is

references yml file with message description

@param data [Object] data object to render via template. This is usualy

deserialized json.

@see Configuration

# 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) click to toggle source
# 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) click to toggle source

Executes message handling and renders template using connector logic

@param channel_id [String] channel name from configuration. This name is

obtained from directory structure we have in configuration directory.

@param msg_id [String] message name from configuration. This name is

references yml file with message description

@param data [Object] data object to render via template. This is usualy

deserialized json.

@see Configuration

# 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

Private Instance Methods

fetch_channel(channel_id) click to toggle source

Find channel by it's id or trow ChannelNotFoundError

# File lib/pechkin/handler.rb, line 77
def fetch_channel(channel_id)
  raise ChannelNotFoundError, channel_id unless channels.key?(channel_id)

  channels[channel_id]
end
fetch_message(channel_config, msg_id) click to toggle source

Find message config by it's id or throw MessageNotFoundError

# File lib/pechkin/handler.rb, line 84
def fetch_message(channel_config, msg_id)
  message_list = channel_config.messages
  raise MessageNotFoundError, msg_id unless message_list.key?(msg_id)

  message_list[msg_id]
end
message_allowed?(message_config, data) click to toggle source
# File lib/pechkin/handler.rb, line 91
def message_allowed?(message_config, data)
  message_matcher.matches?(message_config, data)
end
prepare_message(channel_id, msg_id, data) click to toggle source
# File lib/pechkin/handler.rb, line 95
def prepare_message(channel_id, msg_id, data)
  channel_config = fetch_channel(channel_id)
  # Find message and try substitute values to message parameters.
  message_config = substitute(data, fetch_message(channel_config, msg_id))

  data = (message_config['variables'] || {}).merge(data)
  template = message_config['template']

  text = ''
  text = template.render(data) unless template.nil?

  [channel_config, message_config, text]
end
puts(msg) click to toggle source
# File lib/pechkin/handler.rb, line 72
def puts(msg)
  @stdout.puts(msg)
end
substitute(data, message_desc) click to toggle source
# File lib/pechkin/handler.rb, line 109
def substitute(data, message_desc)
  substitute_recursive(Substitute.new(data), message_desc)
end
substitute_recursive(substitutions, object) click to toggle source
# File lib/pechkin/handler.rb, line 113
def substitute_recursive(substitutions, object)
  case object
  when String
    substitutions.process(object)
  when Array
    object.map { |o| substitute_recursive(substitutions, o) }
  when Hash
    r = {}
    object.each { |k, v| r[k] = substitute_recursive(substitutions, v) }
    r
  else
    object
  end
end