Class: Pione::Notification::Message
- Inherits:
-
Object
- Object
- Pione::Notification::Message
- Defined in:
- lib/pione/notification/message.rb
Constant Summary collapse
- PROTOCOL_VERSION =
"PN1"
Instance Attribute Summary collapse
-
#notifier ⇒ Object
readonly
Returns the value of attribute notifier.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.load(data) ⇒ Network::Notification
Load a notification from the dumped data.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return the value.
-
#dump ⇒ String
Dump the notification message to a serialized string.
-
#initialize(notifier, type, content, version = PROTOCOL_VERSION) ⇒ Message
constructor
A new instance of Message.
Constructor Details
#initialize(notifier, type, content, version = PROTOCOL_VERSION) ⇒ Message
Returns a new instance of Message.
53 54 55 56 57 58 |
# 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 |
Instance Attribute Details
#notifier ⇒ Object (readonly)
Returns the value of attribute notifier.
41 42 43 |
# File 'lib/pione/notification/message.rb', line 41 def notifier @notifier end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
42 43 44 |
# File 'lib/pione/notification/message.rb', line 42 def type @type end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
43 44 45 |
# File 'lib/pione/notification/message.rb', line 43 def version @version end |
Class Method Details
.load(data) ⇒ Network::Notification
Load a notification from the dumped data.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
Instance Method Details
#[](name) ⇒ Object
Return the value.
66 67 68 |
# File 'lib/pione/notification/message.rb', line 66 def [](name) @content[name] end |
#dump ⇒ String
Dump the notification message to a serialized string.
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 100 101 102 103 104 105 106 |
# 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 |