abn 1.2.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 +22 -0
  3. data/lib/abn.rb +45 -0
  4. data/spec/abn_spec.rb +65 -0
  5. metadata +57 -0
@@ -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,22 @@
1
+ A small class for validating Australian Business Numbers (ABN)
2
+
3
+ = Installation
4
+
5
+ gem install yob-abn --source http://gems.github.com/
6
+
7
+ = Usage
8
+
9
+ require 'abn'
10
+
11
+ ABN.new("12042168743").valid?
12
+ => true
13
+
14
+ ABN.valid_abn?("12042168743")
15
+ => true
16
+
17
+ ABN.valid_abn?("12042168744")
18
+ => false
19
+
20
+ = Further Reading
21
+
22
+ - http://www.clearwater.com.au/?action=code
@@ -0,0 +1,45 @@
1
+ class ABN
2
+
3
+ module Version #:nodoc:
4
+ Major = 1
5
+ Minor = 2
6
+ Tiny = 0
7
+
8
+ String = [Major, Minor, Tiny].join('.')
9
+ end
10
+
11
+ # Creates an ABN object representing the ABN number passed
12
+ # as the only parameter.
13
+ def initialize(num)
14
+ @number = num.to_s.tr ' ',''
15
+ end
16
+
17
+ # Returns whether the current ABN class represents a
18
+ # valid ABN number according to a weighting
19
+ # algorithm (not checked against a datbase)
20
+ def valid?
21
+ return false unless @number.length == 11
22
+
23
+ weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
24
+ sum = 0
25
+ (0..10).each do |i|
26
+ c = @number[i,1]
27
+ digit = c.to_i - (i.zero? ? 1 : 0)
28
+ sum += weights[i] * digit
29
+ end
30
+
31
+ sum % 89 == 0 ? true : false
32
+ end
33
+
34
+ # Correctly formats the represented ABN if valid, else returns
35
+ # an empty string
36
+ def to_s
37
+ valid? ? "%s%s %s%s%s %s%s%s %s%s%s" % @number.split('') : ""
38
+ end
39
+
40
+ # Accepts an ABN number as a String or Bignum and returns
41
+ # whether or not it is valid (not checked against a database)
42
+ def self.valid?(abn)
43
+ new(abn).valid?
44
+ end
45
+ end
@@ -0,0 +1,65 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'spec'
4
+ require 'abn'
5
+
6
+ def bad_parameter; (1..11).to_a; end
7
+
8
+ describe "The ABN class" do
9
+ it "should identify a valid ABN" do
10
+ ABN.valid?("12042168743").should be_true
11
+ ABN.valid?(12042168743).should be_true
12
+ end
13
+
14
+ it "should identify a preformatted valid ABN" do
15
+ ABN.valid?("12 042 168 743").should be_true
16
+ end
17
+
18
+ it "should have a problem with a pre-formatted invalid ABN" do
19
+ ABN.valid?("12 042 168 744").should be_false
20
+ end
21
+
22
+ it "should have a problem with an invalid ABN" do
23
+ ABN.valid?("12042168744").should be_false
24
+ ABN.valid?("902865").should be_false
25
+ end
26
+
27
+ it "should have a problem with invalid parameters" do
28
+ ABN.valid?(nil).should be_false
29
+ ABN.valid?(Array).should be_false
30
+ ABN.valid?(Array.new).should be_false
31
+ end
32
+
33
+ it "should have a problem with invalid parameter type that has a #length of 11" do
34
+ bad_parameter.length.should eql(11)
35
+ ABN.valid?(bad_parameter).should be_false
36
+ end
37
+
38
+ it "should be able to format a valid ABN" do
39
+ ABN.new("12042168743").to_s.should eql("12 042 168 743")
40
+ end
41
+
42
+ it "should be able to return a pre-formatted ABN" do
43
+ ABN.new("12 042 168 743").to_s.should eql("12 042 168 743")
44
+ end
45
+
46
+ it "should not format invalid parameter" do
47
+ ABN.new(nil).to_s.should eql("")
48
+ ABN.new(Array).to_s.should eql("")
49
+ ABN.new(Array.new).to_s.should eql("")
50
+ end
51
+
52
+ it "should not format an invalid parameter type that has a #length of 11" do
53
+ bad_parameter.length.should eql(11)
54
+ ABN.new(bad_parameter).to_s.should eql("")
55
+ end
56
+
57
+ it "should not format an invalid ABN" do
58
+ ABN.new("12042168744").to_s.should eql("")
59
+ ABN.new("902865").to_s.should eql("")
60
+ end
61
+
62
+ it "should not format an pre-formatted invalid ABN" do
63
+ ABN.new("12 042 168 744").to_s.should eql("")
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abn
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-12 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a (very) small library for working with Australian Business Numbers.
17
+ email: jimmy@deefa.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/abn.rb
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/yob/abn/tree/master
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --title
33
+ - ABN
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: abn
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: a (very) small library for working with Australian Business Numbers.
56
+ test_files:
57
+ - spec/abn_spec.rb