micro_token 0.0.1

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
+ SHA1:
3
+ metadata.gz: 0cf7c23f62271e5acf8354c0ccb9179a91184e65
4
+ data.tar.gz: 0930aa0dfcdec54bf852b050dc61f5b11db1f758
5
+ SHA512:
6
+ metadata.gz: 0c79c1a4d62a19cb6a5d2188d231b4acfc579b022e76e5c6d5d1311c255ce22f6b020f0c250a28df9e5b6b95ad485807ec2cd50990139fd22ae0a631af9ab4b3
7
+ data.tar.gz: d9e7cf0107416672c31e275eb300e44b687377cabde95435f15925c9e2f2ead2583e35985341ee2d31792cd63ba59d5855d66d9683208a9c0861abc42d57f230
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in micro_token.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 James Harton
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,27 @@
1
+ # MicroToken
2
+
3
+ A very small random token generator
4
+
5
+ [![Build Status](https://travis-ci.org/jamesotron/micro_token.png?branch=master)](https://travis-ci.org/jamesotron/micro_token)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'micro_token'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install micro_token
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ require "micro_token/version"
2
+ require 'securerandom'
3
+
4
+ module MicroToken
5
+ ALPHA_CHARS = ('a'..'z').to_a + ('A'..'Z').to_a
6
+
7
+ def self.generate(length=8,format=:alphanumeric)
8
+ (1..length).collect { self.send("_generate_#{format}_char") }.join
9
+ end
10
+
11
+ private
12
+
13
+ def self._generate_alphanumeric_char
14
+ (i = rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr
15
+ end
16
+
17
+ def self._generate_numeric_char
18
+ rand(9)
19
+ end
20
+
21
+ def self._generate_alpha_char
22
+ ALPHA_CHARS[rand(ALPHA_CHARS.size)]
23
+ end
24
+
25
+ private
26
+
27
+ def rand(n)
28
+ SecureRandom.random_number(n)
29
+ end
30
+ end
31
+
@@ -0,0 +1,3 @@
1
+ module MicroToken
2
+ VERSION = "0.0.1"
3
+ end
data/lib/token.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'securerandom'
2
+
3
+ class Token
4
+ ALPHA_CHARS = ('a'..'z').to_a + ('A'..'Z').to_a
5
+
6
+ def self.generate(length=8,format=:alphanumeric)
7
+ (1..length).collect { self.send("_generate_#{format}_char") }.join
8
+ end
9
+
10
+ private
11
+
12
+ def self._generate_alphanumeric_char
13
+ (i = rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr
14
+ end
15
+
16
+ def self._generate_numeric_char
17
+ rand(9)
18
+ end
19
+
20
+ def self._generate_alpha_char
21
+ ALPHA_CHARS[rand(ALPHA_CHARS.size)]
22
+ end
23
+
24
+ private
25
+
26
+ def rand(n)
27
+ SecureRandom.random_number(n)
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'micro_token/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "micro_token"
8
+ spec.version = MicroToken::VERSION
9
+ spec.authors = ["James Harton"]
10
+ spec.email = ["jamesotron@gmail.com"]
11
+ spec.description = %q{A tiny random token generator}
12
+ spec.summary = %q{A tiny random token generator}
13
+ spec.homepage = "https://github.com/jamesotron/micro_token"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ %w[rake rspec].each do |dep|
23
+ spec.add_development_dependency dep
24
+ end
25
+ end
@@ -0,0 +1,58 @@
1
+ require 'micro_token'
2
+
3
+ describe MicroToken do
4
+
5
+ describe '#generate' do
6
+
7
+ context 'with length' do
8
+ context 'set to 12' do
9
+ let(:length) { 12 }
10
+
11
+ it 'generates a string of the correct length' do
12
+ expect(MicroToken.generate(length).size).to eq(length)
13
+ end
14
+ end
15
+
16
+ context 'not set' do
17
+ it 'generates a string of 8 characters long' do
18
+ expect(MicroToken.generate.size).to eq(8)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ context 'with format' do
25
+ subject { MicroToken.generate(8,format) }
26
+
27
+ context 'set to alphanumeric' do
28
+ let(:format) { :alphanumeric }
29
+
30
+ 23.times do
31
+ it 'contains only alphanumeric characters' do
32
+ expect(subject.split.all? { |c| c =~ /[a-zA-Z0-9]/ }).to be_true
33
+ end
34
+ end
35
+ end
36
+
37
+ context 'set to numeric' do
38
+ let(:format) { :numeric }
39
+
40
+ 23.times do
41
+ it 'contains only numerals' do
42
+ expect(subject.split.all? { |c| c =~ /[0-9]/ }).to be_true
43
+ end
44
+ end
45
+ end
46
+
47
+ context 'set to alpha' do
48
+ let(:format) { :alpha }
49
+
50
+ 23.times do
51
+ it 'contains only letters' do
52
+ expect(subject.split.all? { |c| c =~ /[a-zA-Z]/ }).to be_true
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: micro_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Harton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-04 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A tiny random token generator
56
+ email:
57
+ - jamesotron@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/micro_token.rb
69
+ - lib/micro_token/version.rb
70
+ - lib/token.rb
71
+ - micro_token.gemspec
72
+ - spec/lib/micro_token_spec.rb
73
+ homepage: https://github.com/jamesotron/micro_token
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A tiny random token generator
97
+ test_files:
98
+ - spec/lib/micro_token_spec.rb