evanwalsh-license 0.5.1

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Evan Walsh
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,35 @@
1
+ = License
2
+
3
+ License is a gem that can generate and validate license keys. This can powered paid/shareware apps written in Ruby.
4
+
5
+ == Installation
6
+
7
+ To install, use these commands:
8
+
9
+ gem sources -a http://gems.github.com # you only need to run this once
10
+ sudo gem install evanwalsh-license
11
+
12
+ To use this in your Rails app, add this to your config/environment.rb:
13
+
14
+ Rails::Initializer.run do |config|
15
+ config.gem 'evanwalsh-license', :lib => 'license', :source => 'http://gems.github.com'
16
+ end
17
+
18
+ == Usage
19
+
20
+ For a new license, call the License.new method with the name, email, and product name as the arguments
21
+
22
+ @license = License.new("Evan Walsh","evan@nothingconcept.com","License Gem Test")
23
+
24
+ Then, you can generate a key using the generate method
25
+
26
+ @license.generate # returns 3F7B-D428-ED3A-3636
27
+
28
+ To check if a key is valid, use the validate method with they key as the argument
29
+
30
+ @license.validate("3F7B-D428-ED3A-3636") # returns true
31
+
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2009 Evan Walsh. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "license"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "evan@nothingconcept.com"
10
+ gem.homepage = "http://github.com/evanwalsh/license"
11
+ gem.authors = ["evanwalsh"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
16
+ end
17
+
18
+ require 'rake/testtask'
19
+ Rake::TestTask.new(:test) do |test|
20
+ test.libs << 'lib' << 'test'
21
+ test.pattern = 'test/**/*_test.rb'
22
+ test.verbose = true
23
+ end
24
+
25
+ begin
26
+ require 'rcov/rcovtask'
27
+ Rcov::RcovTask.new do |test|
28
+ test.libs << 'test'
29
+ test.pattern = 'test/**/*_test.rb'
30
+ test.verbose = true
31
+ end
32
+ rescue LoadError
33
+ task :rcov do
34
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
35
+ end
36
+ end
37
+
38
+
39
+ task :default => :test
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ if File.exist?('VERSION.yml')
44
+ config = YAML.load(File.read('VERSION.yml'))
45
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
46
+ else
47
+ version = ""
48
+ end
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "license #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
55
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 5
3
+ :patch: 1
4
+ :major: 0
data/lib/license.rb ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/ruby
2
+
3
+ class License
4
+ require "rubygems"
5
+ require "digest/sha1"
6
+ EXTRA = "evnwlsh"
7
+ attr_reader :name,:email,:product
8
+
9
+ def initialize(name,email,product)
10
+ @name = name
11
+ @email = email
12
+ @product = product
13
+ end
14
+
15
+ def encrypt(str)
16
+ encrypted = Digest::SHA1.hexdigest("#{str}#{EXTRA}")
17
+ return encrypted.slice!(0..3).upcase
18
+ end
19
+
20
+ def generate
21
+ name = @name.split
22
+ key0 = encrypt(@product)
23
+ key1 = encrypt(name[0])
24
+ key2 = encrypt(name[1])
25
+ key3 = encrypt(@email)
26
+
27
+ return "#{key0}-#{key1}-#{key2}-#{key3}"
28
+ end
29
+
30
+ def check(keys)
31
+ valid = String.new
32
+ keys.each do |original,encrypted|
33
+ if encrypt(original).eql? encrypted
34
+ if !valid
35
+ valid = false
36
+ end
37
+ if valid.empty?
38
+ valid = "valid"
39
+ end
40
+ else
41
+ valid = false
42
+ end
43
+ end
44
+ valid
45
+ end
46
+
47
+ def validate(key)
48
+ key = key.split("-")
49
+ name = @name.split
50
+ keys = Hash.new
51
+ keys[@product] = key[0]
52
+ keys[name[0]] = key[1]
53
+ keys[name[1]] = key[2]
54
+ keys[@email] = key[3]
55
+ if check(keys)
56
+ true
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class LicenseTest < Test::Unit::TestCase
4
+ context "A license object" do
5
+
6
+ setup do
7
+ name = "Evan Walsh"
8
+ email = "evan@nothingconcept.com"
9
+ product = "License Gem Test"
10
+ @license = License.new(name,email,product)
11
+ end
12
+
13
+ should "return the name supplied" do
14
+ assert_equal "Evan Walsh", @license.name
15
+ end
16
+
17
+ should "return the email supplied" do
18
+ assert_equal "evan@nothingconcept.com", @license.email
19
+ end
20
+
21
+ should "return the product supplied" do
22
+ assert_equal "License Gem Test", @license.product
23
+ end
24
+
25
+ should "generate a license key" do
26
+ assert_equal "3F7B-D428-ED3A-3636", @license.generate
27
+ end
28
+
29
+ should "return true when a valid key is validated" do
30
+ assert_equal true, @license.validate(@license.generate)
31
+ end
32
+
33
+ should "return nil when an invalid key is validated" do
34
+ assert_equal nil, @license.validate(@license.generate.reverse)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'license'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evanwalsh-license
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - evanwalsh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: evan@nothingconcept.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/license.rb
31
+ - test/license_test.rb
32
+ - test/test_helper.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/evanwalsh/license
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: TODO
59
+ test_files:
60
+ - test/license_test.rb
61
+ - test/test_helper.rb