friendly_key 0.0.1
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/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/friendly_key.gemspec +23 -0
- data/lib/friendly_key.rb +24 -0
- data/lib/friendly_key/version.rb +3 -0
- data/spec/friendly_key_spec.rb +60 -0
- data/spec/spec_helper.rb +2 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0edcba1c30b66f336e4c7b535a447165a3a8667
|
4
|
+
data.tar.gz: 85d489186bf0243efd4e31065ab9e3de41a2e216
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba8c8a17ef0144907f36b4e72587642ed9854bc8f08d40687d1d2375a3a9ef7cf634c4a9c0c7c86c6668deeb808775b86d678103a257aecd492eedc750604574
|
7
|
+
data.tar.gz: 7750c3da5fdc09873a85315f99ffebc8c4f11e4ba65fd89ec41f4f5f188050e00009759e6a750b92d2f937b213dc13a049f8a03e466d13818a27b8de5c075f56
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Yamaguchi.Tomoki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# FriendlyKey
|
2
|
+
|
3
|
+
FriendlyKey lets you generate human friendly random strings. It generates random alphanumeric strings without letters and digits that are hard to distinguish from each other, such as I, l and 1.
|
4
|
+
You can use FriendlyKey when you need random strings that you don't want your users to copy and paste but rather enter by themselves, like product keys and random passwords.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'friendly_key'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install friendly_key
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# Generate a rondom string
|
26
|
+
FriendlyKey.generate # => "QNZD9H8FU5UDM848"
|
27
|
+
|
28
|
+
# Specify the length of a random string
|
29
|
+
FriendlyKey.generate(length: 8) #=> "9AFN87ES"
|
30
|
+
|
31
|
+
# Include small letters
|
32
|
+
FriendlyKey.generate(small_letters: true) # => "qK8fLq2REprjHxvG"
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( https://github.com/tyamagu2/friendly_key/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'friendly_key/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "friendly_key"
|
7
|
+
spec.version = FriendlyKey::VERSION
|
8
|
+
spec.authors = ["tyamagu2"]
|
9
|
+
spec.email = ["tyamagu2@gmail.com"]
|
10
|
+
spec.summary = %q{Human friendly random key generator}
|
11
|
+
spec.description = %q{FriendlyKey lets you generate human friendly random strings. It generates random alphanumeric strings without letters and digits that are hard to distinguish from each other, such as I, l and 1.
|
12
|
+
You can use FriendlyKey when you need random strings that you don't want your users to copy and paste but rather enter by themselves, like product keys and random passwords.}
|
13
|
+
spec.homepage = "https://github.com/tyamagu2/friendly_key"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/friendly_key.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
# FriendlyKey lets you generate human friendly random strings. It generates random alphanumeric strings
|
4
|
+
# excluding letters and digits that are hard to distinguish from each other, such as I, l and 1.
|
5
|
+
# You can use FriendlyKey when you need random strings that you don't want your users to copy and paste
|
6
|
+
# but rather enter by themselves, such as product keys and random passwords.
|
7
|
+
module FriendlyKey
|
8
|
+
DEFAULT_SOURCE = [(2..9), ('A'..'H'), ('J'..'N'), ('P'..'Z')].map(&:to_a).flatten.freeze
|
9
|
+
SMALL_LETTERS = [['a'], ('c'..'k'), ['m', 'n'], ('p'..'z')].map(&:to_a).flatten.freeze
|
10
|
+
SOURCE_WITH_SMALL_LETTERS = (DEFAULT_SOURCE + SMALL_LETTERS).freeze
|
11
|
+
|
12
|
+
# Generates a random alphanumeric string excluding letters that are hard for human to distinguish from each otehr.
|
13
|
+
# More specifically, it returns a random string composed of alphabets and digits excluding
|
14
|
+
# 0 (digit zero), 1 (digit one), I (capital letter I), O (capital letter O), b (small letter b), l (small letter l) and o (small letter o).
|
15
|
+
# @option options [Integer] :length length of the string to be generated. Default is 16
|
16
|
+
# @option options [Boolean] :small_letters Include small letters if true. Default is false
|
17
|
+
# @return [String] random string of given length
|
18
|
+
def self.generate(options = {})
|
19
|
+
options = { length: 16, small_letters: false }.merge(options)
|
20
|
+
|
21
|
+
source = options[:small_letters] ? SOURCE_WITH_SMALL_LETTERS : DEFAULT_SOURCE
|
22
|
+
options[:length].times.map { source[SecureRandom.random_number(source.length)] }.join
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe FriendlyKey do
|
4
|
+
describe '.DEFAULT_SOURCE' do
|
5
|
+
let(:expected_source) { [2, 3, 4, 5, 6, 7, 8, 9, *%w(A B C D E F G H J K L M N P Q R S T U V W X Y Z)] }
|
6
|
+
subject { FriendlyKey::DEFAULT_SOURCE }
|
7
|
+
|
8
|
+
it 'contains numbers and capital letters excluding 0, 1, I and O' do
|
9
|
+
is_expected.to match_array(expected_source)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '.SOURCE_WITH_SMALL_LETTERS' do
|
14
|
+
let(:expected_source) { [2, 3, 4, 5, 6, 7, 8, 9, *%w(A B C D E F G H J K L M N P Q R S T U V W X Y Z a c d e f g h i j k m n p q r s t u v w x y z)] }
|
15
|
+
subject { FriendlyKey::SOURCE_WITH_SMALL_LETTERS }
|
16
|
+
|
17
|
+
it 'contains numbers and capital letters excluding 0, 1, I, O, b, l and o' do
|
18
|
+
is_expected.to match_array(expected_source)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.generate' do
|
23
|
+
let(:length) { 10000 }
|
24
|
+
let(:another_key) { FriendlyKey.generate(length: length, small_letters: small_letters) }
|
25
|
+
subject { FriendlyKey.generate(length: length, small_letters: small_letters) }
|
26
|
+
|
27
|
+
context 'without small letters' do
|
28
|
+
let(:small_letters) { false }
|
29
|
+
let(:expected_regexp) { Regexp.new("\\A[2-9A-HJ-NP-Z]{#{length}}\\z") }
|
30
|
+
|
31
|
+
it 'generates a random string of given length without 0, 1, I and O' do
|
32
|
+
is_expected.to match(expected_regexp)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Theoretically, this example could fail. Pratcically, we can ignore that possibility.
|
36
|
+
it 'generates a different string every time' do
|
37
|
+
is_expected.not_to eq another_key
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with small letters' do
|
42
|
+
let(:small_letters) { true }
|
43
|
+
let(:expected_regexp) { Regexp.new("\\A[2-9A-HJ-NP-Zac-kmnp-z]{#{length}}\\z") }
|
44
|
+
|
45
|
+
it 'generates a random string of given length without 0, 1, I, O, b, l and o' do
|
46
|
+
is_expected.to match(expected_regexp)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Theoretically, this example could fail. Pratcically, we can ignore that possibility.
|
50
|
+
it 'contains small letters' do
|
51
|
+
is_expected.to match(/[a-z]/)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Theoretically, this example could fail. Pratcically, we can ignore that possibility.
|
55
|
+
it 'generates a different string every time' do
|
56
|
+
is_expected.not_to eq another_key
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendly_key
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tyamagu2
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |-
|
42
|
+
FriendlyKey lets you generate human friendly random strings. It generates random alphanumeric strings without letters and digits that are hard to distinguish from each other, such as I, l and 1.
|
43
|
+
You can use FriendlyKey when you need random strings that you don't want your users to copy and paste but rather enter by themselves, like product keys and random passwords.
|
44
|
+
email:
|
45
|
+
- tyamagu2@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- friendly_key.gemspec
|
56
|
+
- lib/friendly_key.rb
|
57
|
+
- lib/friendly_key/version.rb
|
58
|
+
- spec/friendly_key_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: https://github.com/tyamagu2/friendly_key
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.2.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Human friendly random key generator
|
84
|
+
test_files:
|
85
|
+
- spec/friendly_key_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
has_rdoc:
|