Class: RJR::MessageUtil
Overview
Helper utilities for messages
Class Method Summary collapse
-
.clear ⇒ Object
Clear preformatted messages.
-
.message(id, msg = nil) ⇒ String
Mechanism to register / retrieve preformatted message.
-
.rand_msg(transport = nil) ⇒ Object
Return random message from registry.
-
.retrieve_json(data) ⇒ Object
Retrieve and return a single json message from a data string.
Class Method Details
.clear ⇒ Object
Clear preformatted messages
325 326 327 |
# File 'lib/rjr/message.rb', line 325 def self.clear = {} end |
.message(id, msg = nil) ⇒ String
Mechanism to register / retrieve preformatted message
318 319 320 321 322 |
# File 'lib/rjr/message.rb', line 318 def self.(id, msg=nil) ||= {} [id] = msg unless msg.nil? [id] end |
.rand_msg(transport = nil) ⇒ Object
Return random message from registry.
Optionally specify the transport which the message must accept
(TODO turn this into a generic selection callback)
333 334 335 336 337 338 |
# File 'lib/rjr/message.rb', line 333 def self.rand_msg(transport = nil) ||= {} = .select { |mid,m| m[:transports].nil? || transport.nil? || m[:transports].include?(transport) } [.keys[rand(.keys.size)]] end |
.retrieve_json(data) ⇒ Object
Retrieve and return a single json message from a data string.
Returns the message and remaining portion of the data string, if message is found, else nil
XXX really don’t like having to do this, but a quick solution to the issue of multiple messages appearing in one tcp data packet.
TODO efficiency can probably be optimized in the case closing ‘}’ hasn’t arrived yet
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/rjr/message.rb', line 291 def self.retrieve_json(data) return nil if data.nil? || data.empty? start = 0 start += 1 until start == data.length || data[start].chr == '{' on = mi = 0 start.upto(data.length - 1).each { |i| if data[i].chr == '{' on += 1 elsif data[i].chr == '}' on -= 1 end if on == 0 mi = i break end } return nil if mi == 0 return data[start..mi], data[(mi+1)..-1] end |