Module: Radiator::Utils
- Included in:
- Operation, Transaction, Type::Serializer
- Defined in:
- lib/radiator/utils.rb
Instance Method Summary collapse
- #hexlify(s) ⇒ Object
- #pakArr(a) ⇒ Object
- #pakc(i) ⇒ Object
- #pakC(i) ⇒ Object
- #pakHash(h) ⇒ Object
- #pakI(i) ⇒ Object
- #pakL!(i) ⇒ Object
- #paks(i) ⇒ Object
- #pakS(i) ⇒ Object
- #pakStr(s) ⇒ Object
- #unhexlify(s) ⇒ Object
- #varint(n) ⇒ Object
Instance Method Details
#hexlify(s) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/radiator/utils.rb', line 3 def hexlify(s) a = [] if s.respond_to? :each_byte s.each_byte { |b| a << sprintf('%02X', b) } else s.each { |b| a << sprintf('%02X', b) } end a.join.downcase end |
#pakArr(a) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/radiator/utils.rb', line 39 def pakArr(a) varint(a.size) + a.map do |v| case v when Symbol then pakStr(v.to_s) when String then pakStr(v) when Integer then paks(v) when TrueClass then pakC(1) when FalseClass then pakC(0) when Array then pakArr(v) when Hash then pakHash(v) when NilClass then next else raise OperationError, "Unsupported type: #{v.class}" end end.join end |
#pakc(i) ⇒ Object
77 78 79 |
# File 'lib/radiator/utils.rb', line 77 def pakc(i) [i].pack('c') end |
#pakC(i) ⇒ Object
73 74 75 |
# File 'lib/radiator/utils.rb', line 73 def pakC(i) [i].pack('C') end |
#pakHash(h) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/radiator/utils.rb', line 56 def pakHash(h) varint(h.size) + h.map do |k, v| pakStr(k.to_s) + case v when Symbol then pakStr(v.to_s) when String then pakStr(v) when Integer then paks(v) when TrueClass then pakC(1) when FalseClass then pakC(0) when Array then pakArr(v) when Hash then pakHash(v) when NilClass then next else raise OperationError, "Unsupported type: #{v.class}" end end.join end |
#pakI(i) ⇒ Object
89 90 91 |
# File 'lib/radiator/utils.rb', line 89 def pakI(i) [i].pack('I') end |
#pakL!(i) ⇒ Object
93 94 95 |
# File 'lib/radiator/utils.rb', line 93 def pakL!(i) [i].pack('L!') end |
#paks(i) ⇒ Object
81 82 83 |
# File 'lib/radiator/utils.rb', line 81 def paks(i) [i].pack('s') end |
#pakS(i) ⇒ Object
85 86 87 |
# File 'lib/radiator/utils.rb', line 85 def pakS(i) [i].pack('S') end |
#pakStr(s) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/radiator/utils.rb', line 30 def pakStr(s) s = s.dup.force_encoding('BINARY') bytes = [] bytes << varint(s.size) bytes << s bytes.join end |
#unhexlify(s) ⇒ Object
13 14 15 |
# File 'lib/radiator/utils.rb', line 13 def unhexlify(s) s.split.pack('H*') end |
#varint(n) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/radiator/utils.rb', line 17 def varint(n) data = [] while n >= 0x80 data += [(n & 0x7f) | 0x80] n >>= 7 end data += [n] data.pack('C*') end |