Module: Radiator::Utils
- Included in:
- Api, ErrorParser, Operation, Transaction, Type::Serializer
- Defined in:
- lib/radiator/utils.rb
Instance Method Summary collapse
- #debug(obj, prefix = nil) ⇒ Object
- #error(obj, 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, obj, prefix = nil) ⇒ Object
- #unhexlify(s) ⇒ Object
- #varint(n) ⇒ Object
- #warning(obj, prefix = nil, log_debug_node = false) ⇒ Object
Instance Method Details
#debug(obj, prefix = nil) ⇒ Object
60 61 62 63 64 |
# File 'lib/radiator/utils.rb', line 60 def debug(obj, prefix = nil) if %w(DEBUG TRACE).include? ENV['LOG'] send_log(:debug, obj, prefix) end end |
#error(obj, prefix = nil) ⇒ Object
50 51 52 |
# File 'lib/radiator/utils.rb', line 50 def error(obj, prefix = nil) send_log(:error, obj, 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
66 67 68 69 70 71 72 73 74 |
# File 'lib/radiator/utils.rb', line 66 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/radiator/utils.rb', line 102 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
136 137 138 |
# File 'lib/radiator/utils.rb', line 136 def pakC(i) [i].pack('C') end |
#pakc(i) ⇒ Object
140 141 142 |
# File 'lib/radiator/utils.rb', line 140 def pakc(i) [i].pack('c') end |
#pakHash(h) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/radiator/utils.rb', line 119 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
152 153 154 |
# File 'lib/radiator/utils.rb', line 152 def pakI(i) [i].pack('I') end |
#pakL!(i) ⇒ Object
156 157 158 |
# File 'lib/radiator/utils.rb', line 156 def pakL!(i) [i].pack('L!') end |
#pakS(i) ⇒ Object
148 149 150 |
# File 'lib/radiator/utils.rb', line 148 def pakS(i) [i].pack('S') end |
#paks(i) ⇒ Object
144 145 146 |
# File 'lib/radiator/utils.rb', line 144 def paks(i) [i].pack('s') end |
#pakStr(s) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/radiator/utils.rb', line 93 def pakStr(s) s = s.dup.force_encoding('BINARY') bytes = [] bytes << varint(s.size) bytes << s bytes.join end |
#send_log(level, obj, prefix = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/radiator/utils.rb', line 25 def send_log(level, obj, prefix = nil) = case obj when String = if !!prefix "#{prefix} :: #{obj}" else obj end if !!@logger @logger.send level, else puts "#{level}: #{log_message}" end else if !!prefix @logger.ap log_level: level, prefix => obj else @logger.ap obj, level end end nil end |
#unhexlify(s) ⇒ Object
76 77 78 |
# File 'lib/radiator/utils.rb', line 76 def unhexlify(s) s.split.pack('H*') end |
#varint(n) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/radiator/utils.rb', line 80 def varint(n) data = [] while n >= 0x80 data += [(n & 0x7f) | 0x80] n >>= 7 end data += [n] data.pack('C*') end |
#warning(obj, prefix = nil, log_debug_node = false) ⇒ Object
54 55 56 57 58 |
# File 'lib/radiator/utils.rb', line 54 def warning(obj, prefix = nil, log_debug_node = false) debug("Current node: #{@url}", prefix) if !!log_debug_node && @url send_log(:warn, obj, prefix) end |