Module: SecurizeString::BinaryStringDataMethods::ClassMethods

Defined in:
lib/securize_string/binary_string_data_methods.rb

Overview

Adds basic binary data class methods via an include of SecurizeString::BinaryStringDataMethods.

Instance Method Summary collapse

Instance Method Details

#parse_data(value, opts = {}) ⇒ Object

Creates a data string from one many kinds of values:

:data

(default) The passed string value is directly used.

:hex

Initialize using a hexidecimal string.

:int

Initialize using the numeric value of the hexidecimal string.

:base64

Initialize using the given base64 encoded data.

To specify a value kind, use the :type option.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/securize_string/binary_string_data_methods.rb', line 22

def parse_data(value, opts={})
  raise ArgumentError, "The first argument is a symbol; setting the input data type this way is no longer supported. Call `#{self.class.name}.new('string', :type => #{value.inspect})' instead.", caller if value.kind_of?(Symbol)
  data_type_hint = (opts[:data_type_hint] || opts[:type] || :data).to_sym
  case data_type_hint
  when :hex
    hex_string = value.to_s.delete('^0-9a-fA-F')
    data_string = [hex_string].pack('H' + hex_string.bytesize.to_s)
  when :data
    data_string = value.to_s
  when :int
    data_string = self.send(__method__, value.to_i.to_s(16), :type => :hex)
  when :base64
    data_string = Base64.decode64(value.to_s)
  else
    raise ArgumentError, "Unrecognized data type hint: #{data_type_hint.inspect}"
  end
  
  return data_string
end