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) ⇒ 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

#channelsObject (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

Parameters:

  • value

    the value to set the attribute preview to.



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.

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:



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.
  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?

  chats = channel_config.chat_ids
  connector = channel_config.connector
  if preview?
    connector.preview(chats, text, message_config)
  else
    chats.map { |chat| connector.send_message(chat, text, message_config) }
  end
end

#message?(channel_id, msg_id) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/pechkin/handler.rb', line 43

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

#preview?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/pechkin/handler.rb', line 47

def preview?
  @preview
end