crockford 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e80b42112ca5bce44757787938cd25743febab88aba91f79dfe442755d64237
4
+ data.tar.gz: 3e9ab1459f544e4d4b9b3705bebf466b8dfbb5770c3460814500f29e846c946b
5
+ SHA512:
6
+ metadata.gz: 02c36e35fa698a059b9b4a6ca736c0c027c1c733890d8bc7c379b0dfdb4594c2a3afc324b75672f1373b5c1d0386c65983f3f16db640f24b7a4e2db0e26ab3b6
7
+ data.tar.gz: 02aa929e0fcd02bb612cf9a0735ef6fd3e67f0544feca2d3a941c387725d1f9eef67eb493909d99b747e1a76b59f8b9cb4e1f6b20f675236d58a675d1b2ef52a
@@ -0,0 +1,22 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ strategy:
9
+ matrix:
10
+ ruby-version: [2.6.7, 2.7.3, 3.0.1]
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby-version }}
20
+ bundler-cache: true
21
+ - name: Run the default task
22
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,50 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.6
8
+ NewCops: enable
9
+ Exclude:
10
+ - bin/*
11
+ - vendor/**/*
12
+
13
+ Style/StringLiterals:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/StringLiteralsInInterpolation:
18
+ Enabled: true
19
+ EnforcedStyle: double_quotes
20
+
21
+ Style/NumericLiterals:
22
+ Enabled: false
23
+
24
+ Layout/LineLength:
25
+ Max: 120
26
+
27
+ Layout/EmptyLinesAroundBlockBody:
28
+ Enabled: false
29
+
30
+ Layout/EmptyLinesAroundClassBody:
31
+ EnforcedStyle: empty_lines_except_namespace
32
+
33
+ Layout/EmptyLinesAroundModuleBody:
34
+ EnforcedStyle: empty_lines_except_namespace
35
+
36
+ Layout/MultilineMethodCallIndentation:
37
+ EnforcedStyle: indented
38
+
39
+ Metrics/BlockLength:
40
+ Exclude:
41
+ - "spec/**/*_spec.rb"
42
+
43
+ Metrics/MethodLength:
44
+ Max: 20
45
+
46
+ RSpec/MultipleExpectations:
47
+ Enabled: false
48
+
49
+ RSpec/DescribedClass:
50
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-05
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 iain@iain.nl. 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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in crockford.gemspec
6
+ gemspec
7
+
8
+ gem "pry"
9
+ gem "rake"
10
+ gem "rspec"
11
+ gem "rubocop"
12
+ gem "rubocop-performance"
13
+ gem "rubocop-rake"
14
+ gem "rubocop-rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crockford (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.4.4)
12
+ method_source (1.0.0)
13
+ parallel (1.20.1)
14
+ parser (3.0.1.1)
15
+ ast (~> 2.4.1)
16
+ pry (0.14.1)
17
+ coderay (~> 1.1)
18
+ method_source (~> 1.0)
19
+ rainbow (3.0.0)
20
+ rake (13.0.3)
21
+ regexp_parser (2.1.1)
22
+ rexml (3.2.5)
23
+ rspec (3.10.0)
24
+ rspec-core (~> 3.10.0)
25
+ rspec-expectations (~> 3.10.0)
26
+ rspec-mocks (~> 3.10.0)
27
+ rspec-core (3.10.1)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-expectations (3.10.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-mocks (3.10.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-support (3.10.2)
36
+ rubocop (1.14.0)
37
+ parallel (~> 1.10)
38
+ parser (>= 3.0.0.0)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ regexp_parser (>= 1.8, < 3.0)
41
+ rexml
42
+ rubocop-ast (>= 1.5.0, < 2.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 3.0)
45
+ rubocop-ast (1.5.0)
46
+ parser (>= 3.0.1.1)
47
+ rubocop-performance (1.11.2)
48
+ rubocop (>= 1.7.0, < 2.0)
49
+ rubocop-ast (>= 0.4.0)
50
+ rubocop-rake (0.5.1)
51
+ rubocop
52
+ rubocop-rspec (2.3.0)
53
+ rubocop (~> 1.0)
54
+ rubocop-ast (>= 1.1.0)
55
+ ruby-progressbar (1.11.0)
56
+ unicode-display_width (2.0.0)
57
+
58
+ PLATFORMS
59
+ x86_64-darwin-20
60
+ x86_64-linux
61
+
62
+ DEPENDENCIES
63
+ crockford!
64
+ pry
65
+ rake
66
+ rspec
67
+ rubocop
68
+ rubocop-performance
69
+ rubocop-rake
70
+ rubocop-rspec
71
+
72
+ BUNDLED WITH
73
+ 2.2.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Iain Hecker
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,121 @@
1
+ # Crockford
2
+
3
+ Douglas Crockford has described an alternate version of Base32. This is **NOT** [RFC 4648](https://tools.ietf.org/html/rfc4648).
4
+
5
+ It excludes the letters `I`, `L`, and `O`, to avoid confusion with digits. It
6
+ also excludes the letter `U` to reduce the likelihood of accidental obscenity.
7
+
8
+ Instead of rejecting the characters it excludes, it normalizes them. This means
9
+ that if somebody mistook a `0` for a `O`, we correct it instead of rejecting it.
10
+ This makes it great for cases where a person needs to type in the code by hand,
11
+ like a serial number, or a token sent via SMS.
12
+
13
+ For more information, see [Douglas Crockford's own website](https://www.crockford.com/base32.html).
14
+
15
+
16
+ Note: Crockford's Base32 also allows for a checksum character. This
17
+ functionality has not been implemented yet.
18
+
19
+ ## Requirements
20
+
21
+ The encoding scheme is required to
22
+
23
+ * Be human readable and machine readable.
24
+ * Be compact. Humans have difficulty in manipulating long strings of arbitrary symbols.
25
+ * Be error resistant. Entering the symbols must not require keyboarding gymnastics.
26
+ * Be pronounceable. Humans should be able to accurately transmit the symbols to other humans using a telephone.
27
+
28
+ ## Usage
29
+
30
+ Encoding and decoding numbers:
31
+
32
+ ```ruby
33
+ Crockford.encode_number(1234) # => "16J"
34
+ Crockford.decode_number("16J") # => 1234
35
+ ```
36
+
37
+ You can split up longer codes with hyphens. Hyphens are ignored when decoding:
38
+
39
+ ```ruby
40
+ Crockford.encode_number(1234567890, split: 4) # => "14S-C0PJ"
41
+ Crockford.decode_number("14S-C0-PJ") # => 1234567890
42
+ ```
43
+
44
+ Codes are automatically normalized (unknown characters will turn into `?`):
45
+
46
+ ```ruby
47
+ Crockford.decode_number("oil") # => 33
48
+ Crockford.decode_number("011") # => 33
49
+ Crockford.normalize("oil") # => "011"
50
+ Crockford.normalize("wrong@") # => "WR0NG?"
51
+ Crockford.normalize("wrong@", unknown: "_") # => "WR0NG_"
52
+ ```
53
+
54
+ You can encode and decode any string (or any raw bytes):
55
+
56
+ ```ruby
57
+ data = SecureRandom.bytes(10) # => "\x06\x9D\xA5\xC1\x9C8\xA9W\xE9^"
58
+ code = Crockford.encode_string(data, split: 4) # => "TET-BGCW-72MN-FTAY"
59
+ Crockford.decode_string(code) == data # => true
60
+ ```
61
+
62
+ Generate a code:
63
+
64
+ ```ruby
65
+ Crockford.generate(length: 16, split: 4) # => "H222-WEQ8-RHE3-P707"
66
+ ```
67
+
68
+ ## Installation
69
+
70
+ Add this line to your application's Gemfile:
71
+
72
+ ```ruby
73
+ gem 'crockford'
74
+ ```
75
+
76
+ And then execute:
77
+
78
+ $ bundle install
79
+
80
+ Or install it yourself as:
81
+
82
+ $ gem install crockford
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
87
+ run `rake` to run the tests. You can also run `bin/console` for an interactive
88
+ prompt that will allow you to experiment.
89
+
90
+ To install this gem onto your local machine, run `bundle exec rake install`.
91
+ To release a new version, update the version number in `version.rb`, and
92
+ then run `bundle exec rake release`, which will create a git tag for the
93
+ version, push git commits and the created tag, and push the `.gem` file to
94
+ [rubygems.org](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at
99
+ https://github.com/iain/crockford. This project is intended to be a safe,
100
+ welcoming space for collaboration, and contributors are expected to adhere to
101
+ the [code of conduct](https://github.com/iain/crockford/blob/main/CODE_OF_CONDUCT.md).
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the
106
+ [MIT License](https://opensource.org/licenses/MIT).
107
+
108
+ ## Code of Conduct
109
+
110
+ Everyone interacting in the Crockford project's codebases, issue
111
+ trackers, chat rooms and mailing lists is expected to follow the
112
+ [code of conduct](https://github.com/iain/crockford/blob/main/CODE_OF_CONDUCT.md).
113
+
114
+ ## Prior art
115
+
116
+ Some of these haven't been updated in over a decade:
117
+
118
+ * [base32-crockford](https://rubygems.org/gems/base32-crockford)
119
+ * [base32-multi](https://rubygems.org/gems/base32-multi)
120
+ * [base32-url](https://rubygems.org/gems/base32-url)
121
+ * [base32-alphabets](https://rubygems.org/gems/base32-alphabets)
data/Rakefile ADDED
@@ -0,0 +1,11 @@
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
+ RuboCop::RakeTask.new
10
+
11
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "crockford"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("bundle", __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/crockford.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/crockford/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "crockford"
7
+ spec.version = Crockford::VERSION
8
+ spec.authors = ["iain"]
9
+ spec.email = ["iain@iain.nl"]
10
+
11
+ spec.summary = "Crockford's Base32"
12
+ spec.description = "Crockford's Base32"
13
+ spec.homepage = "https://github.com/iain/crockford"
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = File.join(spec.homepage, "blob/HEAD/CHANGELOG.md")
21
+ spec.metadata["bug_tracker_uri"] = File.join(spec.homepage, "issues")
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
data/lib/crockford.rb ADDED
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "crockford/version"
4
+
5
+ # Encodes and decodes Crockford's Base32 variation.
6
+ module Crockford
7
+
8
+ ENCODER = %w[0 1 2 3 4 5 6 7 8 9 A B C D E F G H J K M N P Q R S T V W X Y Z].freeze
9
+ DECODER = ENCODER.each_with_index.to_h.transform_keys(&:to_s).merge({ "I" => 1, "L" => 1, "O" => 0 }).freeze
10
+
11
+ class << self
12
+
13
+ def encode_number(number, **kwargs)
14
+ string = number.to_s(2).each_char.reverse_each.each_slice(5).map do |bits|
15
+ ENCODER[bits.reverse.join.to_i(2)]
16
+ end.reverse.join
17
+
18
+ format_code(string, **kwargs)
19
+ end
20
+
21
+ def decode_number(string)
22
+ clean_code(string).each_char.inject(0) do |result, char|
23
+ val = DECODER[char]
24
+ return nil if val.nil?
25
+
26
+ (result << 5) + val
27
+ end
28
+ end
29
+
30
+ def encode_string(string, **kwargs)
31
+ number = string.bytes.map { |byte| format("%08b", byte) }.join.to_i(2)
32
+ encode_number(number, **kwargs)
33
+ end
34
+
35
+ def decode_string(string)
36
+ number = decode_number(string)
37
+ return nil if number.nil?
38
+
39
+ number.to_s(2).each_char.reverse_each.each_slice(8).map do |byte|
40
+ byte.reverse.join.to_i(2)
41
+ end.reverse.pack("C*")
42
+ end
43
+
44
+ def normalize(string, unknown: "?", **kwargs)
45
+ string = clean_code(string).each_char.inject("") do |memo, char|
46
+ memo + ((index = DECODER[char]) ? ENCODER[index] : unknown)
47
+ end
48
+ format_code(string, **kwargs)
49
+ end
50
+
51
+ def valid?(string)
52
+ !decode_number(string).nil?
53
+ end
54
+
55
+ def generate(length:, split: false)
56
+ encode_number(SecureRandom.random_number(32**length), split: split, length: length)
57
+ end
58
+
59
+ private
60
+
61
+ def clean_code(string)
62
+ string.delete("-").upcase
63
+ end
64
+
65
+ def format_code(string, length: nil, split: false)
66
+ string = string.rjust(length, "0") if length
67
+
68
+ if split
69
+ string = string
70
+ .each_char
71
+ .reverse_each
72
+ .each_slice(split)
73
+ .map { |segment| segment.reverse.join }
74
+ .reverse
75
+ .join("-")
76
+ end
77
+
78
+ string
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crockford
4
+
5
+ VERSION = "0.1.0"
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crockford
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - iain
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Crockford's Base32
14
+ email:
15
+ - iain@iain.nl
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/rspec
33
+ - bin/setup
34
+ - crockford.gemspec
35
+ - lib/crockford.rb
36
+ - lib/crockford/version.rb
37
+ homepage: https://github.com/iain/crockford
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/iain/crockford
42
+ source_code_uri: https://github.com/iain/crockford
43
+ changelog_uri: https://github.com/iain/crockford/blob/HEAD/CHANGELOG.md
44
+ bug_tracker_uri: https://github.com/iain/crockford/issues
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.2.16
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Crockford's Base32
64
+ test_files: []