mini_token 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +4 -0
- data/README.rdoc +9 -0
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/db/mini_token.db +0 -0
- data/db/mini_token_test.db +0 -0
- data/lib/mini_token.rb +33 -9
- data/mini_token.gemspec +9 -2
- data/test/helper.rb +1 -1
- data/test/test_mini_token.rb +43 -2
- metadata +36 -6
- data/.rspec +0 -1
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -18,6 +18,15 @@ Generate token with other size
|
|
18
18
|
MiniToken.output(9)
|
19
19
|
# => "2u27Jum8Y"
|
20
20
|
|
21
|
+
Returning all tokens storaged
|
22
|
+
|
23
|
+
MiniToken.all
|
24
|
+
# => [{:id=>1, :token=>"AcEi87"}, {:id=>2, :token=>"I8uEy2"}]
|
25
|
+
|
26
|
+
For truncate the tokens storage
|
27
|
+
|
28
|
+
MiniToken.truncate_tokens
|
29
|
+
|
21
30
|
== Copyright
|
22
31
|
|
23
32
|
Copyright (c) 2010 Antônio Roberto Silva. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -10,6 +10,8 @@ begin
|
|
10
10
|
gem.email = "forevertonny@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/devton/mini_token"
|
12
12
|
gem.authors = ["Antonio Roberto Silva"]
|
13
|
+
gem.add_dependency 'sequel', '>= 3.16.0'
|
14
|
+
gem.add_dependency 'sqlite3-ruby', '>= 1.3.1'
|
13
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
16
|
end
|
15
17
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/db/mini_token.db
ADDED
Binary file
|
Binary file
|
data/lib/mini_token.rb
CHANGED
@@ -1,24 +1,48 @@
|
|
1
|
-
|
1
|
+
require 'sequel'
|
2
|
+
TOKEN_ENV = 'production'
|
3
|
+
|
4
|
+
class MiniToken < Struct.new(:db, :token)
|
2
5
|
TOKEN_SIZE = 6
|
3
|
-
TOKEN_CHARACTERS = '
|
6
|
+
TOKEN_CHARACTERS = (('A'..'Z').to_a+('a'..'z').to_a+('0'..'9').to_a)
|
7
|
+
DB_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', 'db'))
|
4
8
|
|
5
9
|
class << self
|
6
10
|
def output(size = nil)
|
7
11
|
generate { @token_size = size.nil? ? TOKEN_SIZE : size }
|
8
12
|
end
|
13
|
+
|
14
|
+
def truncate_tokens
|
15
|
+
connection
|
16
|
+
@db.truncate
|
17
|
+
end
|
18
|
+
|
19
|
+
def all
|
20
|
+
connection
|
21
|
+
@db.all
|
22
|
+
end
|
9
23
|
|
10
24
|
private
|
11
25
|
|
12
26
|
def generate
|
13
27
|
yield if block_given?
|
14
|
-
token =
|
15
|
-
|
16
|
-
token.join
|
28
|
+
@token = TOKEN_CHARACTERS.sample(@token_size).join
|
29
|
+
exist_token?
|
17
30
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def exist_token?
|
35
|
+
connection
|
36
|
+
@db.first(:token => @token).nil? ? storing : generate unless @token.nil?
|
37
|
+
end
|
38
|
+
|
39
|
+
def storing
|
40
|
+
@token if @db.insert(:token => @token)
|
41
|
+
end
|
42
|
+
|
43
|
+
def connection
|
44
|
+
sequel = Sequel.sqlite(DB_PATH+(TOKEN_ENV=='production' ? '/mini_token.db' : '/mini_token_test.db' ))
|
45
|
+
@db = sequel[:tokens]
|
22
46
|
end
|
23
47
|
end
|
24
48
|
end
|
data/mini_token.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mini_token}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Antonio Roberto Silva"]
|
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
|
|
20
20
|
".bundle/config",
|
21
21
|
".document",
|
22
22
|
".gitignore",
|
23
|
-
".rspec",
|
24
23
|
".rvmrc",
|
25
24
|
"Gemfile",
|
26
25
|
"Gemfile.lock",
|
@@ -28,6 +27,8 @@ Gem::Specification.new do |s|
|
|
28
27
|
"README.rdoc",
|
29
28
|
"Rakefile",
|
30
29
|
"VERSION",
|
30
|
+
"db/mini_token.db",
|
31
|
+
"db/mini_token_test.db",
|
31
32
|
"lib/mini_token.rb",
|
32
33
|
"mini_token.gemspec",
|
33
34
|
"test/helper.rb",
|
@@ -48,9 +49,15 @@ Gem::Specification.new do |s|
|
|
48
49
|
s.specification_version = 3
|
49
50
|
|
50
51
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_runtime_dependency(%q<sequel>, [">= 3.16.0"])
|
53
|
+
s.add_runtime_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
51
54
|
else
|
55
|
+
s.add_dependency(%q<sequel>, [">= 3.16.0"])
|
56
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
52
57
|
end
|
53
58
|
else
|
59
|
+
s.add_dependency(%q<sequel>, [">= 3.16.0"])
|
60
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
data/test/helper.rb
CHANGED
@@ -4,11 +4,11 @@ require 'test/unit'
|
|
4
4
|
require 'minitest/unit'
|
5
5
|
require 'minitest/spec'
|
6
6
|
require 'shoulda'
|
7
|
+
require 'sequel'
|
7
8
|
|
8
9
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
10
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
11
|
|
11
|
-
|
12
12
|
require 'mini_token'
|
13
13
|
Bundler.setup
|
14
14
|
|
data/test/test_mini_token.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
MiniTest::Unit.autorun
|
3
|
+
TOKEN_ENV = 'test'
|
3
4
|
|
4
5
|
describe MiniToken do
|
5
6
|
describe "MiniToken Constants" do
|
@@ -7,12 +8,15 @@ describe MiniToken do
|
|
7
8
|
assert_equal 6, MiniToken::TOKEN_SIZE
|
8
9
|
end
|
9
10
|
|
10
|
-
it "Default Token Characters" do
|
11
|
-
assert_equal '
|
11
|
+
it "Default Token Characters with use join" do
|
12
|
+
assert_equal 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', MiniToken::TOKEN_CHARACTERS.join
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
16
|
describe "MiniToken" do
|
17
|
+
before(:each) do
|
18
|
+
cleaner
|
19
|
+
end
|
16
20
|
it "without size defined the generated token should be have 6 characters" do
|
17
21
|
assert_equal 6, MiniToken.output.size
|
18
22
|
end
|
@@ -28,5 +32,42 @@ describe MiniToken do
|
|
28
32
|
it 'should be return the token string with size' do
|
29
33
|
assert_match /([a-zA-Z0-9]{10})/, MiniToken.output(10)
|
30
34
|
end
|
35
|
+
|
36
|
+
describe ".truncate_tokens" do
|
37
|
+
before(:each) do
|
38
|
+
4.times { MiniToken.output }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should have many tokens " do
|
42
|
+
assert_equal MiniToken.all.size, 4
|
43
|
+
end
|
44
|
+
|
45
|
+
it "when call truncate then don't have more tokens storaged" do
|
46
|
+
MiniToken.truncate_tokens
|
47
|
+
assert_equal MiniToken.all.size, 0
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe ".all" do
|
52
|
+
before(:each) do
|
53
|
+
@token1 = MiniToken.output
|
54
|
+
@token2 = MiniToken.output
|
55
|
+
@token3 = MiniToken.output
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return one hash with all tokens storaged" do
|
59
|
+
assert_equal MiniToken.all, [{:id=>1, :token=>@token1}, {:id=>2, :token=>@token2}, {:id=>3, :token=>@token3}]
|
60
|
+
end
|
61
|
+
end
|
31
62
|
end
|
32
63
|
end
|
64
|
+
|
65
|
+
|
66
|
+
def mock_tokens
|
67
|
+
sequel = Sequel.sqlite(MiniToken::DB_PATH+'/mini_token_test.db')
|
68
|
+
sequel[:tokens]
|
69
|
+
end
|
70
|
+
|
71
|
+
def cleaner
|
72
|
+
mock_tokens.truncate
|
73
|
+
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: mini_token
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
6
|
- 1
|
9
|
-
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Antonio Roberto Silva
|
@@ -16,8 +16,37 @@ cert_chain: []
|
|
16
16
|
|
17
17
|
date: 2010-10-10 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sequel
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 16
|
31
|
+
- 0
|
32
|
+
version: 3.16.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: sqlite3-ruby
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 3
|
46
|
+
- 1
|
47
|
+
version: 1.3.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
21
50
|
description: Generate simple tokens for short url and other things
|
22
51
|
email: forevertonny@gmail.com
|
23
52
|
executables: []
|
@@ -31,7 +60,6 @@ files:
|
|
31
60
|
- .bundle/config
|
32
61
|
- .document
|
33
62
|
- .gitignore
|
34
|
-
- .rspec
|
35
63
|
- .rvmrc
|
36
64
|
- Gemfile
|
37
65
|
- Gemfile.lock
|
@@ -39,6 +67,8 @@ files:
|
|
39
67
|
- README.rdoc
|
40
68
|
- Rakefile
|
41
69
|
- VERSION
|
70
|
+
- db/mini_token.db
|
71
|
+
- db/mini_token_test.db
|
42
72
|
- lib/mini_token.rb
|
43
73
|
- mini_token.gemspec
|
44
74
|
- test/helper.rb
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color --format nested
|