Skip to content

Commit

Permalink
Merge pull request #7 from avcwisesa/max_input_per_id
Browse files Browse the repository at this point in the history
Add max_inputs_per_id as argument for ReversibleId#initialize. Fixes #5
  • Loading branch information
stevegeek authored Oct 26, 2023
2 parents 52c744c + eba9f88 commit f5e09d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ the maximum input length that will be decoded. If the encoded string exceeds `ma
`EncodedIdLengthError` will be raised. If the input exceeds `max_length` then a `InvalidInputError` will
be raised. If `max_length` is set to `nil`, then no validation, even using the default will be performed.

### `max_inputs_per_id`

`max_inputs_per_id`: the maximum amount of IDs to be encoded together. The default is 32.

This maximum amount is used to limit:
- the length of array input passed to `encode`
- the length of integer array encoded in hex string(s) passed to `encode_hex` function.
`InvalidInputError` wil be raised when array longer than `max_inputs_per_id` is provided.

### `alphabet`

`alphabet`: the alphabet used in the encoded string. By default, it uses a variation of the Crockford reduced character set (https://www.crockford.com/base32.html).
Expand Down
10 changes: 9 additions & 1 deletion lib/encoded_id/reversible_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
# Note hashIds already has a built in profanity limitation algorithm
module EncodedId
class ReversibleId
def initialize(salt:, length: 8, split_at: 4, split_with: "-", alphabet: Alphabet.modified_crockford, hex_digit_encoding_group_size: 4, max_length: 128)
def initialize(salt:, length: 8, split_at: 4, split_with: "-", alphabet: Alphabet.modified_crockford, hex_digit_encoding_group_size: 4, max_length: 128, max_inputs_per_id: 32)
@alphabet = validate_alphabet(alphabet)
@salt = validate_salt(salt)
@length = validate_length(length)
@split_at = validate_split_at(split_at)
@split_with = validate_split_with(split_with, alphabet)
@hex_represention_encoder = HexRepresentation.new(hex_digit_encoding_group_size)
@max_length = validate_max_length(max_length)
@max_inputs_per_id = validate_max_input(max_inputs_per_id)
end

# Encode the input values into a hash
Expand Down Expand Up @@ -80,6 +81,11 @@ def validate_max_length(max_length)
raise InvalidConfigurationError, "Max length must be an integer greater than 0"
end

def validate_max_input(max_inputs_per_id)
return max_inputs_per_id if valid_integer_option?(max_inputs_per_id)
raise InvalidConfigurationError, "Max inputs per ID must be an integer greater than 0"
end

# Split the encoded string into groups of this size
def validate_split_at(split_at)
return split_at if valid_integer_option?(split_at) || split_at.nil?
Expand All @@ -99,6 +105,8 @@ def prepare_input(value)
inputs = value.is_a?(Array) ? value.map(&:to_i) : [value.to_i]
raise ::EncodedId::InvalidInputError, "Integer IDs to be encoded can only be positive" if inputs.any?(&:negative?)

raise ::EncodedId::InvalidInputError, "%d integer IDs provided, maximum amount of IDs is %d" % [inputs.length, @max_inputs_per_id] if inputs.length > @max_inputs_per_id

inputs
end

Expand Down
5 changes: 3 additions & 2 deletions sig/encoded_id.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module EncodedId
end

class ReversibleId
def initialize: (salt: ::String, ?length: ::Integer, ?split_at: ::Integer, ?split_with: ::String, ?alphabet: Alphabet, ?hex_digit_encoding_group_size: ::Integer, ?max_length: ::Integer) -> void
def initialize: (salt: ::String, ?length: ::Integer, ?split_at: ::Integer, ?split_with: ::String, ?alphabet: Alphabet, ?hex_digit_encoding_group_size: ::Integer, ?max_length: ::Integer, ?max_inputs_per_id: ::Integer) -> void

# Encode the input values into a hash
def encode: (encodeableValue values) -> ::String
Expand Down Expand Up @@ -95,6 +95,7 @@ module EncodedId
def validate_salt: (::String) -> ::String
def validate_length: (::Integer) -> ::Integer
def validate_max_length: (::Integer | nil) -> (::Integer | nil)
def validate_max_input: (::Integer) -> ::Integer
def validate_split_at: (::Integer | nil) -> (::Integer | nil)
def validate_split_with: (::String, Alphabet) -> ::String
def validate_hex_digit_encoding_group_size: (::Integer) -> ::Integer
Expand All @@ -108,7 +109,7 @@ module EncodedId

def humanize_length: (::String hash) -> ::String

def convert_to_hash: (::String str) -> ::String
def convert_to_hash: (::String str, bool) -> ::String

def map_equivalent_characters: (::String str) -> ::String
def max_length_exceeded?: (::String str) -> bool
Expand Down
26 changes: 26 additions & 0 deletions test/encoded_id/test_reversible_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ def test_it_raises_with_invalid_max_length_type
end
end

def test_it_raises_with_invalid_inputs_length
assert_raises ::EncodedId::InvalidConfigurationError do
::EncodedId::ReversibleId.new(salt: salt, max_inputs_per_id: 0)
end
end

def test_it_raises_with_invalid_inputs_length_type
assert_raises ::EncodedId::InvalidConfigurationError do
::EncodedId::ReversibleId.new(salt: salt, max_inputs_per_id: "foo")
end
end

def test_it_raises_with_invalid_alphabet
assert_raises ::EncodedId::InvalidAlphabetError do
::EncodedId::ReversibleId.new(salt: salt, alphabet: 1234)
Expand Down Expand Up @@ -400,6 +412,20 @@ def test_it_raises_when_hex_encoding_size_exceeds_max_length
end
end

def test_it_raises_when_encode_amount_of_id_provided_exceeds_max_inputs
id = ["1", "2"]
assert_raises ::EncodedId::InvalidInputError do
::EncodedId::ReversibleId.new(salt: salt, max_inputs_per_id: 1).encode(id)
end
end

def test_it_raises_when_encode_hex_amount_of_id_provided_exceeds_max_inputs
id = "9a566b8b-8618-42ab-8db7-a5a0276401fd"
assert_raises ::EncodedId::InvalidInputError do
::EncodedId::ReversibleId.new(salt: salt, max_inputs_per_id: 7).encode_hex(id)
end
end

private

def salt
Expand Down

0 comments on commit f5e09d6

Please sign in to comment.