Module: Radiator::Utils

Included in:
Api, ErrorParser, Operation, Transaction, Type::Serializer
Defined in:
lib/radiator/utils.rb

Instance Method Summary collapse

Instance Method Details

#debug(obj, prefix = nil) ⇒ Object



70
71
72
73
74
# File 'lib/radiator/utils.rb', line 70

def debug(obj, prefix = nil)
  if %w(DEBUG TRACE).include? ENV['LOG']
    send_log(:debug, obj, prefix)
  end
end

#error(obj, prefix = nil) ⇒ Object



60
61
62
# File 'lib/radiator/utils.rb', line 60

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
24
25
# File 'lib/radiator/utils.rb', line 3

def extract_signatures(options)
  return [] unless defined? options[:params].map
  
  params = options[: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



76
77
78
79
80
81
82
83
84
# File 'lib/radiator/utils.rb', line 76

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/radiator/utils.rb', line 112

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



146
147
148
# File 'lib/radiator/utils.rb', line 146

def pakC(i)
  [i].pack('C')
end

#pakc(i) ⇒ Object



150
151
152
# File 'lib/radiator/utils.rb', line 150

def pakc(i)
  [i].pack('c')
end

#pakHash(h) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/radiator/utils.rb', line 129

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



162
163
164
# File 'lib/radiator/utils.rb', line 162

def pakI(i)
  [i].pack('I')
end

#pakL!(i) ⇒ Object



166
167
168
# File 'lib/radiator/utils.rb', line 166

def pakL!(i)
  [i].pack('L!')
end

#pakS(i) ⇒ Object



158
159
160
# File 'lib/radiator/utils.rb', line 158

def pakS(i)
  [i].pack('S')
end

#paks(i) ⇒ Object



154
155
156
# File 'lib/radiator/utils.rb', line 154

def paks(i)
  [i].pack('s')
end

#pakStr(s) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/radiator/utils.rb', line 103

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/radiator/utils.rb', line 27

def send_log(level, obj, prefix = nil)
  log_message = case obj
  when String
    log_message = if !!prefix
      "#{prefix} :: #{obj}"
    else
      obj
    end
    
    if !!@logger
      @logger.send level, log_message
    else
      puts "#{level}: #{log_message}"
    end
  else
    if defined? @logger.ap
      if !!prefix
        @logger.ap log_level: level, prefix => obj
      else
        @logger.ap obj, level
      end
    else
      if !!prefix
        @logger.send level, ({prefix => obj}).inspect
      else
        @logger.send level, obj.inspect
      end
    end
  end
  
  nil
end

#unhexlify(s) ⇒ Object



86
87
88
# File 'lib/radiator/utils.rb', line 86

def unhexlify(s)
  s.split.pack('H*')
end

#varint(n) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/radiator/utils.rb', line 90

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



64
65
66
67
68
# File 'lib/radiator/utils.rb', line 64

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