encoded_id 1.0.0.rc4 → 1.0.0.rc6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +84 -1
- data/LICENSE.txt +1 -1
- data/README.md +79 -286
- data/context/encoded_id.md +283 -0
- data/lib/encoded_id/alphabet.rb +36 -3
- data/lib/encoded_id/blocklist.rb +90 -0
- data/lib/encoded_id/encoders/base.rb +71 -0
- data/lib/encoded_id/encoders/hash_id.rb +531 -0
- data/lib/encoded_id/encoders/hash_id_consistent_shuffle.rb +110 -0
- data/lib/encoded_id/encoders/hash_id_ordinal_alphabet_separator_guards.rb +270 -0
- data/lib/encoded_id/encoders/hash_id_salt.rb +51 -0
- data/lib/encoded_id/encoders/my_sqids.rb +465 -0
- data/lib/encoded_id/encoders/sqids.rb +42 -0
- data/lib/encoded_id/hex_representation.rb +23 -5
- data/lib/encoded_id/reversible_id.rb +110 -26
- data/lib/encoded_id/version.rb +4 -1
- data/lib/encoded_id.rb +41 -0
- metadata +17 -31
- data/Gemfile +0 -24
- data/Rakefile +0 -14
- data/Steepfile +0 -5
- data/rbs_collection.yaml +0 -24
- data/sig/encoded_id.rbs +0 -117
- data/sig/hash_ids.rbs +0 -70
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e5135a9e76cfd6a49cd14921bfa277deb83eb05b9e753efdb23e4145b3f5f7c7
|
|
4
|
+
data.tar.gz: ed8a43d5e654d9fd947d7b9b0a37aacc18c3b713c9303c9d6dbbf22b890eb965
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 91e58ac5b6df75e7da94f8ea4868967e93f6b8c76ec94cb6fc821a60ab33dfe704b19ad43d41916f8896d5db982f6d610e48335f8e9d03129f0422a7b0f5cd60
|
|
7
|
+
data.tar.gz: f427c6e2ca883e522e80a934053a5b5486dc75be3a6babf5e55f878d85fe8e31a0a1c1af98b4bbda8a5f310be18ac5ea2b2ec8edf322b54584d78b2126c92c15
|
data/CHANGELOG.md
CHANGED
|
@@ -5,10 +5,52 @@
|
|
|
5
5
|
### Breaking changes
|
|
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
|
+
- The default encoding engine is now `:sqids` to reflect the official "deprecated" status of `Hashid`s (see https://sqids.org/faq#why-hashids)
|
|
9
|
+
- Ruby < 3.2 support dropped. The minimum supported Ruby version is now 3.2.0
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
**Important!!: `:sqids` are not compatible with `:hashids`, DO NOT CHANGE FROM ONE TO THE OTHER AFTER GOING LIVE.**
|
|
12
|
+
|
|
13
|
+
## [1.0.0.rc6] - 2025-11-17
|
|
14
|
+
|
|
15
|
+
### Breaking changes
|
|
16
|
+
|
|
17
|
+
- Empty array inputs will now raise `InvalidInputError`
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- Added support for [Sqids](https://sqids.org) as an alternative ID encoding engine. The default remains HashIds for backward compatibility.
|
|
22
|
+
- New encoder abstraction layer allows switching between HashIds and Sqids via the `encoder: :sqids` or `encoder: :hashids` parameter to `ReversibleId.new`.
|
|
23
|
+
- Support for blocklists in both HashIds and Sqids encoders to prevent generating IDs containing specific words. Use the `blocklist` parameter with a `Set` or `Array` of strings to `ReversibleId.new`. For Hashids encodings, an error will be raised if a generated ID contains a blocklisted word. For Sqids a new ID is generated that does not contain the block word.
|
|
24
|
+
|
|
25
|
+
### Added (Rails integration)
|
|
26
|
+
|
|
27
|
+
- Merged `encoded_id-rails` gem into the monorepo
|
|
28
|
+
- Support for configuring the encoder and blocklist options in Rails through the configuration class
|
|
29
|
+
- `#encoded_id` now defaults to returning an 'annotated' ID, one in which a prefix is added to the encoded ID to indicate the 'type' of the record the ID represents. This can be disabled. IDs generated by older versions of this gem will decode correctly. But note that IDs generated by this version onwards will not decode correctly by older versions of this gem so make sure to disable annotation if you need to support older versions of this gem.
|
|
30
|
+
- New `EncodedId::Rails::Persists` module which adds hooks to the model to persist the encoded ID to the DB (see docs).
|
|
31
|
+
- `#encoded_id_hash` has been added to return only the encoded ID without an annotation prefix. If annotation is disabled, this method is basically an alias to `#encoded_id`.
|
|
32
|
+
- `.find_all_by_encoded_id` has been added to return all records matching the given encoded ID. This will return all matching records whose IDs are encoded in the encoded_id. Missing records are ignored.
|
|
33
|
+
- `.find_all_by_encoded_id!` like `.find_all_by_encoded_id` but raises an `ActiveRecord::RecordNotFound` exception if *any* of the records are not found.
|
|
34
|
+
- `Configuration#model_to_param_returns_encoded_id` to allow `EncodedId::Rails::Model` inclusion to also bring in `EncodedId::Rails::PathParam` automatically.
|
|
35
|
+
|
|
36
|
+
### Changed (Rails integration)
|
|
37
|
+
|
|
38
|
+
- `#name_for_encoded_id_slug` no longer provides a default implementation, it is up to the user to define this method, or configure the gem to use a different method name.
|
|
39
|
+
- `#slugged_encoded_id` no longer takes a `with:` parameter. To specify the name of the method to call to generate the slug, use the `slug_value_method_name` configuration option.
|
|
40
|
+
|
|
41
|
+
### Fixed (Rails integration)
|
|
42
|
+
|
|
43
|
+
- `#decode_encoded_id` now raises if the encoded ID is not a string.
|
|
44
|
+
- Handle more cases where a record held onto a memoized `encoded_id` even though its `id` had changed
|
|
45
|
+
|
|
46
|
+
## [1.0.0.rc5] - 2025-04-09
|
|
47
|
+
|
|
48
|
+
- `encoded_id` now uses its own implementation of `hashids` which is more efficient and has a smaller memory footprint. This massively reduces the GC churn in high-throughput applications. This is an implementation based on the original `hashids` gem but with many optimisations and improvements. Functionally it is identical to the original `hashids` gem.
|
|
49
|
+
|
|
50
|
+
## [1.0.0.rc4] - 2024-04-29
|
|
10
51
|
|
|
11
52
|
- Add an optional `max_inputs_per_id` argument to `ReversibleId`, thanks to [@avcwisesa](https://github.com/avcwisesa)
|
|
53
|
+
- The option `split_with:` can also now be set to nil to disable splitting of the encoded ID string
|
|
12
54
|
|
|
13
55
|
## [1.0.0.rc3] - 2023-10-23
|
|
14
56
|
|
|
@@ -41,3 +83,44 @@
|
|
|
41
83
|
## [0.1.0] - 2022-10-11
|
|
42
84
|
|
|
43
85
|
- Initial release
|
|
86
|
+
|
|
87
|
+
## Rails Integration History
|
|
88
|
+
|
|
89
|
+
## [0.6.2] - 2023-02-09
|
|
90
|
+
|
|
91
|
+
- Fix `encoded_id` memoization clearing when record is duplicated
|
|
92
|
+
|
|
93
|
+
## [0.6.1] - 2023-02-09
|
|
94
|
+
|
|
95
|
+
- Fix `#encoded_id` to return nil if `#id` is nil
|
|
96
|
+
- Ensure `encoded_id` memoization is cleared when record is duplicated, or id changes
|
|
97
|
+
|
|
98
|
+
## [0.6.0] - 2022-12-21
|
|
99
|
+
|
|
100
|
+
- Rename mixin to `Model`
|
|
101
|
+
- Introduce optional mixins for overriding `#to_param`
|
|
102
|
+
|
|
103
|
+
## [0.5.0] - 2022-12-21
|
|
104
|
+
|
|
105
|
+
- `name_for_encoded_id_slug` no longer uses the return value from name but rather just uses the `class` `name`.
|
|
106
|
+
- If you want to change the name used in the slug, override `name_for_encoded_id_slug`
|
|
107
|
+
|
|
108
|
+
## [0.4.0] - 2022-12-18
|
|
109
|
+
|
|
110
|
+
- Refactor internals, remove any methods not actually related to creating `encoded_id`, (eg `slugged_id` was removed).
|
|
111
|
+
|
|
112
|
+
## [0.3.1] - 2022-12-15
|
|
113
|
+
|
|
114
|
+
- Fix default config
|
|
115
|
+
|
|
116
|
+
## [0.3.0] - 2022-12-15
|
|
117
|
+
|
|
118
|
+
- Updates gem `encoded_id` dependency and fixes configuration
|
|
119
|
+
|
|
120
|
+
## [0.2.0] - 2022-12-14
|
|
121
|
+
|
|
122
|
+
- No notes...
|
|
123
|
+
|
|
124
|
+
## [0.1.0] - 2022-11-17
|
|
125
|
+
|
|
126
|
+
- Initial release of Rails integration
|
data/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022 Stephen Ierodiaconou
|
|
3
|
+
Copyright (c) 2022-2024 Stephen Ierodiaconou
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
|
@@ -1,329 +1,125 @@
|
|
|
1
|
-
# EncodedId
|
|
1
|
+
# EncodedId and EncodedId::Rails
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+

|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
coder = ::EncodedId::ReversibleId.new(salt: my_salt)
|
|
7
|
-
coder.encode(123)
|
|
8
|
-
# => "p5w9-z27j"
|
|
9
|
-
coder.encode_hex("10f8c")
|
|
10
|
-
# => "w72a-y0az"
|
|
11
|
-
```
|
|
6
|
+
`encoded_id` lets you encode numerical or hex IDs into obfuscated strings that can be used in URLs.
|
|
12
7
|
|
|
13
|
-
|
|
8
|
+
`encoded_id-rails` is a Rails integration that provides additional features for using `encoded_id` with ActiveRecord models.
|
|
14
9
|
|
|
15
|
-
|
|
10
|
+
👉 **Full documentation available at [encoded-id.onrender.com](https://encoded-id.onrender.com)**
|
|
11
|
+
|
|
12
|
+
## Quick Example
|
|
16
13
|
|
|
17
14
|
```ruby
|
|
18
|
-
|
|
19
|
-
coder
|
|
15
|
+
coder = ::EncodedId::ReversibleId.new(salt: "my-salt")
|
|
16
|
+
coder.encode(123)
|
|
17
|
+
# => "p5w9-z27j"
|
|
20
18
|
|
|
21
|
-
#
|
|
19
|
+
# The encoded strings are reversible
|
|
20
|
+
coder.decode("p5w9-z27j")
|
|
21
|
+
# => [123]
|
|
22
|
+
|
|
23
|
+
# Supports encoding multiple IDs at once
|
|
22
24
|
coder.encode([78, 45])
|
|
23
25
|
# => "z2j7-0dmw"
|
|
24
26
|
|
|
25
|
-
#
|
|
26
|
-
coder.decode("z2j7-0dmw")
|
|
27
|
-
# => [78, 45]
|
|
28
|
-
|
|
29
|
-
# The decoder can be resilient to easily confused characters
|
|
30
|
-
coder.decode("z2j7-Odmw") # (note the capital 'o' instead of zero)
|
|
31
|
-
# => [78, 45]
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Features
|
|
35
|
-
|
|
36
|
-
* 🔄 encoded IDs are reversible (uses with https://hashids.org))
|
|
37
|
-
* 👥 supports multiple IDs encoded in one encoded string (eg `7aq6-0zqw` decodes to `[78, 45]`)
|
|
38
|
-
* 🔡 supports custom alphabets for the encoded string (at least 16 characters needed)
|
|
39
|
-
- by default uses a variation of the Crockford reduced character set (https://www.crockford.com/base32.html)
|
|
40
|
-
- 👓 easily confused characters (eg `i` and `j`, `0` and `O`, `1` and `I` etc) are mapped to counterpart characters, to help
|
|
41
|
-
avoid common readability mistakes when reading/sharing
|
|
42
|
-
- 🤬 build in profanity limitation
|
|
43
|
-
* 🤓 encoded string can be split into groups of letters to improve human-readability
|
|
44
|
-
- eg `nft9hr834htu` as `nft9-hr83-4htu`
|
|
45
|
-
* 🥽 supports limits on length to prevent resource exhaustion on encoding and decoding
|
|
46
|
-
* configured with sensible defaults
|
|
47
|
-
|
|
48
|
-
I aim for 100% test coverage and have fuzz tested quite extensively. But please report any issues!
|
|
49
|
-
|
|
50
|
-
### Experimental
|
|
51
|
-
|
|
52
|
-
* support for encoding of hex strings (eg UUIDs), including multiple IDs encoded in one string
|
|
53
|
-
|
|
54
|
-
### Coming soon
|
|
55
|
-
|
|
56
|
-
Performance improvements and benchmarking!
|
|
57
|
-
|
|
58
|
-
### Rails support `encoded_id-rails`
|
|
59
|
-
|
|
60
|
-
To use with **Rails** check out the [`encoded_id-rails`](https://github.com/stevegeek/encoded_id-rails) gem.
|
|
61
|
-
|
|
62
|
-
```ruby
|
|
27
|
+
# Can also be used with ActiveRecord models
|
|
63
28
|
class User < ApplicationRecord
|
|
64
|
-
include EncodedId::
|
|
29
|
+
include EncodedId::Rails::Model
|
|
30
|
+
|
|
31
|
+
# Optional slug for the encoded ID
|
|
32
|
+
def name_for_encoded_id_slug
|
|
33
|
+
full_name
|
|
34
|
+
end
|
|
65
35
|
end
|
|
66
36
|
|
|
67
|
-
|
|
68
|
-
# => #<User id: 78>
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
### Note on security of encoded IDs (hashids)
|
|
72
|
-
|
|
73
|
-
**Encoded IDs are not secure**. It maybe possible to reverse them via brute-force. They are meant to be used in URLs as
|
|
74
|
-
an obfuscation. The algorithm is not an encryption.
|
|
75
|
-
|
|
76
|
-
Please read more on https://hashids.org/
|
|
77
|
-
|
|
78
|
-
## Compare to alternate Gems
|
|
79
|
-
|
|
80
|
-
- https://github.com/excid3/prefixed_ids
|
|
81
|
-
- https://github.com/namick/obfuscate_id
|
|
82
|
-
- https://github.com/norman/friendly_id
|
|
83
|
-
- https://github.com/SPBTV/with_uid
|
|
84
|
-
|
|
85
|
-
## Installation
|
|
86
|
-
|
|
87
|
-
Install the gem and add to the application's Gemfile by executing:
|
|
88
|
-
|
|
89
|
-
$ bundle add encoded_id
|
|
90
|
-
|
|
91
|
-
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
92
|
-
|
|
93
|
-
$ gem install encoded_id
|
|
94
|
-
|
|
95
|
-
## `EncodedId::ReversibleId.new`
|
|
96
|
-
|
|
97
|
-
To create an instance of the encoder/decoder use `.new` with the `salt` option:
|
|
98
|
-
|
|
99
|
-
```ruby
|
|
100
|
-
coder = EncodedId::ReversibleId.new(
|
|
101
|
-
# The salt is required
|
|
102
|
-
salt: ...,
|
|
103
|
-
# And then the following options are optional
|
|
104
|
-
length: 8,
|
|
105
|
-
split_at: 4,
|
|
106
|
-
split_with: "-",
|
|
107
|
-
alphabet: EncodedId::Alphabet.modified_crockford,
|
|
108
|
-
hex_digit_encoding_group_size: 4 # Experimental
|
|
109
|
-
)
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
Note the `salt` value is required and should be a string of some length (greater than 3 characters). This is used to generate the encoded string.
|
|
113
|
-
|
|
114
|
-
It will need to be the same value when decoding the string back into the original ID. If the salt is changed, the encoded
|
|
115
|
-
strings will be different and possibly decode to different IDs.
|
|
116
|
-
|
|
117
|
-
### Options
|
|
118
|
-
|
|
119
|
-
The encoded ID is configurable. The following can be changed:
|
|
120
|
-
|
|
121
|
-
- the length, eg 8 characters for `p5w9-z27j`
|
|
122
|
-
- the alphabet used in it (min 16 characters)
|
|
123
|
-
- and the number of characters to split the output into and the separator
|
|
124
|
-
|
|
125
|
-
### `length`
|
|
126
|
-
|
|
127
|
-
`length`: the minimum length of the encoded string. The default is 8 characters.
|
|
128
|
-
|
|
129
|
-
The actual length of the encoded string can be longer if the inputs cannot be represented in the minimum length.
|
|
130
|
-
|
|
131
|
-
### `max_length`
|
|
132
|
-
|
|
133
|
-
`max_length`: the maximum length of the encoded string. The default is 128 characters.
|
|
134
|
-
|
|
135
|
-
The maximum length represents both the longest encoded string that will be generated and also a limit on
|
|
136
|
-
the maximum input length that will be decoded. If the encoded string exceeds `max_length` then a
|
|
137
|
-
`EncodedIdLengthError` will be raised. If the input exceeds `max_length` then a `InvalidInputError` will
|
|
138
|
-
be raised. If `max_length` is set to `nil`, then no validation, even using the default will be performed.
|
|
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
|
-
|
|
149
|
-
### `alphabet`
|
|
150
|
-
|
|
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).
|
|
152
|
-
|
|
153
|
-
`alphabet` must be an instance of `EncodedId::Alphabet`.
|
|
154
|
-
|
|
155
|
-
The default alphabet is `EncodedId::Alphabet.modified_crockford`.
|
|
156
|
-
|
|
157
|
-
To create a new alphabet, use `EncodedId::Alphabet.new`:
|
|
158
|
-
|
|
159
|
-
```ruby
|
|
160
|
-
alphabet = EncodedId::Alphabet.new("0123456789abcdef")
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
`EncodedId::Alphabet.new(characters, equivalences)`
|
|
164
|
-
|
|
165
|
-
**characters**
|
|
166
|
-
|
|
167
|
-
`characters`: the characters of the alphabet. Can be a string or array of strings.
|
|
168
|
-
|
|
169
|
-
Note that the `characters` of the alphabet must be at least 16 _unique_ characters long and must not contain any
|
|
170
|
-
whitespace characters.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
```ruby
|
|
174
|
-
alphabet = EncodedId::Alphabet.new("ςερτυθιοπλκξηγφδσαζχψωβνμ")
|
|
175
|
-
coder = ::EncodedId::ReversibleId.new(salt: my_salt, alphabet: alphabet)
|
|
176
|
-
coder.encode(123)
|
|
177
|
-
# => "πφλχ-ψησω"
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
Note that larger alphabets can result in shorter encoded strings (but remember that `length` specifies the minimum length
|
|
181
|
-
of the encoded string).
|
|
182
|
-
|
|
183
|
-
**equivalences**
|
|
184
|
-
|
|
185
|
-
You can optionally pass an appropriate character `equivalences` mapping. This is used to map easily confused characters
|
|
186
|
-
to their counterpart.
|
|
187
|
-
|
|
188
|
-
`equivalences`: a hash of characters keys, with their equivalent alphabet character mapped to in the values.
|
|
189
|
-
|
|
190
|
-
Note that the characters to be mapped:
|
|
191
|
-
- must not be in the alphabet,
|
|
192
|
-
- must map to a character that is in the alphabet.
|
|
193
|
-
|
|
194
|
-
`nil` is the default value which means no equivalences are used.
|
|
195
|
-
|
|
196
|
-
```ruby
|
|
197
|
-
alphabet = EncodedId::Alphabet.new("!@#$%^&*()+-={}", {"_" => "-"})
|
|
198
|
-
coder = ::EncodedId::ReversibleId.new(salt: my_salt, alphabet: alphabet)
|
|
199
|
-
coder.encode(123)
|
|
200
|
-
# => "}*^(-^}*="
|
|
37
|
+
# Find by encoded ID
|
|
38
|
+
user = User.find_by_encoded_id("p5w9-z27j") # => #<User id: 78>
|
|
39
|
+
user.encoded_id # => "user_p5w9-z27j"
|
|
40
|
+
user.slugged_encoded_id # => "bob-smith--user_p5w9-z27j"
|
|
201
41
|
```
|
|
202
42
|
|
|
203
|
-
|
|
43
|
+
## Key Features
|
|
204
44
|
|
|
205
|
-
|
|
45
|
+
* 🔄 **Reversible** - Encoded IDs can be decoded back to the original values
|
|
46
|
+
* 👥 **Multiple IDs** - Encode multiple numeric IDs in one string
|
|
47
|
+
* 🚀 **Choose your encoding** - Supports `Hashids` and `Sqids` out of the box, or use your own custom encoder
|
|
48
|
+
* 👓 **Human-readable** - Character grouping & character mappings of easily confused characters for better readability
|
|
49
|
+
* 🔡 **Custom alphabets** - Use your preferred character set, or a provided default
|
|
50
|
+
* 🚗 **Performance** - Uses an optimized `Hashids` encoder (compared to `hashids` gem) for better performance and less memory usage, and have pushed performance improvements to `Sqids` as well
|
|
51
|
+
* 🤬 **Profanity blocking** - Built-in word blocklist support and optional default lists
|
|
206
52
|
|
|
207
|
-
|
|
53
|
+
### Rails Integration Features
|
|
208
54
|
|
|
209
|
-
|
|
55
|
+
* 🏷️ **ActiveRecord integration** - Use with ActiveRecord models
|
|
56
|
+
* 🔑 **Per-model salt** - Use a custom salt for encoding per model
|
|
57
|
+
* 💅 **Slugged IDs** - URL-friendly slugs like `my-product--p5w9-z27j`
|
|
58
|
+
* 🔖 **Annotated IDs** - Model type indicators like `user_p5w9-z27j`
|
|
59
|
+
* 🔍 **Finder methods** - Find records using encoded IDs
|
|
60
|
+
* 🛣️ **URL params** - `to_param` with encoded IDs
|
|
61
|
+
* 🔒 **Safe defaults**: Limits on encoded ID lengths to prevent CPU and memory-intensive encode/decodes eg when used in URLs
|
|
62
|
+
* 💾 **Persistence** - Optional database persistence for efficient lookups
|
|
210
63
|
|
|
211
|
-
### `hex_digit_encoding_group_size`
|
|
212
64
|
|
|
213
|
-
|
|
65
|
+
## Standalone Gem
|
|
214
66
|
|
|
215
|
-
`hex_digit_encoding_group_size`: specifies the number of hex digits to encode in a group. Defaults to 4. Can be
|
|
216
|
-
between 1 and 32.
|
|
217
67
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
But note that bigger values will also result in larger markers that separate the groups so could end up increasing
|
|
222
|
-
the encoded string length undesirably.
|
|
223
|
-
|
|
224
|
-
See below section `Using with hex strings` for more details.
|
|
225
|
-
|
|
226
|
-
## `EncodedId::ReversibleId#encode`
|
|
227
|
-
|
|
228
|
-
`#encode(id)`: where `id` is an integer or array of integers to encode.
|
|
229
|
-
|
|
230
|
-
```ruby
|
|
231
|
-
coder.encode(123)
|
|
232
|
-
# => "p5w9-z27j"
|
|
68
|
+
```bash
|
|
69
|
+
# Add to Gemfile
|
|
70
|
+
bundle add encoded_id
|
|
233
71
|
|
|
234
|
-
#
|
|
235
|
-
|
|
236
|
-
# => "z2j7-0dmw"
|
|
72
|
+
# Or install directly
|
|
73
|
+
gem install encoded_id
|
|
237
74
|
```
|
|
238
75
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
`#decode(encoded_id)`: where `encoded_id` is a string to decode.
|
|
242
|
-
|
|
243
|
-
```ruby
|
|
244
|
-
# The encoded string can then be reversed back into the original IDs
|
|
245
|
-
coder.decode("z2j7-0dmw")
|
|
246
|
-
# => [78, 45]
|
|
247
|
-
```
|
|
76
|
+
See the [EncodedId API](https://encoded-id.onrender.com/docs/encoded_id/api) documentation for more details.
|
|
248
77
|
|
|
249
|
-
##
|
|
78
|
+
## Rails Integration Gem
|
|
250
79
|
|
|
251
|
-
|
|
80
|
+
```bash
|
|
81
|
+
# Add to Gemfile
|
|
82
|
+
bundle add encoded_id-rails
|
|
252
83
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
coder.encode_hex("10f8c")
|
|
256
|
-
# => "w72a-y0az"
|
|
84
|
+
# Then run the generator
|
|
85
|
+
rails g encoded_id:rails:install
|
|
257
86
|
```
|
|
258
87
|
|
|
259
|
-
|
|
260
|
-
integer equivalent. In other words the input is converted into an array of integers and encoded as normal with the
|
|
261
|
-
`encode` method.
|
|
262
|
-
|
|
263
|
-
eg with `hex_digit_encoding_group_size=1` and inpu `f1`, is split into `f` and `1`, and then encoded as `15` and `1`
|
|
264
|
-
respectively, ie `encode` is called with `[15, 1]`.
|
|
88
|
+
See the [Rails Integration](https://encoded-id.onrender.com/docs/encoded_id_rails) documentation for more details.
|
|
265
89
|
|
|
266
|
-
|
|
267
|
-
marker is equal to an integer value which is 1 larger than the maximum value the hex digit encoding group size can
|
|
268
|
-
represent (ie it is `2^(hex_digit_encoding_group_size * 4)`).
|
|
90
|
+
## Security Note
|
|
269
91
|
|
|
270
|
-
|
|
92
|
+
**Encoded IDs are not secure**. They are meant to provide obfuscation, not encryption. Do not use them as a security mechanism.
|
|
271
93
|
|
|
272
|
-
|
|
273
|
-
actual encoded integer array is `[15, 1, 16, 14, 2]`.
|
|
94
|
+
## Compare to Alternate Gems
|
|
274
95
|
|
|
275
|
-
|
|
96
|
+
- [prefixed_ids](https://github.com/excid3/prefixed_ids)
|
|
97
|
+
- [obfuscate_id](https://github.com/namick/obfuscate_id)
|
|
98
|
+
- [friendly_id](https://github.com/norman/friendly_id)
|
|
99
|
+
- [with_uid](https://github.com/SPBTV/with_uid)
|
|
100
|
+
- [bullet_train-obfuscates_id](https://github.com/bullet-train-co/bullet_train-core/blob/main/bullet_train-obfuscates_id/app/models/concerns/obfuscates_id.rb)
|
|
276
101
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
```ruby
|
|
280
|
-
# UUIDs will result in long output strings...
|
|
281
|
-
coder.encode_hex("9a566b8b-8618-42ab-8db7-a5a0276401fd")
|
|
282
|
-
# => "5jjy-c8d9-hxp2-qsve-rgh9-rxnt-7nb5-tve7-bf84-vr"
|
|
283
|
-
#
|
|
284
|
-
# but there is an option to help reduce this...
|
|
285
|
-
coder = ::EncodedId::ReversibleId.new(salt: my_salt, hex_digit_encoding_group_size: 32)
|
|
286
|
-
coder.encode_hex("9a566b8b-8618-42ab-8db7-a5a0276401fd")
|
|
287
|
-
# => "vr7m-qra8-m5y6-dkgj-5rqr-q44e-gp4a-52"
|
|
288
|
-
```
|
|
102
|
+
For a detailed comparison, see the [Compared to Other Gems](https://encoded-id.onrender.com/docs/compared-to) documentation page.
|
|
289
103
|
|
|
290
|
-
|
|
104
|
+
## Documentation
|
|
291
105
|
|
|
292
|
-
|
|
106
|
+
Visit [encoded-id.onrender.com](https://encoded-id.onrender.com) for comprehensive documentation including:
|
|
293
107
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
108
|
+
- [EncodedId Core API](https://encoded-id.onrender.com/docs/encoded_id/api)
|
|
109
|
+
- [Rails Integration API](https://encoded-id.onrender.com/docs/encoded_id_rails/api)
|
|
110
|
+
- [Configuration Options](https://encoded-id.onrender.com/docs/encoded_id/configuration)
|
|
111
|
+
- [Examples](https://encoded-id.onrender.com/docs/encoded_id/examples)
|
|
112
|
+
- [Advanced Topics](https://encoded-id.onrender.com/docs/advanced-topics)
|
|
298
113
|
|
|
299
114
|
## Development
|
|
300
115
|
|
|
301
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
|
302
|
-
|
|
303
|
-
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
304
|
-
|
|
305
|
-
### Running tests
|
|
306
|
-
|
|
307
|
-
Run `bundle exec rake test` to run the tests.
|
|
308
|
-
|
|
309
|
-
### Type check
|
|
310
|
-
|
|
311
|
-
First install RBS dependencies:
|
|
116
|
+
After checking out the repo, run `bin/setup` to install dependencies. Run `bundle exec rake test` to run the tests.
|
|
312
117
|
|
|
313
|
-
|
|
314
|
-
rbs collection install
|
|
315
|
-
```
|
|
118
|
+
Run benchmarks with `bin/benchmark <type>` where type is one of: `ips`, `memory`, `comparison`, `profile`, `flamegraph`, or `stress_decode`.
|
|
316
119
|
|
|
317
|
-
|
|
120
|
+
### Documentation
|
|
318
121
|
|
|
319
|
-
|
|
320
|
-
steep check
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
## See also
|
|
324
|
-
|
|
325
|
-
- https://hashids.org
|
|
326
|
-
- https://www.crockford.com/base32.html
|
|
122
|
+
Run `bundle exec rake website:build` to build or `bundle exec rake website:serve` to preview locally.
|
|
327
123
|
|
|
328
124
|
## Contributing
|
|
329
125
|
|
|
@@ -331,7 +127,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/steveg
|
|
|
331
127
|
|
|
332
128
|
## License
|
|
333
129
|
|
|
334
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
335
|
-
|
|
336
|
-
## keywords
|
|
337
|
-
hash ID, friendly ID, obfuscate ID, rails, ActiveRecord, model, slug, vanity URL, friendly URL
|
|
130
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|