Module: Radiator::Utils
- Included in:
- Api, ErrorParser, Operation, Transaction, Type::Serializer
- Defined in:
- lib/radiator/utils.rb
Instance Method Summary collapse
- #debug(message, prefix = nil) ⇒ Object
- #error(message, prefix = nil) ⇒ Object
- #extract_signatures(options) ⇒ Object
- #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
- #send_log(level, message, prefix = nil) ⇒ Object
- #unhexlify(s) ⇒ Object
- #varint(n) ⇒ Object
- #warning(message, prefix = nil, log_debug_node = false) ⇒ Object
Instance Method Details
#debug(message, prefix = nil) ⇒ Object
51 52 53 54 55 |
# File 'lib/radiator/utils.rb', line 51 def debug(, prefix = nil) if ENV['LOG'] == 'DEBUG' send_log(:debug, , prefix) end end |
#error(message, prefix = nil) ⇒ Object
41 42 43 |
# File 'lib/radiator/utils.rb', line 41 def error(, prefix = nil) send_log(:error, , prefix) end |
#extract_signatures(options) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/radiator/utils.rb', line 3 def extract_signatures() params = [:params] signatures = params.map do |param| next unless defined? param.map param.map do |tx| tx[:signatures] rescue nil end end.flatten.compact expirations = params.map do |param| next unless defined? param.map param.map do |tx| Time.parse(tx[:expiration] + 'Z') rescue nil end end.flatten.compact [signatures, expirations.min] end |
#hexlify(s) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/radiator/utils.rb', line 57 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/radiator/utils.rb', line 93 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
127 128 129 |
# File 'lib/radiator/utils.rb', line 127 def pakC(i) [i].pack('C') end |
#pakc(i) ⇒ Object
131 132 133 |
# File 'lib/radiator/utils.rb', line 131 def pakc(i) [i].pack('c') end |
#pakHash(h) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/radiator/utils.rb', line 110 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
143 144 145 |
# File 'lib/radiator/utils.rb', line 143 def pakI(i) [i].pack('I') end |
#pakL!(i) ⇒ Object
147 148 149 |
# File 'lib/radiator/utils.rb', line 147 def pakL!(i) [i].pack('L!') end |
#pakS(i) ⇒ Object
139 140 141 |
# File 'lib/radiator/utils.rb', line 139 def pakS(i) [i].pack('S') end |
#paks(i) ⇒ Object
135 136 137 |
# File 'lib/radiator/utils.rb', line 135 def paks(i) [i].pack('s') end |
#pakStr(s) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/radiator/utils.rb', line 84 def pakStr(s) s = s.dup.force_encoding('BINARY') bytes = [] bytes << varint(s.size) bytes << s bytes.join end |
#send_log(level, message, prefix = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/radiator/utils.rb', line 25 def send_log(level, , prefix = nil) = if !!prefix "#{prefix} :: #{}" else end if !!@logger @logger.send level, else puts "#{level}: #{}" end nil end |
#unhexlify(s) ⇒ Object
67 68 69 |
# File 'lib/radiator/utils.rb', line 67 def unhexlify(s) s.split.pack('H*') end |
#varint(n) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/radiator/utils.rb', line 71 def varint(n) data = [] while n >= 0x80 data += [(n & 0x7f) | 0x80] n >>= 7 end data += [n] data.pack('C*') end |
#warning(message, prefix = nil, log_debug_node = false) ⇒ Object
45 46 47 48 49 |
# File 'lib/radiator/utils.rb', line 45 def warning(, prefix = nil, log_debug_node = false) debug("Current node: #{@url}", prefix) if !!log_debug_node && @url send_log(:warn, , prefix) end |