Class: Connfu::ConnfuMessageFormatter

Inherits:
Object
  • Object
show all
Includes:
ConnfuLogger
Defined in:
lib/connfu/connfu_message_formatter.rb

Overview

This class is in charge of formatting the incoming messages It’s used by any connFu listener to format a raw message got from the streaming API to a Connfu::Message instance

Class Method Summary collapse

Methods included from ConnfuLogger

included, #logger

Class Method Details

.format_message(message) ⇒ Object

Convert an inbound twitter/RSS event to a Message instance.

Parameters

  • message raw JSON decoded message

Return

Array of Connfu::Message instances



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/connfu/connfu_message_formatter.rb', line 26

def format_message(message)
  message.is_a?(String) and message = ActiveSupport::JSON.decode(message)
  values = []

  # internal method that process the message
  fetch_values = lambda { |msg|
    begin
      if msg.is_a?(Hash)
        sender = msg["actor"]["id"]
        recipients = nil
        user_mentions = msg["object"]["entities"]["user_mentions"]
        logger.debug(user_mentions)
        if user_mentions.is_a?(Array) && user_mentions.length > 0
          recipients = []
          user_mentions.each { |recipient|
            recipients << recipient
          }
          if recipients.length.eql?(1)
            recipients = recipients[0]
          end
        end
        channel_info = msg["backchat"]["bare_uri"].split(":")
        logger.debug(channel_info)
        params = {
            :id => msg["object"]["id"],
            :content => msg["object"]["content"],
            :message_type => "new",
            :channel_type => channel_info[0],
            :channel_name => channel_info[1][2..-2],
            :from => sender,
            :to => recipients
        }
        params
      else
        logger.error("Invalid message format: #{msg}")
        nil
      end
    rescue => ex
      logger.error("Unable to fetch values from message #{msg}. Caught exception #{ex}")
      nil
    end
  }

  if message.is_a?(Array)
    message.each { |msg|
      if msg.respond_to?("has_key?") && msg.has_key?("backchat") && msg["backchat"].has_key?("source")
        case msg["backchat"]["source"].downcase
        when "twitter"
          params = fetch_values.call(msg)
        when "webfeed"
          params = format_rss_message(msg)
        else
          params = nil
        end
        # include the message if valid params
        params.nil? or values << Connfu::Message.new(params)
      end
    }
  else
    if message.respond_to?("has_key?") && message.has_key?("backchat") && message["backchat"].has_key?("source")
      case message["backchat"]["source"].downcase
      when "twitter"
        params = fetch_values.call(message)
      when "webfeed"
        params = format_rss_message(message)
      else
        params = nil
      end
      # include the message if valid params
      params.nil? or values << Connfu::Message.new(params)
    end
  end
  values
end

.format_rss_message(msg) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/connfu/connfu_message_formatter.rb', line 150

def format_rss_message(msg)
  begin
    if msg.is_a?(Hash)
      params = {
          :id => msg["object"]["id"],
          :content => msg["title"],
          :message_type => "new",
          :channel_type => "rss",
          :channel_name => msg["backchat"]["bare_uri"],
          :from => msg["actor"]["id"],
          :to => []
      }
      params
    else
      logger.error("Invalid message format: #{msg}")
      nil
    end
  rescue => ex
    logger.error("Unable to fetch values from message #{msg}. Caught exception #{ex}")
    nil
  end
end

.format_voice_sms(message) ⇒ Object

Convert the inbound voice/sms event to a Message instance

Parameters

  • message raw JSON decoded message

Return

  • Array of one Connfu::Message instance

  • Empty array if the message is invalid



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/connfu/connfu_message_formatter.rb', line 109

def format_voice_sms(message)
  if message.is_a?(Array) and message.length.eql?(2)
    if Connfu::Message.is_voice?(message[0])
      logger.debug("Format the new voice message")
      conference_id = URI.parse(message[1]["conferenceId"]).host
      params = {
          :id => 1234,
          :content => message[1]["newTopic"],
          :from => message[1]["from"],
          :to => message[1]["to"],
          :message_type => message[0],
          :channel_type => "voice",
          :channel_name => conference_id
      }
      logger.debug(params)

      [Connfu::Message.new(params)]
    elsif Connfu::Message.is_sms?(message[0])
      logger.debug("Format the new sms message")
      params = {
          :id => 1234,
          :content => message[1]["message"],
          :from => message[1]["from"],
          :to => message[1]["to"],
          :message_type => "new",
          :channel_type => "sms",
          :channel_name => message[1]["appId"]
      }
      [Connfu::Message.new(params)]
    else
      logger.error("Unexpected message type")
      logger.error(message)
      []
    end
  else
    logger.error("Unexpected message format")
    logger.error(message)
    []
  end
end