readable-token 0.1.0a

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: bc23a3a8458248c0942242a0533b312da56af97d
4
+ data.tar.gz: 53f48a46cc46d711464e31342b903fcb684659c6
5
+ SHA512:
6
+ metadata.gz: 7ced1ed8683e1d289e05d94eb630de2c12fed73db4be739893f1d278fa70d89d4e68327a3953e99662f202c24307f2d79e5f1da3f5cc4f9d5d132f1662181caa
7
+ data.tar.gz: 15b1988ac12655c9a4a46a389971d10e687affd3bfba4c53185dad5243a8f6ecd6ec1ccb5070c2f78e6d243f3a5d90c02e1b821c60e3cbf45c5a4271bbec57f3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/HISTORY.md ADDED
@@ -0,0 +1,3 @@
1
+ ## HEAD
2
+
3
+ * Initial development/hacking initiated.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ readable-token
2
+ ========
3
+
4
+ [![Build Status](https://secure.travis-ci.org/lantins/readable-token.png?branch=master)](http://travis-ci.org/lantins/readable-token)
5
+ [![Gem Version](https://badge.fury.io/rb/readable-token.png)](http://badge.fury.io/rb/readable-token)
6
+
7
+ A tiny Ruby library for generating human readable "tokens".
8
+
9
+ ## Installation
10
+
11
+ Add this line to your Gemfile:
12
+
13
+ gem 'readable-token'
14
+
15
+ And then execute:
16
+
17
+ bundle install
18
+
19
+ Require the gem in your code:
20
+
21
+ require 'readable-token'
22
+
23
+ ## Usage
24
+
25
+ ### Generate a token
26
+
27
+ ReadableToken.generate
28
+ ReadableToken.generate(min: 6)
29
+ ReadableToken.generate(max: 8)
30
+ ReadableToken.generate(separator: '|')
31
+
32
+ ### Loading your own word list
33
+
34
+ generator = ReadableToken.new(%w(cat penguin password))
35
+
36
+ # Development
37
+
38
+ ## Development Commands
39
+
40
+ # run all tests and build code coverage
41
+ bundle exec rake test
42
+
43
+ # hints where to improve docs
44
+ bundle exec inch
45
+
46
+ # TODO
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ # by default run unit tests.
6
+ task :default => 'test'
7
+
8
+ desc 'Run full test suite and generate code coverage -- COVERAGE=false to disable code coverage'
9
+ Rake::TestTask.new(:test) do |task|
10
+ ENV['COVERAGE'] ||= 'yes'
11
+ task.libs << 'test'
12
+ task.pattern = 'test/**/*_test.rb'
13
+ task.verbose = true
14
+ end
@@ -0,0 +1,60 @@
1
+ require 'readable-token/version'
2
+
3
+ class ReadableToken
4
+
5
+ def initialize
6
+ @records = []
7
+ end
8
+
9
+ def self.generate(opts = {})
10
+ options = {
11
+ min: 8,
12
+ max: nil
13
+ }.merge(opts)
14
+
15
+ words = %w{music venue talk top spoon fork tea coffee soup eat drink chat social happy doughnut beans bistro cafe lunch java ruby penguin lion leopard cake tiger crab fish moo milk owl roast aroma balance barista blend body decaf espresso flavor mocha latte organic friend}
16
+
17
+ segments = []
18
+
19
+ # add number
20
+ segments << rand(1..9).to_s
21
+
22
+ looped = 0
23
+ while current_length_of(segments) < options[:min]
24
+ segments.delete_if(&:nil?).delete_if(&:empty?)
25
+ looped += 1
26
+ break if looped > 20
27
+
28
+ new_word = ''
29
+
30
+ current_length = current_length_of(segments)
31
+ if options[:max] && options[:max] > 0
32
+ remaining = options[:max] - current_length
33
+ new_word = filter_by_length(words, remaining - 1).sample
34
+ else
35
+ new_word = words.sample
36
+ end
37
+
38
+ new_length = current_length_of(segments + [new_word])
39
+ next if options[:max] && options[:max] > 0 && new_length > options[:max]
40
+
41
+ segments << new_word
42
+ end
43
+ segments.delete_if(&:nil?).delete_if(&:empty?)
44
+
45
+ # join it all together to give us a token
46
+ segments.reverse.join('-')
47
+ end
48
+
49
+ private
50
+
51
+ def self.current_length_of(segments)
52
+ segments.join('-').length
53
+ end
54
+
55
+ def self.filter_by_length(words, max)
56
+ filtered = words.select { |w| w.length <= max }
57
+ filtered
58
+ end
59
+
60
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # test_case.rb - A file used to setup the testing enviroment for the library.
3
+ #
4
+
5
+ require 'rubygems'
6
+
7
+ # --- code coverage on MRI 1.9 ruby only, but disabled by default --------------
8
+ if RUBY_VERSION >= '1.9' && RUBY_ENGINE == 'ruby' && ENV['COVERAGE']
9
+ require 'simplecov'
10
+ #SimpleCov.command_name 'test:unit'
11
+ SimpleCov.start do
12
+ # code coverage groups.
13
+ add_filter 'test/'
14
+ end
15
+ end
16
+
17
+ # --- load our dependencies using bundler --------------------------------------
18
+ require 'bundler/setup'
19
+ require 'minitest/autorun'
20
+ require 'minitest/pride'
21
+
22
+ # --- Load lib to test ---------------------------------------------------------
23
+ require 'readable-token'
24
+
25
+ # --- Extend Minitest::Test ----------------------------------------------------
26
+ class ReadableToken::TestCase < Minitest::Test
27
+ end
@@ -0,0 +1,3 @@
1
+ class ReadableToken
2
+ Version = '0.1.0a'
3
+ end
@@ -0,0 +1,33 @@
1
+ require './lib/readable-token/version'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ # gem information/details
5
+ s.name = 'readable-token'
6
+ s.version = ReadableToken::Version
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+ s.summary = 'Generate human readable "tokens".'
9
+ s.license = 'MIT'
10
+ s.homepage = 'https://github.com/lantins/readable-token'
11
+ s.authors = ['Luke Antins']
12
+ s.email = ['luke@float64.uk']
13
+
14
+ # gem settings for what files to include.
15
+ s.files = %w(Rakefile README.md HISTORY.md Gemfile readable-token.gemspec)
16
+ s.files += Dir.glob('{test/**/*,lib/**/*}')
17
+ s.require_paths = ['lib']
18
+
19
+ # development dependencies
20
+ s.add_development_dependency('bundler', '~> 1.0')
21
+ s.add_development_dependency('rake', '>= 9.0')
22
+ s.add_development_dependency('minitest', '~> 5.0')
23
+ s.add_development_dependency('simplecov', '~> 0.7.1')
24
+ s.add_development_dependency('yard', '~> 0.8')
25
+ s.add_development_dependency('inch', '~> 0.3')
26
+
27
+ # long description.
28
+ s.description = <<-EOL
29
+ A tiny Ruby library that generates human readable "tokens" that are easy to read and remember.
30
+ end
31
+ EOL
32
+
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'readable-token/test_case'
2
+
3
+ class GenerateTest < ReadableToken::TestCase
4
+
5
+ N_TIMES = 0..1000
6
+
7
+ def assert_valid_token(token)
8
+ assert token, 'we should have a token'
9
+ assert_match /^([a-z]+-)+\d+$/i, token, 'token does not match expected pattern'
10
+ end
11
+
12
+ def test_generate_with_defaults
13
+ (N_TIMES).each do
14
+ token = ReadableToken.generate
15
+ assert_valid_token(token)
16
+ end
17
+ end
18
+
19
+ def test_generate_min_length
20
+ (N_TIMES).each do
21
+ @min = 20
22
+ token = ReadableToken.generate(min: @min)
23
+ assert_valid_token(token)
24
+ assert_operator token.length, :>=, @min, 'token does not meet minimum length'
25
+ end
26
+ end
27
+
28
+ def test_generate_max_length
29
+ (N_TIMES).each do
30
+ @max = 8
31
+ token = ReadableToken.generate(max: @max)
32
+ assert_valid_token(token)
33
+ assert_operator token.length, :<=, @max, 'token exceeds maximum length'
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,9 @@
1
+ require 'readable-token/test_case'
2
+
3
+ class VersionTest < ReadableToken::TestCase
4
+
5
+ def test_should_have_a_version
6
+ assert ReadableToken::Version, 'unable to access the version number'
7
+ end
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: readable-token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0a
5
+ platform: ruby
6
+ authors:
7
+ - Luke Antins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-30 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.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '9.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '9.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: inch
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ description: |
98
+ A tiny Ruby library that generates human readable "tokens" that are easy to read and remember.
99
+ end
100
+ email:
101
+ - luke@float64.uk
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - Gemfile
107
+ - HISTORY.md
108
+ - README.md
109
+ - Rakefile
110
+ - lib/readable-token.rb
111
+ - lib/readable-token/test_case.rb
112
+ - lib/readable-token/version.rb
113
+ - readable-token.gemspec
114
+ - test/generate_test.rb
115
+ - test/version_test.rb
116
+ homepage: https://github.com/lantins/readable-token
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">"
132
+ - !ruby/object:Gem::Version
133
+ version: 1.3.1
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Generate human readable "tokens".
140
+ test_files: []
141
+ has_rdoc: