Class: RABX::Message
- Inherits:
-
Object
- Object
- RABX::Message
- Defined in:
- lib/rabx/message.rb,
lib/rabx/message/version.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Error, InterfaceError, ProtocolError
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.dump(type, *args) ⇒ String
Dumps a RABX message.
-
.load(s) ⇒ Message
Loads a RABX message.
Instance Method Summary collapse
-
#eof? ⇒ Boolean
Returns whether the reader has reached the end-of-file.
-
#getnetstring ⇒ Netstring?
Loads a netstring from the current position in the message.
-
#gets ⇒ Object
Loads a typed netstring from the current position in the message.
-
#initialize(s) ⇒ Message
constructor
A new instance of Message.
-
#to_s ⇒ String
Returns the RABX message.
-
#type ⇒ String
Returns the message type.
Constructor Details
#initialize(s) ⇒ Message
Returns a new instance of Message.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rabx/message.rb', line 49 def initialize(s) if s.empty? raise ProtocolError, 'string is too short' end @s = s.dup # string @p = 1 # pointer @size = @s.size @version = getnetstring parse end |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
46 47 48 |
# File 'lib/rabx/message.rb', line 46 def version @version end |
Class Method Details
.dump(type, *args) ⇒ String
Dumps a RABX message.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rabx/message.rb', line 14 def self.dump(type, *args) string = type + Netstring.dump('0') if type == 'R' string + Netstring.dump(args[0]) + _dump(args[1]) elsif type == 'S' if args.size.zero? string else string + _dump(args[0]) end elsif type == 'E' string + Netstring.dump(args[0].to_s) + Netstring.dump(args[1]) + _dump(args[2]) end end |
.load(s) ⇒ Message
Loads a RABX message.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rabx/message.rb', line 33 def self.load(s) case s[0] when 'R' RequestMessage.new(s) when 'S' SuccessMessage.new(s) when 'E' ErrorMessage.new(s) else raise ProtocolError, %(unknown RABX message type "#{s[0]}") end end |
Instance Method Details
#eof? ⇒ Boolean
Returns whether the reader has reached the end-of-file.
77 78 79 |
# File 'lib/rabx/message.rb', line 77 def eof? @p == @size end |
#getnetstring ⇒ Netstring?
Loads a netstring from the current position in the message.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rabx/message.rb', line 84 def getnetstring if eof? nil else string = Netstring.load(@s[@p..-1]) @p += string.netstring.size string end rescue Netstring::Error => e raise ProtocolError, "#{e.message} in netstring starting at #{@p}" end |
#gets ⇒ Object
Loads a typed netstring from the current position in the message.
99 100 101 102 103 104 105 106 107 108 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rabx/message.rb', line 99 def gets if eof? nil else type = @s[@p] @p += 1 if type == 'N' # null return nil end case type when 'I' # integer value = getnetstring.to_s begin Integer(value) rescue ArgumentError raise ProtocolError, %(expected integer, got #{value.inspect} at position #{@p - value.size - 1}) end when 'R' # real value = getnetstring.to_s begin Float(value) rescue ArgumentError raise ProtocolError, %(expected float, got #{value.inspect} at position #{@p - value.size - 1}) end when 'T', 'B' # text, binary getnetstring when 'L' # list value = getnetstring.to_s begin size = Integer(value) rescue ArgumentError raise ProtocolError, %(expected integer, got #{value.inspect} at position #{@p - value.size - 1}) end array = [] size.times do |n| if eof? raise ProtocolError, %(expected #{size} items, got #{n} items at position #{@p}) end array << gets end array when 'A' # associative array value = getnetstring.to_s begin size = Integer(value) rescue ArgumentError raise ProtocolError, %(expected integer, got #{value.inspect} at position #{@p - value.size - 1}) end hash = {} size.times do |n| if eof? raise ProtocolError, %(expected #{size} items, got #{n} items at position #{@p}) end key = gets # test for repeated keys? if eof? raise ProtocolError, %(expected value, got end-of-file at position #{@p}) end hash[key] = gets end hash else raise ProtocolError, %(bad type character "#{type}" at position #{@p}) end end end |
#to_s ⇒ String
Returns the RABX message.
63 64 65 |
# File 'lib/rabx/message.rb', line 63 def to_s @s end |
#type ⇒ String
Returns the message type.
70 71 72 |
# File 'lib/rabx/message.rb', line 70 def type @s[0] end |