banking_data 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/banking_data.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_model'
2
2
  require 'smarter_csv'
3
3
  require 'banking_data/version'
4
+ require 'banking_data/query'
4
5
  require 'banking_data/bank'
5
6
  require 'banking_data/german_bank'
6
7
  require 'banking_data/austrian_bank'
@@ -2,33 +2,47 @@ require 'active_support/core_ext/object/blank'
2
2
  require 'active_support/core_ext/object/try'
3
3
 
4
4
  class BankingData::AustrianBank < BankingData::Bank
5
- include ActiveModel::Model
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::AttributeMethods
6
8
 
7
- attr_accessor :bic
9
+ LOCALE = :at
8
10
 
9
- def self.all
10
- @@all ||= get_all
11
- end
11
+ attr_accessor :bic, :blz
12
+
13
+ class << self
12
14
 
13
- def self.get_all
14
- banks = []
15
- SmarterCSV.process(file, opts).each do |line|
16
- bic = line[:'swift-code'].try(:gsub, /"/, '')
17
- banks << new(bic: bic)
15
+ delegate :where, :only, to: :query
16
+
17
+ def all
18
+ @@all ||= get_all
18
19
  end
19
- banks
20
- end
21
20
 
22
- def self.file
23
- File.dirname(__FILE__) +
24
- '/../../data/kiverzeichnis_gesamt_de_1381499802577.csv'
25
- end
21
+ def get_all
22
+ banks = []
23
+ SmarterCSV.process(file, opts).each do |line|
24
+ if line[:kennzeichen] == 'Hauptanstalt'
25
+ blz = line[:bankleitzahl].try(:gsub, /"/, '')
26
+ bic = line[:'swift-code'].try(:gsub, /"/, '')
27
+ banks << new(bic: bic, blz: blz)
28
+ end
29
+ end
30
+ banks
31
+ end
32
+
33
+ private
34
+
35
+ def file
36
+ File.dirname(__FILE__) +
37
+ '/../../data/kiverzeichnis_gesamt_de_1381499802577.csv'
38
+ end
26
39
 
27
- def self.opts
28
- {
29
- col_sep: ';',
30
- file_encoding: 'iso-8859-1',
31
- force_simple_split: true
32
- }
40
+ def opts
41
+ {
42
+ col_sep: ';',
43
+ file_encoding: 'iso-8859-1',
44
+ force_simple_split: true
45
+ }
46
+ end
33
47
  end
34
48
  end
@@ -1,37 +1,21 @@
1
1
  module BankingData
2
2
  class Bank
3
3
 
4
- @@options = {}
5
-
6
- # goal: do something like:
7
- # BankingData::Bank.where(locale: :de).only(:bic)
8
- def self.where(options = {})
9
- locale = options.delete(:locale)
10
- if locale == 'DE' || locale == :de
11
- GermanBank.where(options)
12
- elsif locale == 'AT' || locale == :at
13
- AustrianBank.where(options)
14
- elsif locale == 'CH' || locale == :ch
15
- SwissBank.where(options)
16
- elsif options.empty?
17
- self
18
- else
19
- @@options = @@options.merge(options)
20
- self
4
+ def initialize(attributes = {})
5
+ attributes.each do |name, value|
6
+ send("#{name}=", value)
21
7
  end
22
8
  end
23
9
 
24
- def self.only(*attributes)
25
- options = @@options
26
- @@options = {}
27
- all.select { |bank| options.map { |k, v| bank.send(k) == v }.all? }
28
- .map { |bank| attributes.map { |attr| bank.send(attr) } }
29
- end
10
+ class << self
30
11
 
31
- def self.to_a
32
- options = @@options
33
- @@options = {}
34
- all.select { |bank| options.map { |k, v| bank.send(k) == v }.all? }
12
+ def where(options)
13
+ Query.new(options)
14
+ end
15
+
16
+ def query
17
+ Query.new(locale: self::LOCALE)
18
+ end
35
19
  end
36
20
  end
37
21
  end
@@ -1,25 +1,36 @@
1
1
  require 'active_support/core_ext/object/blank'
2
2
 
3
3
  class BankingData::GermanBank < BankingData::Bank
4
- include ActiveModel::Model
4
+ extend ActiveModel::Naming
5
+ include ActiveModel::Conversion
6
+ include ActiveModel::AttributeMethods
7
+
8
+ LOCALE = :de
5
9
 
6
10
  attr_accessor :bic, :blz
7
11
 
8
- def self.all
9
- @@all ||= get_all
10
- end
12
+ class << self
13
+
14
+ delegate :where, :only, to: :query
11
15
 
12
- def self.get_all
13
- banks = []
14
- File.open(file).each_line do |line|
15
- blz = line[0..7]
16
- bic = line[139..149]
17
- banks << new(bic: bic, blz: blz)
16
+ def all
17
+ @@all ||= get_all
18
18
  end
19
- banks
20
- end
21
19
 
22
- def self.file
23
- File.dirname(__FILE__) + '/../../data/BLZ_20130909.txt'
20
+ private
21
+
22
+ def get_all
23
+ banks = []
24
+ File.open(file).each_line do |line|
25
+ blz = line[0..7]
26
+ bic = line[139..149]
27
+ banks << new(bic: bic, blz: blz)
28
+ end
29
+ banks
30
+ end
31
+
32
+ def file
33
+ File.dirname(__FILE__) + '/../../data/BLZ_20130909.txt'
34
+ end
24
35
  end
25
36
  end
@@ -0,0 +1,42 @@
1
+ require 'active_support/core_ext/class/subclasses'
2
+
3
+ module BankingData
4
+ class Query
5
+
6
+ delegate :each, :flatten, :map, :first, :last, to: :to_a
7
+ attr_accessor :options, :attributes, :locale
8
+
9
+ def initialize(options, only = nil)
10
+ @locale = options.delete(:locale)
11
+ @options = options
12
+ @attributes = only
13
+ end
14
+
15
+ def where(opts = {})
16
+ clone.tap do |query|
17
+ query.locale = opts.delete(:locale)
18
+ query.options = @options.merge(opts)
19
+ end
20
+ end
21
+
22
+ def only(*attrs)
23
+ clone.tap do |query|
24
+ query.attributes = attrs
25
+ end
26
+ end
27
+
28
+ def to_a
29
+ data = bank.all
30
+ .select { |bank| @options.map { |k, v| bank.send(k) == v }.all? }
31
+ if @attributes
32
+ data.map { |bank| @attributes.map { |attr| bank.send(attr) } }
33
+ else
34
+ data
35
+ end
36
+ end
37
+
38
+ def bank
39
+ Bank.subclasses.find { |bank_class| bank_class::LOCALE == locale }
40
+ end
41
+ end
42
+ end
@@ -1,27 +1,37 @@
1
1
  require 'active_support/core_ext/object/blank'
2
2
 
3
3
  class BankingData::SwissBank < BankingData::Bank
4
- include ActiveModel::Model
4
+ extend ActiveModel::Naming
5
+ include ActiveModel::Conversion
6
+ include ActiveModel::AttributeMethods
5
7
 
6
- attr_accessor :bic
8
+ LOCALE = :ch
7
9
 
8
- def self.all
9
- @@all ||= get_all
10
- end
10
+ attr_accessor :bic, :blz
11
+
12
+ class << self
11
13
 
12
- def self.get_all
13
- banks = []
14
- File.read(file, encoding: 'iso-8859-1').lines.each do |line|
15
- kennzeichen = line[7..10]
16
- if kennzeichen == '0000'
17
- bic = line[284..294]
18
- banks << new(bic: bic)
14
+ delegate :where, :only, to: :query
15
+
16
+ def all
17
+ @@all ||= get_all
18
+ end
19
+
20
+ def get_all
21
+ banks = []
22
+ File.read(file, encoding: 'iso-8859-1').lines.each do |line|
23
+ kennzeichen = line[7..10]
24
+ if kennzeichen == '0000'
25
+ blz = line[2..6].strip
26
+ bic = line[284..294]
27
+ banks << new(bic: bic, blz: blz)
28
+ end
19
29
  end
30
+ banks
20
31
  end
21
- banks
22
- end
23
32
 
24
- def self.file
25
- File.dirname(__FILE__) + '/../../data/bcbankenstamm.txt'
33
+ def file
34
+ File.dirname(__FILE__) + '/../../data/bcbankenstamm.txt'
35
+ end
26
36
  end
27
37
  end
@@ -1,4 +1,4 @@
1
1
  module BankingData
2
2
  # banking_data version
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
@@ -13,7 +13,11 @@ module BankingData
13
13
  ['76350000', '37040044'].each do |blz|
14
14
  it "includes #{blz}" do
15
15
  expect(GermanBank.only(:blz).map(&:first)).to include(blz)
16
- expect(Bank.where(locale: :de, blz: blz).only(:blz).first).to eq([blz])
16
+ expect(GermanBank.only(:blz).flatten).to include(blz)
17
+ expect(Bank.where(locale: :de, blz: blz).only(:blz).first)
18
+ .to eq([blz])
19
+ expect(Bank.where(blz: blz, locale: :de).only(:blz).last)
20
+ .to eq([blz])
17
21
  end
18
22
  end
19
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banking_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
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: 2013-10-18 00:00:00.000000000 Z
12
+ date: 2013-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -162,6 +162,7 @@ files:
162
162
  - lib/banking_data/austrian_bank.rb
163
163
  - lib/banking_data/bank.rb
164
164
  - lib/banking_data/german_bank.rb
165
+ - lib/banking_data/query.rb
165
166
  - lib/banking_data/swiss_bank.rb
166
167
  - lib/banking_data/version.rb
167
168
  - rubocop-todo.yml