encoded_id 0.1.0
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 +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +14 -0
- data/lib/encoded_id/reversible_id.rb +67 -0
- data/lib/encoded_id/version.rb +5 -0
- data/lib/encoded_id.rb +10 -0
- data/sig/encoded_id.rbs +4 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 87f85eb4edadbf1953f3f3343f3b4166f8bc0058593a66507f07f7dcd589d0e0
|
4
|
+
data.tar.gz: 03fe5a25403ad4d75279f6e7c66d5043075e1b5a42b359a117b34c83a86b269e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f6ad0446336da43661dd07ab253c8e80e3ded3b0fb81744c9bf662ec6ddfe3937df17bb5956f538e1b3d4e059eb074e94843e01f68e37131aaf1d7aec89bd61
|
7
|
+
data.tar.gz: 9a6daafde92782a631571c67557665adadd5998151319a4330d3d6700ec33bf1188e37d76e7c0341721703d7de83dc72840d60b7ea9fb57655cc9fdfc3e5c967
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Stephen Ierodiaconou
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# EncodedId
|
2
|
+
|
3
|
+
Encode your numerical IDs (eg record primary keys) into obfuscated strings that can be used in URLs.
|
4
|
+
|
5
|
+
`::EncodedId::ReversibleId.new(salt: my_salt).encode(123)` => `"p5w9-z27j"`
|
6
|
+
|
7
|
+
The obfuscated strings are reversible, so you can decode them back into the original numerical IDs. Also supports
|
8
|
+
encoding multiple IDs at once.
|
9
|
+
|
10
|
+
```
|
11
|
+
reversibles = ::EncodedId::ReversibleId.new(salt: my_salt)
|
12
|
+
reversibles.encode([78, 45]) # "7aq6-0zqw"
|
13
|
+
reversibles.decode("7aq6-0zqw") # [78, 45]
|
14
|
+
```
|
15
|
+
|
16
|
+
Length of the ID, the alphabet used, and the number of characters per group can be configured.
|
17
|
+
|
18
|
+
The custom alphabet (at least 16 characters needed) and character group sizes is to make the IDs easier to read or share.
|
19
|
+
Easily confused characters (eg `i` and `j`, `0` and `O`, `1` and `I` etc) are mapped to counterpart characters, to help
|
20
|
+
common mistakes when sharing (eg customer over phone to customer service agent).
|
21
|
+
|
22
|
+
## Features
|
23
|
+
|
24
|
+
Build with https://hashids.org
|
25
|
+
|
26
|
+
* Hashids are reversible, no need to persist the generated Id
|
27
|
+
* supports slugged IDs (eg 'beef-tenderloins-prime--p5w9-z27j')
|
28
|
+
* supports multiple IDs encoded in one `EncodedId` (eg '7aq6-0zqw' decodes to `[78, 45]`)
|
29
|
+
* uses a reduced character set (Crockford alphabet) & ids split into groups of letters, ie 'human-readability'
|
30
|
+
|
31
|
+
To use with **Rails** check out the `encoded_id-rails` gem.
|
32
|
+
|
33
|
+
## Compared to alternate Gems
|
34
|
+
|
35
|
+
- https://github.com/excid3/prefixed_ids
|
36
|
+
- https://github.com/namick/obfuscate_id
|
37
|
+
- https://github.com/norman/friendly_id
|
38
|
+
- https://github.com/SPBTV/with_uid
|
39
|
+
|
40
|
+
## See also
|
41
|
+
|
42
|
+
- https://hashids.org
|
43
|
+
- https://www.crockford.com/wrmg/base32.html
|
44
|
+
|
45
|
+
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Install the gem and add to the application's Gemfile by executing:
|
49
|
+
|
50
|
+
$ bundle add encoded_id
|
51
|
+
|
52
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
53
|
+
|
54
|
+
$ gem install encoded_id
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
TODO: Write usage instructions here
|
59
|
+
|
60
|
+
### Rails
|
61
|
+
|
62
|
+
To use with rails try the `encoded_id-rails` gem.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class User < ApplicationRecord
|
66
|
+
include EncodedId::WithUid
|
67
|
+
end
|
68
|
+
|
69
|
+
User.find_by_uid("p5w9-z27j") # => #<User id: 78>
|
70
|
+
```
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also
|
75
|
+
run `bin/console` for an interactive prompt that will allow you to experiment.
|
76
|
+
|
77
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version
|
78
|
+
number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git
|
79
|
+
commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/encoded_id.
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
88
|
+
|
89
|
+
## keywords
|
90
|
+
hash ID, friendly ID, obfuscate ID, rails, ActiveRecord, model, slug, vanity URL, friendly URL
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "standard/rake"
|
13
|
+
|
14
|
+
task default: %i[test standard]
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hashids"
|
4
|
+
|
5
|
+
# Hashid with a reduced character set Crockford alphabet and split groups
|
6
|
+
# See: https://www.crockford.com/wrmg/base32.html
|
7
|
+
# Build with https://hashids.org
|
8
|
+
# Note hashIds already has a biuld in profanity limitation algorithm
|
9
|
+
module EncodedId
|
10
|
+
class ReversibleId
|
11
|
+
ALPHABET = "0123456789abcdefghjkmnpqrstuvwxyz"
|
12
|
+
|
13
|
+
def initialize(salt:, length: 8, split_at: 4, alphabet: ALPHABET)
|
14
|
+
unique_alphabet = alphabet.chars.uniq
|
15
|
+
raise InvalidAlphabetError, "Alphabet must be at least 16 characters" if unique_alphabet.size < 16
|
16
|
+
|
17
|
+
@human_friendly_alphabet = unique_alphabet.join
|
18
|
+
@salt = salt
|
19
|
+
@length = length
|
20
|
+
@split_at = split_at
|
21
|
+
end
|
22
|
+
|
23
|
+
# Encode the input values into a hash
|
24
|
+
def encode(*values)
|
25
|
+
uid = convert_to_string(uid_generator.encode(*values))
|
26
|
+
uid = humanize_length(uid) unless split_at.nil?
|
27
|
+
uid
|
28
|
+
end
|
29
|
+
|
30
|
+
# Decode the hash to original array
|
31
|
+
def decode(str)
|
32
|
+
uid_generator.decode(convert_to_hash(str))
|
33
|
+
rescue ::Hashids::InputError => e
|
34
|
+
raise EncodedIdFormatError, e.message
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
attr_reader :salt, :length, :human_friendly_alphabet, :split_at
|
40
|
+
|
41
|
+
def uid_generator
|
42
|
+
@uid_generator ||= ::Hashids.new(salt, length, human_friendly_alphabet)
|
43
|
+
end
|
44
|
+
|
45
|
+
def convert_to_string(hash)
|
46
|
+
hash.is_a?(Array) ? hash.join : hash.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
def split_regex
|
50
|
+
@split_regex ||= /.{#{split_at}}(?=.)/
|
51
|
+
end
|
52
|
+
|
53
|
+
def humanize_length(hash)
|
54
|
+
hash.gsub(split_regex, '\0-')
|
55
|
+
end
|
56
|
+
|
57
|
+
def convert_to_hash(str)
|
58
|
+
map_crockford_set(str.delete("-").downcase)
|
59
|
+
end
|
60
|
+
|
61
|
+
def map_crockford_set(str)
|
62
|
+
# Crockford suggest i==1 , but I think i==j is more appropriate as we
|
63
|
+
# only use lowercase
|
64
|
+
str.tr("o", "0").tr("l", "1").tr("i", "j")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/encoded_id.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "encoded_id/version"
|
4
|
+
require_relative "encoded_id/reversible_id"
|
5
|
+
|
6
|
+
module EncodedId
|
7
|
+
class EncodedIdFormatError < Hashids::InputError; end
|
8
|
+
|
9
|
+
class InvalidAlphabetError < Hashids::AlphabetError; end
|
10
|
+
end
|
data/sig/encoded_id.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encoded_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Ierodiaconou
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashids
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description: Encode your numerical IDs (eg record primary keys) into obfuscated strings
|
28
|
+
that can be used in URLs. The obfuscated strings are reversible, so you can decode
|
29
|
+
them back into the original numerical IDs. Supports encoding multiple IDs at once,
|
30
|
+
and generating IDs with custom alphabets and separators to make the IDs easier to
|
31
|
+
read or share.
|
32
|
+
email:
|
33
|
+
- stevegeek@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- CHANGELOG.md
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/encoded_id.rb
|
44
|
+
- lib/encoded_id/reversible_id.rb
|
45
|
+
- lib/encoded_id/version.rb
|
46
|
+
- sig/encoded_id.rbs
|
47
|
+
homepage: https://github.com/stevegeek/encoded_id
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata:
|
51
|
+
homepage_uri: https://github.com/stevegeek/encoded_id
|
52
|
+
source_code_uri: https://github.com/stevegeek/encoded_id
|
53
|
+
changelog_uri: https://github.com/stevegeek/encoded_id/blob/master/CHANGELOG.md
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.7.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.1.4
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: EncodedId is a gem for creating reversible obfuscated IDs from numerical
|
73
|
+
IDs. It uses Hash IDs under the hood.
|
74
|
+
test_files: []
|