encoded_id 1.0.0.rc3 → 1.0.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44375d994a7eabe8245cf6bcb0e889e4d9de13f3e67feb98ee77959bc8f076a5
4
- data.tar.gz: 4e6db397ee9564375ae7387d57f341266f22fd58214ccecc890b7fb68d8cc4ab
3
+ metadata.gz: '0844f07baeecd48a70bf0b2564dfb9f02998b002279b44bf75bd0172ccc265be'
4
+ data.tar.gz: 0bcd81de8a4ea7e034097a92dff9ba82478d92f87f7820dda872a9f35b5294d3
5
5
  SHA512:
6
- metadata.gz: aade9242ac4d6fffd5ea3957af76a065d14143e547e31b4e7fda5203dcb2aa27f05166e5e803a8677f874bb443fc136196f0ce3bdd78b514606f251721f95d27
7
- data.tar.gz: fa4cc2385cbb6a726fc00bdbb5e70d1546abe46b86ba020243327942f32886a4928b8b65bc7a76c9e134bb162c17f4c69798a5e0080c43bf2e7802f92dfa580f
6
+ metadata.gz: 4dcf3c99891f8a3a195d188656d1c45c9471760dfbc715fe994fac4c4145a086d9573b663c5a4a79894c71c4e3a15bf68ff0d30b20e44b8fe661c68f1707c065
7
+ data.tar.gz: 8edb3675d565ff7be88441fcdb2a55783415f170fcd8d9102c6bb0fff8cc6fa0d22f1af19e72dd5558dedb37ba2af23c29fd6171d12da2032a0e2ce187bedeca
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
  - `ReversibleId` now no longer downcases the encodedid input string by default on decode, ie the `decode` option `downcase` is now `false`. In a future release the `downcase` option will be removed.
8
8
 
9
+ ## [1.0.0.rc4] - unreleased
10
+
11
+ - Add an optional `max_inputs_per_id` argument to `ReversibleId`, thanks to [@avcwisesa](https://github.com/avcwisesa)
12
+
9
13
  ## [1.0.0.rc3] - 2023-10-23
10
14
 
11
15
  - Add an optional `max_length` argument to `ReversibleId`, thanks to [@jugglebird](https://github.com/jugglebird)
data/README.md CHANGED
@@ -137,6 +137,15 @@ the maximum input length that will be decoded. If the encoded string exceeds `ma
137
137
  `EncodedIdLengthError` will be raised. If the input exceeds `max_length` then a `InvalidInputError` will
138
138
  be raised. If `max_length` is set to `nil`, then no validation, even using the default will be performed.
139
139
 
140
+ ### `max_inputs_per_id`
141
+
142
+ `max_inputs_per_id`: the maximum amount of IDs to be encoded together. The default is 32.
143
+
144
+ This maximum amount is used to limit:
145
+ - the length of array input passed to `encode`
146
+ - the length of integer array encoded in hex string(s) passed to `encode_hex` function.
147
+ `InvalidInputError` wil be raised when array longer than `max_inputs_per_id` is provided.
148
+
140
149
  ### `alphabet`
141
150
 
142
151
  `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).
@@ -8,7 +8,7 @@ require "hashids"
8
8
  # Note hashIds already has a built in profanity limitation algorithm
9
9
  module EncodedId
10
10
  class ReversibleId
11
- def initialize(salt:, length: 8, split_at: 4, split_with: "-", alphabet: Alphabet.modified_crockford, hex_digit_encoding_group_size: 4, max_length: 128)
11
+ 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)
12
12
  @alphabet = validate_alphabet(alphabet)
13
13
  @salt = validate_salt(salt)
14
14
  @length = validate_length(length)
@@ -16,13 +16,14 @@ module EncodedId
16
16
  @split_with = validate_split_with(split_with, alphabet)
17
17
  @hex_represention_encoder = HexRepresentation.new(hex_digit_encoding_group_size)
18
18
  @max_length = validate_max_length(max_length)
19
+ @max_inputs_per_id = validate_max_input(max_inputs_per_id)
19
20
  end
20
21
 
21
22
  # Encode the input values into a hash
22
23
  def encode(values)
23
24
  inputs = prepare_input(values)
24
25
  encoded_id = encoded_id_generator.encode(inputs)
25
- encoded_id = humanize_length(encoded_id) unless split_at.nil?
26
+ encoded_id = humanize_length(encoded_id) unless split_with.nil? || split_at.nil?
26
27
 
27
28
  raise EncodedIdLengthError if max_length_exceeded?(encoded_id)
28
29
 
@@ -80,6 +81,11 @@ module EncodedId
80
81
  raise InvalidConfigurationError, "Max length must be an integer greater than 0"
81
82
  end
82
83
 
84
+ def validate_max_input(max_inputs_per_id)
85
+ return max_inputs_per_id if valid_integer_option?(max_inputs_per_id)
86
+ raise InvalidConfigurationError, "Max inputs per ID must be an integer greater than 0"
87
+ end
88
+
83
89
  # Split the encoded string into groups of this size
84
90
  def validate_split_at(split_at)
85
91
  return split_at if valid_integer_option?(split_at) || split_at.nil?
@@ -87,8 +93,8 @@ module EncodedId
87
93
  end
88
94
 
89
95
  def validate_split_with(split_with, alphabet)
90
- return split_with if split_with.is_a?(String) && !alphabet.characters.include?(split_with)
91
- raise InvalidConfigurationError, "Split with must be a string and not part of the alphabet"
96
+ return split_with if split_with.nil? || (split_with.is_a?(String) && !alphabet.characters.include?(split_with))
97
+ raise InvalidConfigurationError, "Split with must be a string and not part of the alphabet or nil"
92
98
  end
93
99
 
94
100
  def valid_integer_option?(value)
@@ -99,6 +105,8 @@ module EncodedId
99
105
  inputs = value.is_a?(Array) ? value.map(&:to_i) : [value.to_i]
100
106
  raise ::EncodedId::InvalidInputError, "Integer IDs to be encoded can only be positive" if inputs.any?(&:negative?)
101
107
 
108
+ 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
109
+
102
110
  inputs
103
111
  end
104
112
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EncodedId
4
- VERSION = "1.0.0.rc3"
4
+ VERSION = "1.0.0.rc4"
5
5
  end
data/sig/encoded_id.rbs CHANGED
@@ -59,7 +59,7 @@ module EncodedId
59
59
  end
60
60
 
61
61
  class ReversibleId
62
- def initialize: (salt: ::String, ?length: ::Integer, ?split_at: ::Integer, ?split_with: ::String, ?alphabet: Alphabet, ?hex_digit_encoding_group_size: ::Integer, ?max_length: ::Integer) -> void
62
+ 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
63
63
 
64
64
  # Encode the input values into a hash
65
65
  def encode: (encodeableValue values) -> ::String
@@ -95,6 +95,7 @@ module EncodedId
95
95
  def validate_salt: (::String) -> ::String
96
96
  def validate_length: (::Integer) -> ::Integer
97
97
  def validate_max_length: (::Integer | nil) -> (::Integer | nil)
98
+ def validate_max_input: (::Integer) -> ::Integer
98
99
  def validate_split_at: (::Integer | nil) -> (::Integer | nil)
99
100
  def validate_split_with: (::String, Alphabet) -> ::String
100
101
  def validate_hex_digit_encoding_group_size: (::Integer) -> ::Integer
@@ -108,7 +109,7 @@ module EncodedId
108
109
 
109
110
  def humanize_length: (::String hash) -> ::String
110
111
 
111
- def convert_to_hash: (::String str) -> ::String
112
+ def convert_to_hash: (::String str, bool) -> ::String
112
113
 
113
114
  def map_equivalent_characters: (::String str) -> ::String
114
115
  def max_length_exceeded?: (::String str) -> bool
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encoded_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc3
4
+ version: 1.0.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-23 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashids
@@ -67,11 +67,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
67
  version: 2.7.0
68
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
- - - ">"
70
+ - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: 1.3.1
72
+ version: '0'
73
73
  requirements: []
74
- rubygems_version: 3.4.20
74
+ rubygems_version: 3.5.3
75
75
  signing_key:
76
76
  specification_version: 4
77
77
  summary: EncodedId is a gem for creating reversible obfuscated IDs from numerical