class Pione::Notification::Message
Constants
- PROTOCOL_VERSION
Attributes
notifier[R]
type[R]
version[R]
Public Class Methods
load(data)
click to toggle source
Load a notification from the dumped data.
@param data [String]
dumped data
@return [Network::Notification]
notification object
# File lib/pione/notification/message.rb, line 12 def self.load(data) io = StringIO.new(data) # read total = io.read(2).unpack("n")[0] psize = io.read(1).unpack("C")[0] version = io.read(psize) nsize = io.read(1).unpack("C")[0] notifier = io.read(nsize) tsize = io.read(1).unpack("C")[0] type = io.read(tsize) msize = io.read(2).unpack("n")[0] json = io.read(msize) # check size unless total == psize + nsize + tsize + msize raise MessageError.new("Total size and real message size are different.") end # convert json to ruby object begin content = JSON.parse(json) rescue JSON::ParserError MessageError.new("Format of the message content is invalid.") end new(notifier, type, content) end
new(notifier, type, content, version=PROTOCOL_VERSION)
click to toggle source
@param notifier [String]
name of notification sender
@param type [String]
notification type name
@param message [Hash]
JSON data
@param version [String]
protocol version
# File lib/pione/notification/message.rb, line 53 def initialize(notifier, type, content, version=PROTOCOL_VERSION) @notifier = notifier @type = type @content = content @version = version end
Public Instance Methods
[](name)
click to toggle source
Return the value.
@param name [String]
key of message content
@return [Object]
the value
# File lib/pione/notification/message.rb, line 66 def [](name) @content[name] end
dump()
click to toggle source
Dump the notification message to a serialized string.
@return [String]
a serialized string
# File lib/pione/notification/message.rb, line 74 def dump json = JSON.generate(@content) psize = PROTOCOL_VERSION.bytesize nsize = @notifier.bytesize tsize = @type.bytesize msize = json.bytesize total = psize + nsize + tsize + msize if nsize > 255 raise MessageError.new('notifier "%s" is too long.' % @notifier) end if tsize > 255 raise MessageError.new('type "%s" is too long.' % @type) end if msize > 65335 raise MessageError.new('message is too long.') end if total > 65535 raise MessageError.new('The notification is too long.') end # build a serialized string data = "" data << [total].pack("n") data << [psize].pack("C") << PROTOCOL_VERSION data << [nsize].pack("C") << @notifier data << [tsize].pack("C") << @type data << [msize].pack("n") << json end