tokenize 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.
Binary file
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-11-30
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ init.rb
6
+ lib/tokenize.rb
7
+ test/test_helper.rb
8
+ test/test_tokenize.rb
@@ -0,0 +1,48 @@
1
+ = Tokenize
2
+
3
+ * http://github.com/yickster/tokenize
4
+
5
+ == DESCRIPTION:
6
+
7
+ Quick Dirty Token generation
8
+ by Nick Merwin (lemurheavy.com) 11.29.2009
9
+
10
+ == SYNOPSIS:
11
+
12
+ class Foo < ActiveRecord::Base
13
+ tokenize :api_key, :size => 32, :type => :base64
14
+ tokenize :public_key, :size => 64, :type => :hex, :url_safe => false
15
+ end
16
+
17
+ == REQUIREMENTS:
18
+
19
+ none
20
+
21
+ == INSTALL:
22
+
23
+ sudo gem i tokenize
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 Nick Merwin
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/tokenize'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'tokenize' do
14
+ self.developer 'Nick Merwin', 'nick@lemurheavy.com'
15
+ self.rubyforge_name = self.name # TODO this is default value
16
+ self.spec_extras[:rdoc_options] = ""
17
+ self.spec_extras[:homepage] = %q{http://github.com/yickster/tokenize}
18
+ self.extra_deps = [['activerecord']]
19
+
20
+ end
21
+
22
+ require 'newgem/tasks'
23
+ Dir['tasks/**/*.rake'].each { |t| load t }
24
+
25
+ # TODO - want other tests/tasks run by default? Add them to the list
26
+ # remove_task :default
27
+ # task :default => [:spec, :features]
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tokenize'
@@ -0,0 +1,64 @@
1
+ # LemurTokenizer
2
+ # Quick Dirty Token generation
3
+ # by Nick Merwin 11.29.2009
4
+
5
+ module LemurTokenize
6
+ VERSION = "1.0"
7
+
8
+ # Usage:
9
+ # class Foo < ActiveRecord::Base
10
+ # tokenize :api_key, :size => 32
11
+ # tokenize :public_token, :size => 5, :type => :base64
12
+ # end
13
+ #
14
+ # Options:
15
+ #
16
+ # * column
17
+ #
18
+ # The column that will store the token.
19
+ # default - :token
20
+ #
21
+ # * size
22
+ #
23
+ # The length of the token string
24
+ # default - 32
25
+ #
26
+ # * when
27
+ #
28
+ # Callback
29
+ # default - :before_create
30
+ #
31
+ # * type
32
+ #
33
+ # The type of random generator - :hex or :base64
34
+ # default - :hex
35
+ #
36
+ # * url_safe
37
+ #
38
+ # Parses out \W characters (for URL shorteners,etc)
39
+ # default - true
40
+ #
41
+ def tokenize(column=:token, opts={})
42
+ generate_method = "generate_#{column}".to_sym
43
+ define_method generate_method do
44
+ opts = {:column => column, :type => :hex}.merge(opts)
45
+ while true
46
+ proposed_token = ActiveSupport::SecureRandom.send(opts[:type],32)
47
+ proposed_token.gsub!(/\W/,'') unless opts[:url_safe] == false
48
+ #TODO: fill in gaps left by url safety
49
+ proposed_token = proposed_token.to_s[0..(opts[:size] || 32)]
50
+
51
+ break unless self.class.exists?(opts[:column] => proposed_token)
52
+ end
53
+ send "#{opts[:column]}=", proposed_token
54
+ raise IncompatibleTokenColumn unless send(opts[:column]) == proposed_token
55
+ end
56
+
57
+ before_create generate_method
58
+ end
59
+
60
+ class IncompatibleTokenColumn < StandardError; end
61
+ end
62
+
63
+ require "active_record"
64
+ ActiveRecord::Base.extend(LemurTokenize)
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+ require 'factory_girl'
6
+
7
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
8
+ require 'tokenize'
9
+ require File.dirname(__FILE__) + "/../init"
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => 'sqlite3',
13
+ :database => 'test.db'
14
+ )
15
+
16
+ ActiveRecord::Base.connection.drop_table :dummies rescue nil
17
+ ActiveRecord::Base.connection.drop_table :bad_dummies rescue nil
18
+
19
+ ActiveRecord::Base.connection.create_table :dummies do |t|
20
+ t.string :token
21
+ t.string :api_key
22
+ t.string :public_token
23
+
24
+ t.string :private_key
25
+ end
26
+
27
+ ActiveRecord::Base.connection.create_table :bad_dummies do |t|
28
+ t.integer :int_field
29
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ Factory.define(:dummy) {}
4
+ Factory.define(:bad_dummy) {}
5
+
6
+ class Dummy < ActiveRecord::Base
7
+ tokenize
8
+ tokenize :public_token, :size => 5
9
+ tokenize :api_key, :type => :hex
10
+ tokenize :private_key, :type => :base64, :url_safe => false
11
+ end
12
+
13
+ class BadDummy < ActiveRecord::Base
14
+ tokenize :int_field
15
+ end
16
+
17
+ class TokenizeTest < Test::Unit::TestCase
18
+ context "An instance of a class that has tokenized attributes" do
19
+
20
+ setup do
21
+ @dummy = Factory(:dummy)
22
+ end
23
+
24
+ should "create generate methods" do
25
+ assert_respond_to @dummy, :generate_token
26
+ assert_respond_to @dummy, :generate_public_token
27
+ assert_respond_to @dummy, :generate_api_key
28
+ assert_respond_to @dummy, :generate_private_key
29
+ end
30
+
31
+ should "use defaults" do
32
+ assert_match /[a-g0-9]{32}/, @dummy.token
33
+ end
34
+
35
+ should "size generated token correctly" do
36
+ assert_match /[a-g0-9]{5}/, @dummy.api_key
37
+ end
38
+
39
+ should "use correct random method" do
40
+ assert_match /[a-zA-Z0-9\W]{32}/, @dummy.private_key
41
+ end
42
+
43
+ should "ensure url-safe generation" do
44
+ assert_no_match /\W/, @dummy.api_key
45
+ end
46
+
47
+ end
48
+
49
+ context "An instance of a class with an incorrect field type" do
50
+ setup do
51
+ @bad_dummy = Factory.build(:bad_dummy)
52
+ end
53
+
54
+ should "raise an error" do
55
+ assert_raise LemurTokenize::IncompatibleTokenColumn do
56
+ @bad_dummy.save
57
+ end
58
+ end
59
+
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tokenize
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Nick Merwin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDNDCCAhygAwIBAgIBADANBgkqhkiG9w0BAQUFADBAMQ0wCwYDVQQDDARuaWNr
14
+ M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gfTqKd3DB+14BvAyvc5dTbR/RX
30
+ DrswbWvrKlA=
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2009-11-30 00:00:00 -08:00
34
+ default_executable:
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: hoe
48
+ type: :development
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.3
55
+ version:
56
+ description: |-
57
+ Quick Dirty Token generation
58
+ by Nick Merwin (lemurheavy.com) 11.29.2009
59
+ email:
60
+ - nick@lemurheavy.com
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files:
66
+ - History.txt
67
+ - Manifest.txt
68
+ files:
69
+ - History.txt
70
+ - Manifest.txt
71
+ - README.rdoc
72
+ - Rakefile
73
+ - init.rb
74
+ - lib/tokenize.rb
75
+ - test/test_helper.rb
76
+ - test/test_tokenize.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/yickster/tokenize
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project: tokenize
101
+ rubygems_version: 1.3.5
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Quick Dirty Token generation by Nick Merwin (lemurheavy.com) 11.29.2009
105
+ test_files:
106
+ - test/test_helper.rb
107
+ - test/test_tokenize.rb
@@ -0,0 +1 @@
1
+ 0����A�6� Ó��0�c/��=���f�e_��Т]�����NC� K�|$7tjK��=��S+�/1�ܣ�T#��������)Ԃ���F���竸�ZΈ��OC��y�|ߗ:�h�3G�����9gg3%�q��:��2�`�tYDz���|U�\s�T4�t��[�1���G���Td4T�ޛ\P3,JW��%�c#[%���3��P�Mvc��>�F�r˴��r�^��k�Ij it���k8�����WK�