san 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +21 -0
  3. data/lib/san.rb +39 -0
  4. data/spec/san_spec.rb +24 -0
  5. metadata +57 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Peter Yandell
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,21 @@
1
+ A small class for generating and validating Standard Address Numbers (SAN),
2
+ a unique global identifier used in the book and publishing industries.
3
+
4
+ = Usage
5
+
6
+ SAN.new("9013725").valid?
7
+ => true
8
+
9
+ SAN.valid_san?("9013725")
10
+ => true
11
+
12
+ SAN.valid_san?("9013726")
13
+ => false
14
+
15
+ SAN.check_digit?("901372")
16
+ => 5
17
+
18
+ = Further Reader
19
+
20
+ - http://www.bowker.com/index.php/component/content/article/3
21
+ - http://www.thorpe.com.au/services/services_san.htm
data/lib/san.rb ADDED
@@ -0,0 +1,39 @@
1
+ class SAN
2
+
3
+ class Version #:nodoc:
4
+ Major = 1
5
+ Minor = 0
6
+ Tiny = 0
7
+
8
+ String = [Major, Minor, Tiny].join('.')
9
+ end
10
+
11
+ def initialize(num)
12
+ @number = str.to_s
13
+ end
14
+
15
+ def valid?
16
+ SAN.valid? @number
17
+ end
18
+
19
+ def self.valid?(san)
20
+ san = san.to_s
21
+ san.length == 7 && san == SAN.complete(san[0,6])
22
+ end
23
+
24
+ # Purely for generating new SAN numbers
25
+ def self.complete(six_digit_san)
26
+ six_digit_san = six_digit_san.to_s
27
+ return nil unless six_digit_san.length == 6 && six_digit_san.match(/\d{6}/)
28
+
29
+ #sum = (0..5).to_a.sum { |i| six_digit_san[i,1].to_i * (7-i) }
30
+ arr = (0..5).to_a.collect { |i| six_digit_san[i,1].to_i * (7-i) }
31
+ sum = arr.inject { |sum, n| sum + n }
32
+ check = 11 - (sum % 11)
33
+
34
+ check = 0 if check == 11
35
+ check = 'X' if check == 10
36
+
37
+ six_digit_san + check.to_s
38
+ end
39
+ end
data/spec/san_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'spec'
4
+ require 'san'
5
+
6
+ describe "The SAN class" do
7
+ it "should identify a valid SAN" do
8
+ SAN.valid?("9013725").should be_true
9
+ SAN.valid?(9013725).should be_true
10
+ SAN.valid?("902865X").should be_true
11
+ end
12
+
13
+ it "should identify an invalid SAN" do
14
+ SAN.valid?(nil).should be_false
15
+ SAN.valid?("902865").should be_false
16
+ SAN.valid?(Array).should be_false
17
+ SAN.valid?(Array.new).should be_false
18
+ end
19
+
20
+ it "should calculate a SAN check digit correctly" do
21
+ SAN.complete("901372").should eql("9013725")
22
+ SAN.complete(901372).should eql("9013725")
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: san
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-16 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a (very) small library for working with Standard Address Numbers.
17
+ email: jimmy@deefa.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/san.rb
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/yob/san/tree/master
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --title
33
+ - SAN
34
+ - --line-numbers
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: san
52
+ rubygems_version: 1.1.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: a (very) small library for working with Standard Address Numbers.
56
+ test_files:
57
+ - spec/san_spec.rb