istc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +2 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +33 -0
  4. data/lib/istc.rb +75 -0
  5. data/spec/istc_spec.rb +38 -0
  6. metadata +58 -0
@@ -0,0 +1,2 @@
1
+ v1.0 (26th September 2008)
2
+ - Initial Release
@@ -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.
@@ -0,0 +1,33 @@
1
+ A small class for generating and validating Internation Standard Text
2
+ Codes (ISTC).
3
+
4
+ = Installation
5
+
6
+ gem install istc
7
+
8
+ = Usage
9
+
10
+ ISTC.new("0A9200800000007C").valid?
11
+ => true
12
+
13
+ ISTC.valid?("0A9200800000007C")
14
+ => true
15
+
16
+ ISTC.valid?("0A9200800000007B")
17
+ => false
18
+
19
+ ISTC.complete?("0A9200800000007")
20
+ => "0A9200800000007C"
21
+
22
+ ISTC.new("0A9200800000007").to_s
23
+ => "0A9-2008-00000007-C"
24
+
25
+ = Further Reading
26
+
27
+ - http://en.wikipedia.org/wiki/International_Standard_Text_Code
28
+ - http://www.istcinfo.org/
29
+
30
+ = Contributing
31
+
32
+ Source code is publicly available @ http://github.com/yob/istc/tree/master.
33
+ Patches welcome, preferably via a git repo I can pull from.
@@ -0,0 +1,75 @@
1
+ class ISTC
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(str)
12
+ @number = str.to_s
13
+ end
14
+
15
+ def valid?
16
+ ISTC.valid? @number
17
+ end
18
+
19
+ def agency
20
+ return nil unless valid?
21
+ @number[0,3]
22
+ end
23
+
24
+ def year
25
+ return nil unless valid?
26
+ @number[3,4]
27
+ end
28
+
29
+ def work
30
+ return nil unless valid?
31
+ @number[7,8]
32
+ end
33
+
34
+ def check
35
+ return nil unless valid?
36
+ @number[15,1]
37
+ end
38
+
39
+ # output a hyphenated version of this ISTC
40
+ def to_s
41
+ return nil unless valid?
42
+ "#{agency}-#{year}-#{work}-#{check}"
43
+ end
44
+
45
+ def self.valid?(istc)
46
+ istc = istc.to_s
47
+ istc.length == 16 && istc == ISTC.complete(istc[0,15])
48
+ end
49
+
50
+ # Purely for generating new ISTC numbers
51
+ #
52
+ def self.complete(num)
53
+ num = num.to_s
54
+ return nil unless num.length == 15 && num.match(/[\dA-Fa-f]{11}/)
55
+
56
+ #puts num
57
+ arr = (0..14).to_a.collect do |i|
58
+ if i == 0 || i % 4 == 0
59
+ num[i,1].hex * 11
60
+ elsif i % 4 == 1
61
+ num[i,1].hex * 9
62
+ elsif i % 4 == 2
63
+ num[i,1].hex * 3
64
+ else
65
+ num[i,1].hex * 1
66
+ end
67
+ end
68
+ #puts arr.inspect
69
+ sum = arr.inject { |sum, n| sum + n }
70
+ remainder = sum % 16
71
+ check = remainder.to_s(16).upcase
72
+
73
+ "#{num}#{check}"
74
+ end
75
+ end
@@ -0,0 +1,38 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'spec'
4
+ require 'istc'
5
+
6
+ describe "The ISTC class" do
7
+ it "should identify a valid ISTC" do
8
+ ISTC.new("0A9200112C4F1324").valid?.should be_true
9
+ end
10
+
11
+ it "should identify a valid ISTC" do
12
+ ISTC.valid?("0A9200112C4F1324").should be_true
13
+ end
14
+
15
+ it "should identify an invalid ISTC" do
16
+ ISTC.valid?(nil).should be_false
17
+ ISTC.valid?("902865").should be_false
18
+ ISTC.valid?(Array).should be_false
19
+ ISTC.valid?(Array.new).should be_false
20
+ ISTC.valid?("0A9200800000007B").should be_false
21
+ ISTC.valid?("0A9200800000007").should be_false
22
+ end
23
+
24
+ it "should calculate a ISTC check digit correctly" do
25
+ ISTC.complete("0A9200112C4F132").should eql("0A9200112C4F1324")
26
+ end
27
+
28
+ it "should hyphen a ISTC correctly" do
29
+ ISTC.new("0A9200112C4F1324").to_s.should eql("0A9-2001-12C4F132-4")
30
+ end
31
+
32
+ it "should return sections of an ISTC correctly" do
33
+ ISTC.new("0A9200112C4F1324").agency.should eql("0A9")
34
+ ISTC.new("0A9200112C4F1324").year.should eql("2001")
35
+ ISTC.new("0A9200112C4F1324").work.should eql("12C4F132")
36
+ ISTC.new("0A9200112C4F1324").check.should eql("4")
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: istc
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-10-06 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a (very) small library for working with ISTC
17
+ email: jimmy@deefa.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/istc.rb
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ - CHANGELOG
29
+ has_rdoc: true
30
+ homepage: http://github.com/yob/istc/tree/master
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --title
34
+ - ISTC
35
+ - --line-numbers
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project: yob-projects
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: a (very) small library for working with ISTC
57
+ test_files:
58
+ - spec/istc_spec.rb