Class: EncodedId::ReversibleId

Inherits:
Object
  • Object
show all
Defined in:
lib/encoded_id/reversible_id.rb

Instance Method Summary collapse

Constructor Details

#initialize(salt:, length: 8, split_at: 4, split_with: "-", alphabet: Alphabet.modified_crockford, hex_digit_encoding_group_size: 4) ⇒ ReversibleId

Returns a new instance of ReversibleId.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/encoded_id/reversible_id.rb', line 11

def initialize(salt:, length: 8, split_at: 4, split_with: "-", alphabet: Alphabet.modified_crockford, hex_digit_encoding_group_size: 4)
  raise InvalidAlphabetError, "alphabet must be an instance of Alphabet" unless alphabet.is_a?(Alphabet)
  @alphabet = alphabet

  raise InvalidConfigurationError, "Salt must be a string and longer that 3 characters" unless salt.is_a?(String) && salt.size > 3
  @salt = salt
  # Target length of the encoded string (the minimum but not maximum length)
  raise InvalidConfigurationError, "Length must be an integer greater than 0" unless length.is_a?(Integer) && length > 0
  @length = length
  # Split the encoded string into groups of this size
  unless (split_at.is_a?(Integer) && split_at > 0) || split_at.nil?
    raise InvalidConfigurationError, "Split at must be an integer greater than 0 or nil"
  end
  @split_at = split_at
  unless split_with.is_a?(String) && !alphabet.characters.include?(split_with)
    raise InvalidConfigurationError, "Split with must be a string and not part of the alphabet"
  end
  @split_with = split_with
  # Number of hex digits to encode in each group, larger values will result in shorter hashes for longer inputs.
  # Vice versa for smaller values, ie a smaller value will result in smaller hashes for small inputs.
  if hex_digit_encoding_group_size < 1 || hex_digit_encoding_group_size > 32
    raise InvalidConfigurationError, "hex_digit_encoding_group_size must be > 0 and <= 32"
  end
  @hex_digit_encoding_group_size = hex_digit_encoding_group_size
end

Instance Method Details

#decode(str) ⇒ Object

Decode the hash to original array



51
52
53
54
55
# File 'lib/encoded_id/reversible_id.rb', line 51

def decode(str)
  encoded_id_generator.decode(convert_to_hash(str))
rescue ::Hashids::InputError => e
  raise EncodedIdFormatError, e.message
end

#decode_hex(str) ⇒ Object

Decode hex strings from a hash



58
59
60
61
# File 'lib/encoded_id/reversible_id.rb', line 58

def decode_hex(str)
  integers = encoded_id_generator.decode(convert_to_hash(str))
  integers_to_hex_strings(integers)
end

#encode(values) ⇒ Object

Encode the input values into a hash



38
39
40
41
42
43
# File 'lib/encoded_id/reversible_id.rb', line 38

def encode(values)
  inputs = prepare_input(values)
  encoded_id = encoded_id_generator.encode(inputs)
  encoded_id = humanize_length(encoded_id) unless split_at.nil?
  encoded_id
end

#encode_hex(hexs) ⇒ Object

Encode hex strings into a hash



46
47
48
# File 'lib/encoded_id/reversible_id.rb', line 46

def encode_hex(hexs)
  encode(integer_representation(hexs))
end