token_attr 0.0.1 → 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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +45 -3
- data/lib/token_attr/version.rb +1 -1
- data/lib/token_attr.rb +13 -3
- data/spec/token_attr_spec.rb +44 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2b3cdab36a03426a385de026e0b16b271e94b79
|
4
|
+
data.tar.gz: 39bbbec8a942559030535a44f3b0ead803550177
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6f07c48755e3b73c7f07741fcce0a17f0a58fd113d2c87a6edcf873c31b9f5041f629409e60ee2a4bcd2bef53737e194421ad3e71137df6ce6d46c5075e6706
|
7
|
+
data.tar.gz: 6d7668305f5a4868b1ac2a1b460a24b9b8f5bf520367bea6ce560aca5033bcd641aa23cac3954c2e5e03f6342d6ff4c47e8e72d21c6d3abcbc1b11683d2793a6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,16 +1,58 @@
|
|
1
1
|
# token_attr
|
2
2
|
|
3
|
-
|
3
|
+
Unique random token generator for ActiveRecord.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add `token_attr` to your Gemfile:
|
8
8
|
|
9
|
-
gem 'token_attr'
|
9
|
+
gem 'token_attr', '~> 0.1.0'
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
|
13
|
+
```
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
include TokenAttr
|
16
|
+
token_attr :token
|
17
|
+
end
|
18
|
+
|
19
|
+
user = User.new
|
20
|
+
user.valid?
|
21
|
+
user.token # => "b8bd30ff"
|
22
|
+
```
|
23
|
+
|
24
|
+
The token is generated in a `before_validation` callback.
|
25
|
+
|
26
|
+
### Options
|
27
|
+
|
28
|
+
#### Length
|
29
|
+
|
30
|
+
The length of the token to generate.
|
31
|
+
|
32
|
+
Default: 8
|
33
|
+
|
34
|
+
```
|
35
|
+
token_attr :token, length: 40
|
36
|
+
```
|
37
|
+
|
38
|
+
#### Alphabet
|
39
|
+
|
40
|
+
The alphabet to use to generate the token.
|
41
|
+
|
42
|
+
Uses hexadecimal characters by default.
|
43
|
+
|
44
|
+
Accepted values:
|
45
|
+
- `:alphabetic` - any character from a to z (both lower and upper case)
|
46
|
+
- `:numeric` - any number
|
47
|
+
- `:alphanumeric` - any character or number
|
48
|
+
- a string - a string of your choice of the characters you want to use
|
49
|
+
|
50
|
+
```
|
51
|
+
token_attr :token, alphabet: :numeric # => "82051173"
|
52
|
+
token_attr :token, alphabet: :alphabetic # => "xqnInSJa"
|
53
|
+
token_attr :token, alphabet: :alphanumeric # => "61nD0lUo"
|
54
|
+
token_attr :token, alphabet: "token" # => "ktnekoet"
|
55
|
+
```
|
14
56
|
|
15
57
|
## Contributing
|
16
58
|
|
data/lib/token_attr/version.rb
CHANGED
data/lib/token_attr.rb
CHANGED
@@ -6,7 +6,9 @@ module TokenAttr
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
DEFAULT_TOKEN_LENGTH = 8.freeze
|
9
|
-
|
9
|
+
ALPHABETIC_ALPHABET = [('a'..'z'),('A'..'Z')].map(&:to_a).flatten.freeze
|
10
|
+
NUMERIC_ALPHABET = [(0..9)].map(&:to_a).flatten.freeze
|
11
|
+
ALPHANUMERIC_ALPHABET = [ALPHABETIC_ALPHABET, NUMERIC_ALPHABET].flatten.freeze
|
10
12
|
|
11
13
|
class TooManyAttemptsError < StandardError
|
12
14
|
attr_reader :attribute, :token
|
@@ -35,8 +37,16 @@ module TokenAttr
|
|
35
37
|
token_length = options.fetch(:length, DEFAULT_TOKEN_LENGTH)
|
36
38
|
|
37
39
|
if alphabet = options[:alphabet]
|
38
|
-
|
39
|
-
|
40
|
+
alphabet_array = case alphabet
|
41
|
+
when :alphanumeric
|
42
|
+
ALPHANUMERIC_ALPHABET
|
43
|
+
when :alphabetic
|
44
|
+
ALPHABETIC_ALPHABET
|
45
|
+
when :numeric
|
46
|
+
NUMERIC_ALPHABET
|
47
|
+
else
|
48
|
+
alphabet.split('')
|
49
|
+
end
|
40
50
|
(0...token_length).map{ alphabet_array.sample }.join
|
41
51
|
else
|
42
52
|
hex_length = (token_length / 2.0).ceil # 2 characters per length
|
data/spec/token_attr_spec.rb
CHANGED
@@ -19,10 +19,22 @@ describe TokenAttr do
|
|
19
19
|
token_attr :token, alphabet: 'abc123'
|
20
20
|
end
|
21
21
|
|
22
|
-
class
|
22
|
+
class ModelWithAlphanumericAlphabet < ActiveRecord::Base
|
23
23
|
self.table_name = 'models'
|
24
24
|
include TokenAttr
|
25
|
-
token_attr :token, alphabet: :
|
25
|
+
token_attr :token, alphabet: :alphanumeric
|
26
|
+
end
|
27
|
+
|
28
|
+
class ModelWithAlphabeticAlphabet < ActiveRecord::Base
|
29
|
+
self.table_name = 'models'
|
30
|
+
include TokenAttr
|
31
|
+
token_attr :token, alphabet: :alphabetic
|
32
|
+
end
|
33
|
+
|
34
|
+
class ModelWithNumericAlphabet < ActiveRecord::Base
|
35
|
+
self.table_name = 'models'
|
36
|
+
include TokenAttr
|
37
|
+
token_attr :token, alphabet: :numeric
|
26
38
|
end
|
27
39
|
|
28
40
|
class ModelWithMultipleTokens < ActiveRecord::Base
|
@@ -88,13 +100,28 @@ describe TokenAttr do
|
|
88
100
|
model.token.length.should == 12
|
89
101
|
end
|
90
102
|
|
91
|
-
it "generates a token with
|
92
|
-
|
93
|
-
|
103
|
+
it "generates a token with alphanumeric characters when the alphabet is :alphanumeric" do
|
104
|
+
fake_alphabet('TokenAttr::ALPHANUMERIC_ALPHABET', times: 8, sample: 'T')
|
105
|
+
|
106
|
+
model = ModelWithAlphanumericAlphabet.new
|
94
107
|
model.valid?
|
95
|
-
model.token.
|
96
|
-
|
97
|
-
|
108
|
+
model.token.should == 'TTTTTTTT'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "generates a token with alphabetic characters when the alphabet is :alphabetic" do
|
112
|
+
fake_alphabet('TokenAttr::ALPHABETIC_ALPHABET', times: 8, sample: 't')
|
113
|
+
|
114
|
+
model = ModelWithAlphabeticAlphabet.new
|
115
|
+
model.valid?
|
116
|
+
model.token.should == 'tttttttt'
|
117
|
+
end
|
118
|
+
|
119
|
+
it "generates a token with numeric characters when the alphabet is :numeric" do
|
120
|
+
fake_alphabet('TokenAttr::NUMERIC_ALPHABET', times: 8, sample: '0')
|
121
|
+
|
122
|
+
model = ModelWithNumericAlphabet.new
|
123
|
+
model.valid?
|
124
|
+
model.token.should == '00000000'
|
98
125
|
end
|
99
126
|
end
|
100
127
|
|
@@ -119,4 +146,13 @@ describe TokenAttr do
|
|
119
146
|
end
|
120
147
|
end
|
121
148
|
|
149
|
+
protected
|
150
|
+
|
151
|
+
def fake_alphabet(alphabet_const, times: 8, sample: 'T')
|
152
|
+
fake = double('Array')
|
153
|
+
fake.stub(:split).and_return(fake)
|
154
|
+
fake.should_receive(:sample).exactly(times).times.and_return(sample)
|
155
|
+
stub_const(alphabet_const, fake)
|
156
|
+
end
|
157
|
+
|
122
158
|
end
|