Module: ScaleRb::Utils
- Defined in:
- lib/scale_rb/utils.rb
Class Method Summary collapse
- .camelize(str) ⇒ Object
- .get(hash, *keys) ⇒ Object
- .hex_to_u8a(str) ⇒ Object
- .int_to_u8a(int, bit_length = nil) ⇒ Object
- .int_to_uint(signed, bit_length) ⇒ Object
- .key?(hash, key) ⇒ Boolean
- .u8a?(arr) ⇒ Boolean
- .u8a_to_bin(u8a) ⇒ Object
- .u8a_to_hex(u8a) ⇒ Object
- .u8a_to_int(u8a, bit_length) ⇒ Object
- .u8a_to_uint(u8a) ⇒ Object
- .u8a_to_utf8(u8a) ⇒ Object
- .uint_to_int(unsigned, bit_length) ⇒ Object
- .underscore(str) ⇒ Object
-
.unix_to_utc(unix) ⇒ Object
unix timestamp to utc.
-
.utc_to_unix(utc_asctime) ⇒ Object
utc to unix timestamp.
Class Method Details
.camelize(str) ⇒ Object
31 32 33 |
# File 'lib/scale_rb/utils.rb', line 31 def camelize(str) str.split('_').collect(&:capitalize).join end |
.get(hash, *keys) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/scale_rb/utils.rb', line 110 def get(hash, *keys) keys.reduce(hash) do |h, key| break nil unless h.is_a?(Hash) if key.instance_of?(String) h[key] || h[key.to_sym] elsif key.instance_of?(Symbol) h[key] || h[key.to_s] else h[key] end end end |
.hex_to_u8a(str) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/scale_rb/utils.rb', line 23 def hex_to_u8a(str) data = str.start_with?('0x') ? str[2..] : str raise 'Not valid hex string' if data =~ /[^\da-f]+/i data = "0#{data}" if data.length.odd? data.scan(/../).map(&:hex) end |
.int_to_u8a(int, bit_length = nil) ⇒ Object
43 44 45 46 |
# File 'lib/scale_rb/utils.rb', line 43 def int_to_u8a(int, bit_length = nil) hex = bit_length ? int.to_s(16).rjust(bit_length / 4, '0') : int.to_s(16) hex_to_u8a(hex) end |
.int_to_uint(signed, bit_length) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/scale_rb/utils.rb', line 54 def int_to_uint(signed, bit_length) unsigned_mid = 2**(bit_length - 1) unsigned_ceiling = 2**bit_length raise 'Out of scope' if signed >= unsigned_mid || signed <= -unsigned_mid signed.negative? ? unsigned_ceiling + signed : signed end |
.key?(hash, key) ⇒ Boolean
102 103 104 105 106 107 108 |
# File 'lib/scale_rb/utils.rb', line 102 def key?(hash, key) if key.instance_of?(String) hash.key?(key) || hash.key?(key.to_sym) else hash.key?(key) || hash.key?(key.to_s) end end |
.u8a?(arr) ⇒ Boolean
72 73 74 |
# File 'lib/scale_rb/utils.rb', line 72 def u8a?(arr) arr.all? { |e| e >= 0 && e <= 255 } end |
.u8a_to_bin(u8a) ⇒ Object
82 83 84 85 86 |
# File 'lib/scale_rb/utils.rb', line 82 def u8a_to_bin(u8a) raise 'Not a byte array' unless u8a?(u8a) u8a.reduce('0b') { |bin, u8| bin + u8.to_s(2).rjust(8, '0') } end |
.u8a_to_hex(u8a) ⇒ Object
76 77 78 79 80 |
# File 'lib/scale_rb/utils.rb', line 76 def u8a_to_hex(u8a) raise 'Not a byte array' unless u8a?(u8a) u8a.reduce('0x') { |hex, u8| hex + u8.to_s(16).rjust(2, '0') } end |
.u8a_to_int(u8a, bit_length) ⇒ Object
98 99 100 |
# File 'lib/scale_rb/utils.rb', line 98 def u8a_to_int(u8a, bit_length) uint_to_int(u8a_to_uint(u8a), bit_length) end |
.u8a_to_uint(u8a) ⇒ Object
94 95 96 |
# File 'lib/scale_rb/utils.rb', line 94 def u8a_to_uint(u8a) u8a_to_hex(u8a).to_i(16) end |
.u8a_to_utf8(u8a) ⇒ Object
88 89 90 91 92 |
# File 'lib/scale_rb/utils.rb', line 88 def u8a_to_utf8(u8a) raise 'Not a byte array' unless u8a?(u8a) u8a.pack('C*').force_encoding('utf-8') end |
.uint_to_int(unsigned, bit_length) ⇒ Object
48 49 50 51 52 |
# File 'lib/scale_rb/utils.rb', line 48 def uint_to_int(unsigned, bit_length) unsigned_mid = 2**(bit_length - 1) unsigned_ceiling = 2**bit_length unsigned >= unsigned_mid ? unsigned - unsigned_ceiling : unsigned end |
.underscore(str) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/scale_rb/utils.rb', line 35 def underscore(str) str.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |
.unix_to_utc(unix) ⇒ Object
unix timestamp to utc
63 64 65 |
# File 'lib/scale_rb/utils.rb', line 63 def unix_to_utc(unix) Time.at(unix).utc.asctime end |
.utc_to_unix(utc_asctime) ⇒ Object
utc to unix timestamp
68 69 70 |
# File 'lib/scale_rb/utils.rb', line 68 def utc_to_unix(utc_asctime) Time.parse(utc_asctime) end |