tokenie 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +1 -0
- data/lib/tokenie/version.rb +3 -0
- data/lib/tokenie.rb +15 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/tokenie_spec.rb +41 -0
- data/tokenie.gemspec +27 -0
- metadata +71 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2011 Andrew Djoga
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= Tokenie
|
2
|
+
|
3
|
+
Tokenie gem provides generation friendly token randomically.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Install as a gem from GemCutter:
|
8
|
+
|
9
|
+
gem install tokenie
|
10
|
+
|
11
|
+
== Getting Started
|
12
|
+
|
13
|
+
Generate a token:
|
14
|
+
|
15
|
+
Tokenie.friendly # => "De6paD"
|
16
|
+
|
17
|
+
By default it uses 6 characters for end string. To change it use :length option:
|
18
|
+
|
19
|
+
Tokenie.friendly(:length => 8) # => "gRaxwBg7"
|
20
|
+
|
21
|
+
If you want to ensure uniqueness you should provide a block which returns false in the case that means a token is not included in your storage:
|
22
|
+
|
23
|
+
existing_tokens = ['qwerty1', 'qwerty2', 'qwerty3']
|
24
|
+
Tokenie.friendly { |token| existing_tokens.include?(token) }
|
25
|
+
|
26
|
+
Example with ActiveRecord:
|
27
|
+
|
28
|
+
Tokenie.friendly { |t| self.class.exists?(:token => t) }
|
29
|
+
|
30
|
+
== Maintainers
|
31
|
+
|
32
|
+
* Andrew Djoga <andrew.djoga@gmail.com>
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/tokenie.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "tokenie/version"
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Tokenie
|
5
|
+
# Generates a friendly string randomically
|
6
|
+
def self.friendly(options = {})
|
7
|
+
length = options[:length] || 6
|
8
|
+
raise ArgumentError, "'length' must be in range (2..16)" if length < 2 || length > 16
|
9
|
+
|
10
|
+
loop do
|
11
|
+
token = SecureRandom.base64(15).tr('+/=lIO0o', 'abcdefgh')[0, length]
|
12
|
+
break token unless block_given? && yield(token)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Tokenie do
|
4
|
+
describe ".friendly" do
|
5
|
+
it "generates a token with 6 characters by default" do
|
6
|
+
SecureRandom.stub(:base64) { 'qwertytoken' }
|
7
|
+
Tokenie.friendly.should eq('qwerty')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "generates a token with 2 characters as passed" do
|
11
|
+
SecureRandom.stub(:base64) { 'qwerty' }
|
12
|
+
Tokenie.friendly(:length => 2).should eq('qw')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "generates a friendly token" do
|
16
|
+
SecureRandom.stub(:base64) { '+/=lIO0o' }
|
17
|
+
Tokenie.friendly(:length => 8).should eq('abcdefgh')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "generates an unique token" do
|
21
|
+
existing_tokens = ['qwerty', 'abcdef']
|
22
|
+
generation_line = ['qwerty', 'abcdef', 'abcde1', 'abcde2']
|
23
|
+
SecureRandom.stub(:base64) { generation_line.shift }
|
24
|
+
|
25
|
+
token = Tokenie.friendly { |t| existing_tokens.include?(t) }
|
26
|
+
token.should eq('abcde1')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises an argument error if length is greater than 16" do
|
30
|
+
expect {
|
31
|
+
Tokenie.friendly(:length => 17)
|
32
|
+
}.to raise_error("'length' must be in range (2..16)")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises an argument error if length is less than 2" do
|
36
|
+
expect {
|
37
|
+
Tokenie.friendly(:length => 1)
|
38
|
+
}.to raise_error("'length' must be in range (2..16)")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/tokenie.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "tokenie/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "tokenie"
|
7
|
+
s.version = Tokenie::VERSION
|
8
|
+
s.authors = ["Andrew Djoga"]
|
9
|
+
s.email = ["andrew.djoga@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/Djo/tokenie/"
|
11
|
+
s.summary = %q{Token generation}
|
12
|
+
s.description = %q{Generation a friendly token randomically}
|
13
|
+
|
14
|
+
s.rubyforge_project = "tokenie"
|
15
|
+
|
16
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE"]
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
s.add_development_dependency 'rspec', '~> 2.6'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tokenie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Djoga
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-18 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70225010708200 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.6'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70225010708200
|
25
|
+
description: Generation a friendly token randomically
|
26
|
+
email:
|
27
|
+
- andrew.djoga@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README.rdoc
|
32
|
+
- LICENSE
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- .rspec
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- lib/tokenie.rb
|
41
|
+
- lib/tokenie/version.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
- spec/tokenie_spec.rb
|
44
|
+
- tokenie.gemspec
|
45
|
+
homepage: http://github.com/Djo/tokenie/
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project: tokenie
|
65
|
+
rubygems_version: 1.8.10
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Token generation
|
69
|
+
test_files:
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/tokenie_spec.rb
|