ibanizator 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/db/blz.db ADDED
Binary file
@@ -0,0 +1,69 @@
1
+ module Iban
2
+ # according to wikipedia (http://en.wikipedia.org/wiki/IBAN)
3
+ LENGTHS = {
4
+ :AL => 28,
5
+ :AD => 24,
6
+ :AT => 20,
7
+ :AZ => 28,
8
+ :BH => 22,
9
+ :BE => 16,
10
+ :BA => 20,
11
+ :BR => 29,
12
+ :BG => 22,
13
+ :CR => 21,
14
+ :HR => 21,
15
+ :CY => 28,
16
+ :CZ => 24,
17
+ :DK => 18,
18
+ :DO => 28,
19
+ :EE => 20,
20
+ :FO => 18,
21
+ :FI => 18,
22
+ :FR => 27,
23
+ :GE => 22,
24
+ :DE => 22,
25
+ :GI => 23,
26
+ :GR => 27,
27
+ :GL => 18,
28
+ :GT => 28,
29
+ :HU => 28,
30
+ :IS => 26,
31
+ :IE => 22,
32
+ :IL => 23,
33
+ :IT => 27,
34
+ :KZ => 20,
35
+ :KW => 30,
36
+ :LV => 21,
37
+ :LB => 28,
38
+ :LI => 21,
39
+ :LT => 20,
40
+ :LU => 20,
41
+ :MK => 19,
42
+ :MT => 31,
43
+ :MR => 27,
44
+ :MU => 30,
45
+ :MC => 27,
46
+ :MD => 24,
47
+ :ME => 22,
48
+ :NL => 18,
49
+ :NO => 15,
50
+ :PK => 24,
51
+ :PS => 29,
52
+ :PL => 28,
53
+ :PT => 25,
54
+ :RO => 24,
55
+ :SM => 27,
56
+ :SA => 24,
57
+ :RS => 22,
58
+ :SK => 24,
59
+ :SI => 19,
60
+ :ES => 24,
61
+ :SE => 24,
62
+ :CH => 21,
63
+ :TN => 24,
64
+ :TR => 26,
65
+ :AE => 23,
66
+ :GB => 22,
67
+ :VG => 24
68
+ }.freeze
69
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'lengths'
2
+
3
+ module Iban
4
+ class Validator
5
+ def validate_iban(input)
6
+ iban = sanitize_input(input)
7
+
8
+ valid_length?(iban) && valid_checksum?(iban)
9
+ end
10
+
11
+ private
12
+
13
+ def valid_length?(iban)
14
+ return false if iban.length <= 4 # two digits for the country code and two for the checksum
15
+ country_code = iban[0..1].upcase.to_sym
16
+ iban.length == LENGTHS[country_code]
17
+ end
18
+
19
+ def sanitize_input(input)
20
+ input.to_s.chomp.gsub(/\s+/,"")
21
+ end
22
+
23
+ def valid_checksum?(iban)
24
+ number_representation = integerize(reorder(iban))
25
+ number_representation % 97 == 1
26
+ end
27
+
28
+ def reorder(iban)
29
+ "#{iban[4..-1]}#{iban[0..3]}"
30
+ end
31
+
32
+ def integerize(iban)
33
+ iban.gsub(/[A-Z]/) do |match|
34
+ match.ord - 'A'.ord + 10
35
+ end.to_i
36
+ end
37
+ end
38
+ end
data/lib/ibanizator.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require_relative 'iban/validator'
2
+ require_relative 'swift_bic/bank_db'
3
+
1
4
  class Ibanizator
2
5
  def calculate_iban options
3
6
  # Error handling
@@ -15,21 +18,19 @@ class Ibanizator
15
18
  end
16
19
 
17
20
  def validate_iban iban
18
- country_codes = { :de => 22 }
19
- country_code = iban[0..1].downcase.to_sym
20
- checksum = iban[2..3]
21
-
22
- # Error handling
23
- if country_codes[country_code]
24
- raise '' if iban.length != 22
25
- else
26
- raise ''
27
- end
21
+ # for the sake of compatibility
22
+ validator = Iban::Validator.new
23
+ validator.validate_iban(iban)
24
+ end
28
25
 
29
- # Works only for Germany
30
- calculated_checksum = calculate_checksum iban[4..11], iban[12..22], character_to_digit(country_code.to_s)
26
+ def bic bank_code
27
+ bank_db = SwiftBic::BankDb.new bank_code
28
+ bank_db.bic
29
+ end
31
30
 
32
- checksum == calculated_checksum ? true : false
31
+ def bank_name bank_code
32
+ bank_db = SwiftBic::BankDb.new bank_code
33
+ bank_db.bank_name
33
34
  end
34
35
 
35
36
  def character_to_digit char
@@ -41,4 +42,4 @@ class Ibanizator
41
42
  checksum = (98 - x).to_s
42
43
  checksum.length == 1 ? checksum.insert(0, '0') : checksum
43
44
  end
44
- end
45
+ end
@@ -0,0 +1,32 @@
1
+ require 'sqlite3'
2
+
3
+ module SwiftBic
4
+ class BankDb
5
+ def initialize bank_number
6
+ @bank_data = load_table_row bank_number
7
+ end
8
+
9
+ def bank_name
10
+ @bank_data[1]
11
+ end
12
+
13
+ def bic
14
+ @bank_data[2]
15
+ end
16
+
17
+
18
+ private
19
+
20
+ def load_table_row bank_number
21
+ begin
22
+ db = SQLite3::Database.open("db/blz.db")
23
+ row = db.execute("SELECT * FROM blz WHERE blz = '#{bank_number}'")
24
+ rescue SQLite3::Exception => e
25
+ throw e
26
+ ensure
27
+ db.close
28
+ end
29
+ row[0]
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibanizator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: ibanizator generates the correct IBAN for given account number and bank
15
31
  number for german accounts.
16
32
  email: christoph.stettner@softwareinmotion.de
@@ -19,6 +35,10 @@ extensions: []
19
35
  extra_rdoc_files: []
20
36
  files:
21
37
  - lib/ibanizator.rb
38
+ - lib/iban/validator.rb
39
+ - lib/iban/lengths.rb
40
+ - lib/swift_bic/bank_db.rb
41
+ - db/blz.db
22
42
  homepage: https://github.com/softwareinmotion/ibanizator
23
43
  licenses: []
24
44
  post_install_message: