blz 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/CHANGELOG.md +6 -0
  2. data/README.md +4 -0
  3. data/blz.gemspec +1 -1
  4. data/lib/blz/bank.rb +43 -68
  5. metadata +2 -2
@@ -1,3 +1,9 @@
1
+ # 0.1.1, released 2012-11-18
2
+
3
+ * Fixes a bug with `nil` values (kudos to @max-power)
4
+ * Refactoring code, and it's just better to
5
+ read now (again, thanks to @max-power)
6
+
1
7
  # 0.1.0, released 2012-09-27
2
8
 
3
9
  * Initial version released on GitHub
data/README.md CHANGED
@@ -52,6 +52,10 @@ from the German Bundesbank
52
52
  [here](http://www.bundesbank.de/Redaktion/DE/Standardartikel/Kerngeschaeftsfelder/Unbarer_Zahlungsverkehr/bankleitzahlen_download.html).
53
53
  Now go and build your own BLZ gem ;-)
54
54
 
55
+ ## Contributors
56
+
57
+ * [max-power](https://github.com/max-power)
58
+
55
59
  ## License
56
60
 
57
61
  Released under the MIT License. See the LICENSE file for further
@@ -5,7 +5,7 @@ extra_rdoc_files = ['CHANGELOG.md', 'LICENSE', 'README.md']
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'blz'
8
- s.version = '0.1.0'
8
+ s.version = '0.1.1'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = "BLZ (Bankleitzahlen) for Ruby"
11
11
 
@@ -1,85 +1,60 @@
1
1
  # encoding: utf-8
2
+ require 'csv'
2
3
 
3
4
  module BLZ
4
- # Represents a bank with a BLZ, a BIC, a name etc.
5
5
  class Bank
6
- # BLZ = a bank idenfier code widely used in DE and AT
7
- attr_reader :blz
8
- # Name of the bank
9
- attr_reader :name
10
- # Postal code
11
- attr_reader :zip
12
- # City
13
- attr_reader :city
14
- # Short name = short description of the bank
15
- attr_reader :short_name
16
- # BIC = bank identifier code (ISO 9362)
17
- attr_reader :bic
18
-
19
- # Initializes a single Bank record.
20
- def initialize(blz, name, zip, city, short_name, bic)
21
- @blz = blz
22
- @name = name
23
- @zip = zip
24
- @city = city
25
- @short_name = short_name
26
- @bic = bic
27
- end
28
-
29
- # Returns an array of all Banks specified by +code+.
30
- # The following options apply:
31
- #
32
- # exact:: Only return exact matches (false by default)
33
- #
34
- def self.find_by_blz(code, options = nil)
35
- options ||= {}
36
- exact = options.fetch(:exact, false)
6
+ # Represents a bank with a BLZ, a BIC, a name etc.
7
+ class << self
8
+ # Returns an array of all Banks specified by +code+.
9
+ # The following options apply:
10
+ # exact:: Only return exact matches (false by default)
11
+ #
12
+ def find_by_blz(code, options = {})
13
+ exact = options.fetch(:exact, false)
14
+ all.select do |bank|
15
+ bank.blz == code || (!exact && bank.blz.start_with?(code))
16
+ end
17
+ end
37
18
 
38
- self.all.inject([]) do |results, bank|
39
- if bank.blz == code || (!exact && bank.blz.start_with?(code))
40
- results << bank
19
+ # Returns an array of all Banks with a substring of +city+.
20
+ def find_by_city(substring)
21
+ all.select do |bank|
22
+ bank.city.index(substring)
41
23
  end
42
- results
43
24
  end
44
- end
45
25
 
46
- # Returns an array of all Banks with a substring of +city+.
47
- def self.find_by_city(substring)
48
- self.all.inject([]) do |results, bank|
49
- results << bank if bank.city.index(substring)
50
- results
26
+ # Returns an array of all banks.
27
+ def all
28
+ @banks ||= read_banks
29
+ end
30
+
31
+ private
32
+
33
+ def read_banks
34
+ banks = []
35
+ filename = File.expand_path('data/blz.tsv')
36
+ CSV.foreach(filename, col_sep: "\t") do |r|
37
+ banks << new(r[0], r[2], r[3], r[4], r[5], r[7])
38
+ end
39
+ banks
51
40
  end
52
41
  end
42
+
43
+ attr_reader :blz, :name, :zip, :city, :short_name, :bic
53
44
 
54
- # Returns an array of all banks.
55
- def self.all
56
- # TODO is this the right way to do it?
57
- Thread.current[:'BLZ::Bank.all'] ||= read_banks
45
+ # Initializes a single Bank record.
46
+ def initialize(blz, name, zip, city, short_name, bic)
47
+ @blz = blz
48
+ @name = name
49
+ @zip = zip
50
+ @city = city
51
+ @bic = bic
52
+ @short_name = short_name
58
53
  end
59
54
 
60
55
  # Returns a nice representation of the bank.
61
56
  def to_s
62
- [blz, name, "#{zip} #{city}", bic].reject { |s| s.empty? }.join ", "
57
+ [blz, name, "#{zip} #{city}", bic].compact.reject(&:empty?).join(', ')
63
58
  end
64
-
65
- private
66
-
67
- def self.read_banks
68
- banks = []
69
- filename = File.join(File.dirname(__FILE__), '..', '..', 'data', 'blz.tsv')
70
- File.foreach(filename) do |line|
71
- columns = line.split /\t/
72
- blz = columns[0].strip
73
- name = columns[2].strip
74
- zip = columns[3].strip
75
- city = columns[4].strip
76
- short_name = columns[5].strip
77
- bic = columns[7].strip
78
- banks << Bank.new(blz, name, zip, city, short_name, bic)
79
- end
80
- banks
81
- end
82
-
83
59
  end
84
- end
85
-
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-27 00:00:00.000000000 Z
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler