Class: Connfu::ConnfuMessageFormatter
- Inherits:
-
Object
- Object
- Connfu::ConnfuMessageFormatter
- 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
-
.format_message(message) ⇒ Object
Convert an inbound twitter/RSS event to a Message instance.
- .format_rss_message(msg) ⇒ Object
-
.format_voice_sms(message) ⇒ Object
Convert the inbound voice/sms event to a Message instance ==== Parameters *
messageraw JSON decoded message.
Methods included from ConnfuLogger
Class Method Details
.format_message(message) ⇒ Object
Convert an inbound twitter/RSS event to a Message instance.
Parameters
-
messageraw 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 () .is_a?(String) and = ActiveSupport::JSON.decode() 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 .is_a?(Array) .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 = (msg) else params = nil end # include the message if valid params params.nil? or values << Connfu::Message.new(params) end } else if .respond_to?("has_key?") && .has_key?("backchat") && ["backchat"].has_key?("source") case ["backchat"]["source"].downcase when "twitter" params = fetch_values.call() when "webfeed" params = () 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 (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
-
messageraw 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() if .is_a?(Array) and .length.eql?(2) if Connfu::Message.is_voice?([0]) logger.debug("Format the new voice message") conference_id = URI.parse([1]["conferenceId"]).host params = { :id => 1234, :content => [1]["newTopic"], :from => [1]["from"], :to => [1]["to"], :message_type => [0], :channel_type => "voice", :channel_name => conference_id } logger.debug(params) [Connfu::Message.new(params)] elsif Connfu::Message.is_sms?([0]) logger.debug("Format the new sms message") params = { :id => 1234, :content => [1]["message"], :from => [1]["from"], :to => [1]["to"], :message_type => "new", :channel_type => "sms", :channel_name => [1]["appId"] } [Connfu::Message.new(params)] else logger.error("Unexpected message type") logger.error() [] end else logger.error("Unexpected message format") logger.error() [] end end |