banktools-de 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,17 @@
1
1
  # http://de.wikipedia.org/wiki/Bankleitzahl
2
2
  # http://translate.google.com/translate?hl=en&sl=de&tl=en&u=http%3A%2F%2Fde.wikipedia.org%2Fwiki%2FBankleitzahl
3
3
 
4
- require "attr_extras"
4
+ require "bank_tools/de"
5
5
  require "bank_tools/de/errors"
6
6
 
7
+ require "attr_extras"
8
+ require "yaml"
9
+
7
10
  module BankTools
8
11
  module DE
9
12
  class BLZ
10
13
  LENGTH = 8
14
+ BLZ_TO_NAME_PATH = File.join(BankTools::DE.data_dir, "blz_to_name.yml")
11
15
 
12
16
  pattr_initialize :original_value
13
17
 
@@ -31,8 +35,16 @@ module BankTools
31
35
  errors
32
36
  end
33
37
 
38
+ def bank_name
39
+ blz_to_name.fetch(compacted_value, nil)
40
+ end
41
+
34
42
  private
35
43
 
44
+ def blz_to_name
45
+ @blz_to_name ||= YAML.load_file(BLZ_TO_NAME_PATH).fetch(:data)
46
+ end
47
+
36
48
  def compacted_value
37
49
  original_value.to_s.gsub(/\s/, "")
38
50
  end
@@ -0,0 +1,49 @@
1
+ require "attr_extras"
2
+ require "yaml"
3
+ require "open-uri"
4
+
5
+ # Also tried "roo" (slow) and "spreadsheet" (got errors).
6
+ require "creek"
7
+
8
+ module BankTools
9
+ module DE
10
+ class BLZDownloader
11
+ pattr_initialize :url
12
+
13
+ def run
14
+ puts "Download: #{url}"
15
+
16
+ if url.include?("://")
17
+ puts "Downloading…"
18
+ local_file = "/tmp/blz.xlsx"
19
+ File.write(local_file, open(url).read)
20
+ else
21
+ local_file = url
22
+ end
23
+
24
+ puts "Parsing…"
25
+
26
+ book = Creek::Book.new(local_file)
27
+ sheet = book.sheets[0]
28
+
29
+ hash = {
30
+ generated_at: Time.now,
31
+ generated_from_url: url,
32
+ data: {}
33
+ }
34
+
35
+ data = hash[:data]
36
+
37
+ sheet.rows.each_with_index do |row, index|
38
+ blz, feature, name, plz, location, short_name, *_ = row.values
39
+ next if index.zero? && blz.match(/\D/) # Headers
40
+ data[blz] = short_name
41
+ end
42
+
43
+ File.write("data/blz_to_name.yml", hash.to_yaml)
44
+
45
+ puts "Done. Wrote #{data.length} entries."
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  module BankTools
2
2
  module DE
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
data/lib/bank_tools/de.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  require "bank_tools/de/version"
2
+ require "bank_tools/de/blz"
3
+ require "bank_tools/de/account"
2
4
 
3
5
  module BankTools
4
6
  module DE
5
- # Your code goes here...
7
+ def self.data_dir
8
+ File.join(root_dir, "data")
9
+ end
10
+
11
+ def self.root_dir
12
+ File.expand_path '../../..', __FILE__
13
+ end
6
14
  end
7
15
  end
data/spec/account_spec.rb CHANGED
@@ -37,7 +37,7 @@ describe BankTools::DE::Account, "#valid?" do
37
37
  end
38
38
 
39
39
  describe BankTools::DE::Account, "#errors" do
40
- Errors = BankTools::DE::Errors
40
+ Errors ||= BankTools::DE::Errors
41
41
 
42
42
  it "is empty when valid" do
43
43
  expect(BankTools::DE::Account.new(" 1-2 ").errors).to be_empty
data/spec/blz_spec.rb CHANGED
@@ -28,7 +28,7 @@ describe BankTools::DE::BLZ, "#valid?" do
28
28
  end
29
29
 
30
30
  describe BankTools::DE::BLZ, "#errors" do
31
- Errors = BankTools::DE::Errors
31
+ Errors ||= BankTools::DE::Errors
32
32
 
33
33
  it "is empty if there's 8 digits and possibly whitespace" do
34
34
  expect(BankTools::DE::BLZ.new(" 123 456 78 ").errors).to be_empty
@@ -46,3 +46,13 @@ describe BankTools::DE::BLZ, "#errors" do
46
46
  expect(BankTools::DE::BLZ.new("1X").errors).to include(Errors::INVALID_CHARACTERS)
47
47
  end
48
48
  end
49
+
50
+ describe BankTools::DE::BLZ, "#bank_name" do
51
+ it "is provided if known" do
52
+ expect(BankTools::DE::BLZ.new("10000000").bank_name).to eq "BBk Berlin"
53
+ end
54
+
55
+ it "is provided if known" do
56
+ expect(BankTools::DE::BLZ.new("X").bank_name).to be_nil
57
+ end
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banktools-de
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: creek
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description:
70
84
  email:
71
85
  - henrik@nyh.se
@@ -78,9 +92,11 @@ files:
78
92
  - README.md
79
93
  - Rakefile
80
94
  - banktools-de.gemspec
95
+ - data/blz_to_name.yml
81
96
  - lib/bank_tools/de.rb
82
97
  - lib/bank_tools/de/account.rb
83
98
  - lib/bank_tools/de/blz.rb
99
+ - lib/bank_tools/de/blz_downloader.rb
84
100
  - lib/bank_tools/de/errors.rb
85
101
  - lib/bank_tools/de/version.rb
86
102
  - spec/account_spec.rb