iban-check 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.3.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.2"
7
+ gem "rcov", ">= 0"
8
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Piotr Niełacny
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,30 @@
1
+ = Iban-check
2
+ Check your IBAN number.
3
+
4
+ = Usage
5
+
6
+ iban = Iban::IbanCheck.new :iban => "PL27 1140 2004 0000 3002 0135 5387"
7
+ iban.checksum => "27"
8
+ iban.iban? => true
9
+
10
+ iban = Iban::IbanCheck.new :iban => "GB29 NWBK 6016 1331 9268 19"
11
+ iban.checksum => "29"
12
+ iban.iban? => true
13
+
14
+ iban = Iban::IbanCheck.new :iban => "IE55 AIBK 9311 5212 3456 78" # should be "IE29 AIBK 9311 5212 3456 78"
15
+ iban.checksum => "29"
16
+ iban.iban? => false
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright © 2010 Piotr Niełacny (http://ruby-blog.pl), released under the MIT license
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "iban-check"
16
+ gem.homepage = "http://github.com/LTe/iban-check"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Valid IBAN number}
19
+ gem.description = %Q{Ruby class to validate iban numbers}
20
+ gem.email = "piotr.nielacny@gmail.com"
21
+ gem.authors = ["Piotr Niełacny"]
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "iban-check #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{iban-check}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Piotr Nie\305\202acny"]
12
+ s.date = %q{2011-02-08}
13
+ s.description = %q{Ruby class to validate iban numbers}
14
+ s.email = %q{piotr.nielacny@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "iban-check.gemspec",
29
+ "lib/iban-check.rb",
30
+ "lib/iban-check/ext/array.rb",
31
+ "lib/iban-check/ext/string.rb",
32
+ "lib/iban-check/iban.rb",
33
+ "spec/iban-check_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/LTe/iban-check}
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.5.0}
40
+ s.summary = %q{Valid IBAN number}
41
+ s.test_files = [
42
+ "spec/iban-check_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
51
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_development_dependency(%q<rcov>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<rcov>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
64
+ s.add_dependency(%q<rcov>, [">= 0"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'iban-check/ext/array'
4
+ require 'iban-check/ext/string'
5
+ require 'iban-check/iban'
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def rotate
3
+ push shift
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def switch
3
+ if (65..(65+35)).include? self[0].ord
4
+ (self[0].ord) - 55
5
+ else
6
+ self
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,89 @@
1
+ module Iban
2
+ class IbanCheck
3
+
4
+ BRANCH_WEIGHT = [7,1,3,9,7,1,3]
5
+ BRANCH_WEIGHT_CHECK = [3,9,7,1,3,9,7,1]
6
+
7
+ attr_accessor :iban, :branch, :country
8
+
9
+ def initialize(options = {})
10
+ self.iban = options[:iban].gsub(/[^A-Za-z0-9]/, '')
11
+ self.country = options[:country] || try_get_country
12
+ self.branch = options[:branch] || self.iban[2..9]
13
+ end
14
+
15
+ def check_branch
16
+ number = self.branch.split(//)
17
+ number.pop
18
+
19
+ result = 0
20
+
21
+ number.each_with_index do |item, index|
22
+ result += item.to_i * BRANCH_WEIGHT[index]
23
+ end
24
+
25
+ result % 10
26
+ end
27
+
28
+ def branch?
29
+ number = self.branch.split(//)
30
+ result = 0
31
+
32
+ number.each_with_index do |item, index|
33
+ result += item.to_i * BRANCH_WEIGHT_CHECK[index]
34
+ end
35
+
36
+ if result.to_s[-1] == 48
37
+ true
38
+ else
39
+ false
40
+ end
41
+ end
42
+
43
+ def checksum
44
+ checksum = 98 - (prepare_iban % 97)
45
+
46
+ if checksum < 10
47
+ "0#{checksum}"
48
+ else
49
+ checksum.to_s
50
+ end
51
+ end
52
+
53
+ def iban?
54
+ checksum == iban[0..1]
55
+ end
56
+
57
+ private
58
+
59
+ def prepare_iban
60
+ result = country + reset_checksum(self.iban.dup)
61
+
62
+ result = result.split(//)
63
+ 4.times { result.rotate }
64
+
65
+ result.map! do |item|
66
+ item.switch
67
+ end
68
+
69
+ result = result.join.to_i
70
+ end
71
+
72
+ def reset_checksum(number)
73
+ number[0] = number[1] = "0"
74
+ number
75
+ end
76
+
77
+ def try_get_country
78
+ if iban[0].chr =~ /[A-Z]/ and iban[1].chr =~ /[A-Z]/
79
+ result = iban[0..1]
80
+ iban.sub!(/^[A-Z]{2}/, '')
81
+
82
+ result
83
+ else
84
+ raise "No country specified"
85
+ end
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "IbanCheck" do
4
+ it "should create proper checksum from valid IBAN number" do
5
+ iban = Iban::IbanCheck.new :iban => "PL27 1140 2004 0000 3002 0135 5387"
6
+ iban.checksum.should == "27"
7
+ iban.iban?.should == true
8
+
9
+ iban = Iban::IbanCheck.new :iban => "HU42 1177 3016 1111 1018 0000 0000"
10
+ iban.checksum.should == "42"
11
+ iban.iban?.should == true
12
+ end
13
+
14
+ it "should create proper checksum from invalid IBAN number" do
15
+ iban = Iban::IbanCheck.new :iban => "PL27 1140 2004 0010 3002 0135 5387"
16
+ iban.checksum.should == "10"
17
+ iban.iban?.should == false
18
+ end
19
+
20
+ it "should raise error -- no country" do
21
+ lambda {Iban::IbanCheck.new(:iban => "27 1140 2004 0010 3002 0135 5387")}.should raise_error
22
+ end
23
+
24
+ it "should recognize country" do
25
+ iban = Iban::IbanCheck.new :iban => "PL27 1140 2004 0010 3002 0135 5387"
26
+ iban.country.should == "PL"
27
+ end
28
+
29
+ it "should create proper checksum for branch" do
30
+ iban = Iban::IbanCheck.new :iban => "PL27 1140 2004 0010 3002 0135 5387"
31
+ iban.check_branch.should == 4
32
+
33
+ iban = Iban::IbanCheck.new :iban => "PL27 1145 2024 0010 3002 0135 5387"
34
+ iban.check_branch.should_not == 4
35
+ end
36
+
37
+ it "should answer true for proper branch" do
38
+ iban = Iban::IbanCheck.new :iban => "PL27 1160 2202 0010 3002 0135 5387"
39
+ iban.branch?.should == true
40
+ end
41
+
42
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'iban-check'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iban-check
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "Piotr Nie\xC5\x82acny"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-08 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 0
33
+ version: 2.3.0
34
+ prerelease: false
35
+ type: :development
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ prerelease: false
51
+ type: :development
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ prerelease: false
67
+ type: :development
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ prerelease: false
81
+ type: :development
82
+ requirement: *id004
83
+ description: Ruby class to validate iban numbers
84
+ email: piotr.nielacny@gmail.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ files:
93
+ - .document
94
+ - .rspec
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.rdoc
99
+ - Rakefile
100
+ - VERSION
101
+ - iban-check.gemspec
102
+ - lib/iban-check.rb
103
+ - lib/iban-check/ext/array.rb
104
+ - lib/iban-check/ext/string.rb
105
+ - lib/iban-check/iban.rb
106
+ - spec/iban-check_spec.rb
107
+ - spec/spec_helper.rb
108
+ has_rdoc: true
109
+ homepage: http://github.com/LTe/iban-check
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project:
138
+ rubygems_version: 1.5.0
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Valid IBAN number
142
+ test_files:
143
+ - spec/iban-check_spec.rb
144
+ - spec/spec_helper.rb