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
+ :major: 0
3
+ :minor: 6
4
+ :patch: 0
data/lib/license.rb ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/ruby
2
+
3
+ class License
4
+ require "digest/sha1"
5
+ EXTRA = "evnwlsh"
6
+ attr_reader :name,:email,:product
7
+
8
+ def initialize(name,email,product)
9
+ @name = name
10
+ @email = email
11
+ @product = product
12
+ end
13
+
14
+ def encrypt(str)
15
+ encrypted = Digest::SHA1.hexdigest("#{str}#{EXTRA}")
16
+ return encrypted.slice!(0..3).upcase
17
+ end
18
+
19
+ def generate
20
+ name = @name.split
21
+ key0 = encrypt(@product)
22
+ key1 = encrypt(name[0])
23
+ key2 = encrypt(name[1])
24
+ key3 = encrypt(@email)
25
+
26
+ return "#{key0}-#{key1}-#{key2}-#{key3}"
27
+ end
28
+
29
+ def check(keys)
30
+ valid = String.new
31
+ keys.each do |original,encrypted|
32
+ if encrypt(original).eql? encrypted
33
+ if !valid
34
+ valid = false
35
+ end
36
+ if valid.empty?
37
+ valid = "valid"
38
+ end
39
+ else
40
+ valid = false
41
+ end
42
+ end
43
+ valid
44
+ end
45
+
46
+ def validate(key)
47
+ key = key.split("-")
48
+ name = @name.split
49
+ keys = Hash.new
50
+ keys[@product] = key[0]
51
+ keys[name[0]] = key[1]
52
+ keys[name[1]] = key[2]
53
+ keys[@email] = key[3]
54
+ if check(keys)
55
+ true
56
+ end
57
+ end
58
+ 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,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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 -04: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
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: A lib for generating and validating license keys
61
+ test_files:
62
+ - test/license_test.rb
63
+ - test/test_helper.rb