lib/amqp/client/table.rb in amqp-client-1.0.2 vs lib/amqp/client/table.rb in amqp-client-1.1.0
- old
+ new
@@ -3,15 +3,13 @@
module AMQP
class Client
# Encode and decode an AMQP table to/from hash, only used internally
# @api private
module Table
- module_function
-
# Encodes a hash into a byte array
# @return [String] Byte array
- def encode(hash)
+ def self.encode(hash)
tbl = StringIO.new
hash.each do |k, v|
key = k.to_s
tbl.write [key.bytesize, key].pack("Ca*")
tbl.write encode_field(v)
@@ -19,15 +17,15 @@
tbl.string
end
# Decodes an AMQP table into a hash
# @return [Hash<String, Object>]
- def decode(bytes)
+ def self.decode(bytes)
hash = {}
pos = 0
while pos < bytes.bytesize
- key_len = bytes[pos].ord
+ key_len = bytes.getbyte(pos)
pos += 1
key = bytes.byteslice(pos, key_len).force_encoding("utf-8")
pos += key_len
rest = bytes.byteslice(pos, bytes.bytesize - pos)
len, value = decode_field(rest)
@@ -37,11 +35,11 @@
hash
end
# Encoding a single value in a table
# @api private
- def encode_field(value)
+ def self.encode_field(value)
case value
when Integer
if value > 2**31
["l", value].pack("a q>")
else
@@ -70,11 +68,11 @@
end
# Decodes a single value
# @return [Array<Integer, Object>] Bytes read and the parsed object
# @api private
- def decode_field(bytes)
+ def self.decode_field(bytes)
type = bytes[0]
pos = 1
case type
when "S"
len = bytes.byteslice(pos, 4).unpack1("L>")
@@ -92,11 +90,11 @@
pos += length + 1
a << value
end
[4 + len, a]
when "t"
- [1, bytes[pos].ord == 1]
+ [1, bytes.getbyte(pos) == 1]
when "b"
[1, bytes.byteslice(pos, 1).unpack1("c")]
when "B"
[1, bytes.byteslice(pos, 1).unpack1("C")]
when "s"
@@ -112,10 +110,10 @@
when "f"
[4, bytes.byteslice(pos, 4).unpack1("g")]
when "d"
[8, bytes.byteslice(pos, 8).unpack1("G")]
when "D"
- scale = bytes[pos].ord
+ scale = bytes.getbyte(pos)
pos += 1
value = bytes.byteslice(pos, 4).unpack1("L>")
d = value / 10**scale
[5, d]
when "x"