cuid2 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d073902862a8146e76858ff1e28d218f9c61bc86b44b91827b6bacaa25361823
4
+ data.tar.gz: 070ed76bfff065d1dd51149096e88c7b9fe593adcd462d219c99ce3f4ef264d4
5
+ SHA512:
6
+ metadata.gz: 4fb0a7e863fb5097090493e0a1ff64238a0523255224f7cf5728ff264d781c2a2c04041041a7e539c556e49c812b63134844608e3e656d2c1eb98149c46056fe
7
+ data.tar.gz: 2f45bd473652443707359b676a6340719b268743e832e6224a7cbb6404521206040daca4f3d9f5db963e5296db2a12b88078cf673399e7c7fb767027d164db13
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ Enabled: true
3
+ TargetRubyVersion: 2.6
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: single_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-18
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rubens.stulzer@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in cuid2.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cuid2 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.1.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.7.0)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.1)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.45.1)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.26.0)
43
+ parser (>= 3.2.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.4.2)
46
+
47
+ PLATFORMS
48
+ arm64-darwin-22
49
+
50
+ DEPENDENCIES
51
+ cuid2!
52
+ rake (~> 13.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.21)
55
+
56
+ BUNDLED WITH
57
+ 2.4.6
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Cuid2
2
+
3
+ Ruby implementation of [Cuid2](https://github.com/paralleldrive/cuid2).
4
+
5
+ ## Development
6
+
7
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add cuid2
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install cuid2
18
+
19
+ ## Contributing
20
+
21
+ Bug reports and pull requests are welcome on GitHub at https://github.com/stulzer/cuid2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/stulzer/cuid2/blob/main/CODE_OF_CONDUCT.md).
22
+
23
+ ## Code of Conduct
24
+
25
+ Everyone interacting in the Cuid2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/stulzer/cuid2/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cuid2
4
+ VERSION = '0.1.0'
5
+ end
data/lib/cuid2.rb ADDED
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'cuid2/version'
4
+ require 'digest'
5
+
6
+ module Cuid2
7
+ # Entropy is a class that generates a random string of characters
8
+ class Entropy
9
+ PRIMES = [109_717, 109_721, 109_741, 109_751, 109_789, 109_793, 109_807, 109_819, 109_829, 109_831].freeze
10
+
11
+ def self.create(length)
12
+ entropy = ''
13
+
14
+ while entropy.length < length
15
+ random_prime = PRIMES[(rand * PRIMES.size).floor]
16
+ entropy += (rand * random_prime).floor.to_s(36)
17
+ end
18
+
19
+ entropy[0, length]
20
+ end
21
+ end
22
+
23
+ # Hash is a class that generates a hash of a string
24
+ class Hash
25
+ BIG_LENGTH = 32
26
+
27
+ def self.create(input, length = BIG_LENGTH)
28
+ # The salt should be long enough to be globally unique across the full
29
+ # length of the hash. For simplicity, we use the same length as the
30
+ # intended id output, defaulting to the maximum recommended size.
31
+ salt = Entropy.create(length)
32
+ text = input + salt
33
+
34
+ # Drop the first two characters because they bias the histogram
35
+ # to the left.
36
+ Digest::SHA512.hexdigest(text).chars.map(&:to_i).join.to_i.to_s(36)[2..]
37
+ end
38
+ end
39
+
40
+ # Generate is a class that generates a Cuid2
41
+ class Generate
42
+ DEFAULT_LENGTH = 24
43
+
44
+ def self.call
45
+ new.generate
46
+ end
47
+
48
+ def generate(length = DEFAULT_LENGTH)
49
+ hash_input = (time + Entropy.create(length) + count + fingerprint).to_s
50
+
51
+ "#{random_letter}#{Cuid2::Hash.create(hash_input, length)[2..length]}"
52
+ end
53
+
54
+ private
55
+
56
+ def time
57
+ @time ||= Time.now.to_i.to_s(36)
58
+ end
59
+
60
+ def count
61
+ @count ||= counter.to_i.to_s(36)
62
+ end
63
+
64
+ def random
65
+ @random ||= rand
66
+ end
67
+
68
+ def counter
69
+ (rand + 1) * 2057
70
+ end
71
+
72
+ def random_letter
73
+ ('a'..'z').to_a[(random * 26).floor]
74
+ end
75
+
76
+ def fingerprint
77
+ Cuid2::Hash.create("#{((random + 1) * 2063).floor} #{Object.constants.map(&:to_s)}")
78
+ end
79
+ end
80
+ end
data/sig/cuid2.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Cuid2
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuid2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rubens Stulzer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Secure, collision-resistant ids optimized for horizontal scaling and performance.\
15
+ Next generation UUIDs. Need unique ids in your app? Forget UUIDs which often collide in large apps.\
16
+ Use Cuid2, instead.
17
+ email:
18
+ - rubens.stulzer@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - README.md
29
+ - Rakefile
30
+ - lib/cuid2.rb
31
+ - lib/cuid2/version.rb
32
+ - sig/cuid2.rbs
33
+ homepage: https://github.com/stulzer/cuid2
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/stulzer/cuid2
38
+ source_code_uri: https://github.com/stulzer/cuid2
39
+ changelog_uri: https://github.com/stulzer/cuid2/CHANGELOG.md
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.6.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.4.6
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Cuid2 is a Ruby implementation of the Cuid2 algorithm by Eric Elliott.
59
+ test_files: []