Module: Antrapol::ToolRack::DataConversionUtils

Includes:
ConditionUtils
Included in:
FileUtils
Defined in:
lib/toolrack/data_conversion_utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Make it available at class level too



154
155
156
157
158
# File 'lib/toolrack/data_conversion_utils.rb', line 154

def self.included(klass)
  klass.class_eval <<-END
    extend Antrapol::ToolRack::DataConversionUtils
  END
end

Instance Method Details

#bin_to_int(bin) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/toolrack/data_conversion_utils.rb', line 115

def bin_to_int(bin)
  if not_empty?(bin)
    bin.bytes.inject { |a,b| (a << 8) + b }
  else
    bin
  end
end

#from_b64(str) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/toolrack/data_conversion_utils.rb', line 10

def from_b64(str)
  if not_empty?(str)
    # decode operation supports both strict or non strict output
    Base64.decode64(str)
  else
    str
  end
end

#from_b64_strict(str) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/toolrack/data_conversion_utils.rb', line 37

def from_b64_strict(str)
  if not_empty?(str)
    Base64.strict_decode64(str)
  else
    str
  end
end

#from_hex(str) ⇒ Object Also known as: from_hex_string

def b58_to_number(str)

return 0 if is_empty?(str)
Base58.base58_to_int(str)

end



66
67
68
69
70
71
72
# File 'lib/toolrack/data_conversion_utils.rb', line 66

def from_hex(str)
  if not_empty?(str)
    str.scan(/../).map { |x| x.hex.chr }.join
  else
    str
  end
end

#hex_to_bin(hex) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/toolrack/data_conversion_utils.rb', line 100

def hex_to_bin(hex)
  if not_empty?(hex)
    # [abcd1234].pack('H*')
    # turn hex to binary
    case hex
    when Array
      hex.pack('H*')
    else 
      hex.split.pack('H*')
    end
  else
    hex
  end
end

#hex_to_number(str) ⇒ Object Also known as: hex_to_num, hex_to_int



75
76
77
78
79
80
81
# File 'lib/toolrack/data_conversion_utils.rb', line 75

def hex_to_number(str)
  if not_empty?(str)
    str.to_i(16)
  else
    str
  end
end

#int_to_bin(intStr) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/toolrack/data_conversion_utils.rb', line 123

def int_to_bin(intStr)
  if not_empty?(intStr)
    hex_to_bin(intStr.to_i.to_s(16))
  else
    intStr
  end
end

#string_to_bool(str) ⇒ Object Also known as: str_to_bool, string_to_boolean



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/toolrack/data_conversion_utils.rb', line 131

def string_to_bool(str)
  if not_empty?(str) and is_str_bool?(str)
    s = str.to_s.strip.downcase
    case s
    when "true"
      true
    when "false"
      false
    else
      nil
    end
  else
    nil
  end
end

#to_b64(bin) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/toolrack/data_conversion_utils.rb', line 19

def to_b64(bin)
  if not_empty?(bin)
    # Base64 standard output which has new line in the middle of the output
    Base64.encode64(bin)
  else
    bin
  end
end

#to_b64_strict(bin) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/toolrack/data_conversion_utils.rb', line 28

def to_b64_strict(bin)
  if not_empty?(bin)
    # strict base64 shall have NO new line in the middle of the output
    Base64.strict_encode64(bin)
  else
    bin
  end
end

#to_hex(bin) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/toolrack/data_conversion_utils.rb', line 85

def to_hex(bin)
  if not_empty?(bin) 
    case bin
    when String
      bin.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
    when Integer
      bin.to_s(16)
    else
      bin
    end
  else
    bin
  end
end