sepa_clearer 0.0.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -1,20 +1,66 @@
1
+ require 'sqlite3'
2
+
1
3
  module SepaClearer
2
4
  class Clearer
3
- def initialize(clearer = 'DeutscheBundesbank')
4
- @klass = SepaClearer.const_get(clearer).new
5
+ attr_accessor :db
6
+
7
+ # Make sure we always have the correct order of columns
8
+ ATTRIBUTES = [:name, :bic, :sct, :core, :cor1, :b2b]
9
+
10
+ def initialize(db_name = 'deutsche_bundesbank')
11
+ self.db = SQLite3::Database.new File.join(File.dirname(__FILE__), '../..', "data/#{db_name}.db")
12
+ end
13
+
14
+ def add(provider)
15
+ data = boolean_to_integers(provider.to_a)
16
+ db.execute "INSERT INTO payment_providers (#{ATTRIBUTES.join(',')}) VALUES (?, ?, ?, ?, ?, ?)", data
5
17
  end
6
18
 
7
19
  def all
8
- @klass.data
20
+ db.execute("SELECT #{ATTRIBUTES.join(',')} FROM payment_providers ORDER BY name").map do |row|
21
+ PaymentProvider.new(hash_from_result(row))
22
+ end
9
23
  end
10
24
 
11
25
  def find_by_bic(bic)
12
- normalized_bic = normalize_bic(bic)
13
- @klass.data.find { |provider| provider.bic == normalized_bic }
26
+ db.execute("SELECT #{ATTRIBUTES.join(',')} FROM payment_providers WHERE bic = ?", normalize_bic(bic)).map do |row|
27
+ PaymentProvider.new(hash_from_result(row))
28
+ end.first
29
+ end
30
+
31
+ def delete
32
+ db.execute("DELETE FROM payment_providers").first
14
33
  end
15
34
 
16
35
  def normalize_bic(bic)
17
36
  bic.to_s.ljust(11, 'X').upcase
18
37
  end
38
+
39
+ protected
40
+
41
+ def hash_from_result(raw_array)
42
+ raw_array_with_boolean = integers_to_boolean(raw_array)
43
+ Hash[ATTRIBUTES.zip(raw_array_with_boolean)]
44
+ end
45
+
46
+ def boolean_to_integers(array)
47
+ array.map do |element|
48
+ if element.kind_of?(TrueClass) || element.kind_of?(FalseClass)
49
+ element ? 1 : 0
50
+ else
51
+ element
52
+ end
53
+ end
54
+ end
55
+
56
+ def integers_to_boolean(array)
57
+ array.map do |element|
58
+ if element.kind_of?(Integer)
59
+ element == 1
60
+ else
61
+ element
62
+ end
63
+ end
64
+ end
19
65
  end
20
66
  end
@@ -2,29 +2,28 @@ require 'csv'
2
2
 
3
3
  module SepaClearer
4
4
  class DeutscheBundesbank
5
- CAPABILITIES_MAPPING = {
6
- service_sct: :sct,
7
- service_sdd: :core,
8
- service_cor1: :cor1,
9
- service_b2b: :b2b
10
- }
5
+ MAPPING = {
6
+ service_sct: :sct,
7
+ service_sdd: :core,
8
+ service_cor1: :cor1,
9
+ service_b2b: :b2b
10
+ }
11
11
 
12
12
  def data(file = nil)
13
13
  @data ||= begin
14
14
  [].tap do |data|
15
- CSV.foreach(file || default_data_file, { headers: true, col_sep: ';', header_converters: :symbol }) do |row|
16
- data.push PaymentProvider.new(*parse_raw_data(row))
15
+ CSV.foreach(file || default_data_file, { headers: true, col_sep: ';', header_converters: :symbol, encoding: "iso-8859-1:UTF-8" }) do |row|
16
+ data.push PaymentProvider.new(parse_raw_data(row))
17
17
  end
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
22
  def parse_raw_data(data)
23
- [
24
- data[:name].strip.chomp,
25
- data[:bic],
26
- CAPABILITIES_MAPPING.map { |key, service| data[key] == '1' ? service : nil }.compact
27
- ]
23
+ MAPPING.reduce({}) { |hsh,(key, service)| hsh[service] = (data[key] == '1'); hsh }.merge({
24
+ name: data[:name].strip.chomp,
25
+ bic: data[:bic].strip.chomp,
26
+ })
28
27
  end
29
28
 
30
29
  def default_data_file
@@ -2,12 +2,45 @@ module SepaClearer
2
2
  class PaymentProvider
3
3
  SERVICES = [:sct, :core, :cor1, :b2b]
4
4
 
5
- attr_accessor :name, :bic, :capabilities
5
+ attr_accessor :name, :bic, :sct, :core, :cor1, :b2b
6
6
 
7
- def initialize(name, bic, capabilities)
8
- self.name = name
9
- self.bic = bic
10
- self.capabilities = capabilities
7
+ def initialize(options = {})
8
+ self.name = options[:name]
9
+ self.bic = options[:bic]
10
+ self.sct = options[:sct] || false
11
+ self.core = options[:core] || false
12
+ self.cor1 = options[:cor1] || false
13
+ self.b2b = options[:b2b] || false
14
+ end
15
+
16
+ def capabilities
17
+ SERVICES.map { |s| [s, send(s)] }.select { |_,v| v }.map { |k,v| k }
18
+ end
19
+
20
+ # Returns a list of provided direct debit methods sorted by there speed
21
+ # :cor1 => 2 business days
22
+ # :core => 3 business days (recurring) & 6 business days for first
23
+ def direct_debit_instruments
24
+ (capabilities & [:core, :cor1]).sort
25
+ end
26
+
27
+ def bic
28
+ @bic.to_s.ljust(11, 'X').upcase
29
+ end
30
+
31
+ def to_a
32
+ to_hash.values
33
+ end
34
+
35
+ def to_hash
36
+ {
37
+ name: name,
38
+ bic: bic,
39
+ sct: sct,
40
+ core: core,
41
+ cor1: cor1,
42
+ b2b: b2b,
43
+ }
11
44
  end
12
45
  end
13
46
  end
@@ -1,3 +1,3 @@
1
1
  module SepaClearer
2
- VERSION = '0.0.2'
2
+ VERSION = '0.5.1'
3
3
  end
data/sepa-clearer.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "sqlite3"
21
22
  spec.add_development_dependency "bundler", "~> 1.6"
22
23
  spec.add_development_dependency "rake", "~> 0"
23
24
  spec.add_development_dependency "rspec", "~> 3"
data/spec/clearer_spec.rb CHANGED
@@ -9,18 +9,22 @@ describe SepaClearer::Clearer do
9
9
 
10
10
  describe '#find_by_bic' do
11
11
  it 'returns the provider with the given BIC' do
12
- expect(subject.find_by_bic('AABAFI22TMS')).to be_kind_of(SepaClearer::PaymentProvider)
12
+ expect(subject.find_by_bic('AABAFI22')).to be_kind_of(SepaClearer::PaymentProvider)
13
13
  end
14
14
 
15
15
  it 'returns nil if no object matches the BIC' do
16
16
  expect(subject.find_by_bic('MYTHINGY')).to eq(nil)
17
17
  end
18
18
 
19
- it 'normalizes thebic before searching' do
19
+ it 'normalizes the bic before searching' do
20
20
  allow(subject).to receive(:normalize_bic).and_return('AAA')
21
21
  expect(subject.find_by_bic('MYTHINGY')).to eq(nil)
22
22
  expect(subject).to have_received(:normalize_bic)
23
23
  end
24
+
25
+ it 'returns an initialize object' do
26
+ expect(subject.find_by_bic('AABAFI22').name).to eq('BANK OF ALAND PLC')
27
+ end
24
28
  end
25
29
 
26
30
  describe '#normalize_bic' do
@@ -3,20 +3,24 @@ require 'spec_helper'
3
3
  describe SepaClearer::DeutscheBundesbank do
4
4
  describe '#parse_raw_data' do
5
5
  let(:data) { { name: "My Org \n", bic: 'QWERTZ12345', service_sct: '1' } }
6
- it 'returns an array' do
7
- expect(subject.parse_raw_data(data)).to eq(['My Org', 'QWERTZ12345', [:sct]])
6
+
7
+ it 'returns a hash' do
8
+ expect(subject.parse_raw_data(data)).to be_kind_of(Hash)
8
9
  end
9
10
 
10
11
  it 'cleans up the name' do
11
- expect(subject.parse_raw_data(data)[0]).to eq('My Org')
12
+ expect(subject.parse_raw_data(data)[:name]).to eq('My Org')
12
13
  end
13
14
 
14
15
  it 'returns the BIC' do
15
- expect(subject.parse_raw_data(data)[1]).to eq('QWERTZ12345')
16
+ expect(subject.parse_raw_data(data)[:bic]).to eq('QWERTZ12345')
16
17
  end
17
18
 
18
19
  it 'maps the capabilities' do
19
- expect(subject.parse_raw_data(data)[2]).to eq([:sct])
20
+ expect(subject.parse_raw_data(data)[:sct]).to eq(true)
21
+ expect(subject.parse_raw_data(data)[:core]).to eq(false)
22
+ expect(subject.parse_raw_data(data)[:cor1]).to eq(false)
23
+ expect(subject.parse_raw_data(data)[:b2b]).to eq(false)
20
24
  end
21
25
  end
22
26
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe SepaClearer::PaymentProvider do
4
4
  describe '.new' do
5
- subject(:provider) { described_class.new('Testorg', 'SOMEVALIDID', [:core]) }
5
+ subject(:provider) { described_class.new(name: 'Testorg', bic: 'SOMEVALIDID') }
6
6
 
7
7
  it 'sets the proper name' do
8
8
  expect(provider.name).to eq('Testorg')
@@ -12,8 +12,52 @@ describe SepaClearer::PaymentProvider do
12
12
  expect(provider.bic).to eq('SOMEVALIDID')
13
13
  end
14
14
 
15
- it 'sets the proper capabilities' do
16
- expect(provider.capabilities).to eq([:core])
15
+ it 'does not set any capabilities' do
16
+ expect(provider.capabilities).to eq([])
17
+ end
18
+ end
19
+
20
+ describe '#capabilities' do
21
+ subject(:provider) { described_class.new(sct: true, core: false, cor1: true, b2b: true) }
22
+
23
+ it 'returns all capabilities' do
24
+ expect(provider.capabilities).to eq([:sct, :cor1, :b2b])
25
+ end
26
+ end
27
+
28
+ describe '#direct_debit_instruments' do
29
+ it 'returns only DD instruments' do
30
+ allow(subject).to receive(:capabilities).and_return([:b2b, :cor1, :core, :sct])
31
+ expect(subject.direct_debit_instruments).to eq([:cor1, :core])
32
+ end
33
+
34
+ it 'returns faster cor1 first if order last' do
35
+ allow(subject).to receive(:capabilities).and_return([:core, :cor1])
36
+ expect(subject.direct_debit_instruments.first).to eq(:cor1)
37
+ end
38
+
39
+ it 'returns faster cor1 first if order properly' do
40
+ allow(subject).to receive(:capabilities).and_return([:cor1, :core])
41
+ expect(subject.direct_debit_instruments.first).to eq(:cor1)
42
+ end
43
+ end
44
+
45
+ describe '#to_hash' do
46
+ subject(:provider) { described_class.new(name: 'test', bic: 'bic', sct: true, core: false, cor1: true, b2b: true) }
47
+
48
+ it 'returns a hash' do
49
+ expect(provider.to_hash).to be_kind_of(Hash)
50
+ end
51
+
52
+ it 'sets correct values' do
53
+ expect(provider.to_hash).to match({
54
+ name: 'test',
55
+ bic: 'BICXXXXXXXX',
56
+ sct: true,
57
+ core: false,
58
+ cor1: true,
59
+ b2b: true
60
+ })
17
61
  end
18
62
  end
19
63
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sepa_clearer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maximilian Schulz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-17 00:00:00.000000000 Z
11
+ date: 2021-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +83,7 @@ files:
69
83
  - Rakefile
70
84
  - bin/update_data
71
85
  - data/deutsche_bundesbank.csv
86
+ - data/deutsche_bundesbank.db
72
87
  - lib/sepa_clearer.rb
73
88
  - lib/sepa_clearer/clearer.rb
74
89
  - lib/sepa_clearer/deutsche_bundesbank.rb
@@ -83,7 +98,7 @@ homepage: http://opensource.railslove.com/sepa-clearer
83
98
  licenses:
84
99
  - MIT
85
100
  metadata: {}
86
- post_install_message:
101
+ post_install_message:
87
102
  rdoc_options: []
88
103
  require_paths:
89
104
  - lib
@@ -98,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
113
  - !ruby/object:Gem::Version
99
114
  version: '0'
100
115
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.2.2
103
- signing_key:
116
+ rubygems_version: 3.0.8
117
+ signing_key:
104
118
  specification_version: 4
105
119
  summary: List of all available SEPA clearers and their supported features
106
120
  test_files:
@@ -108,4 +122,3 @@ test_files:
108
122
  - spec/deutsche_bundesbank_spec.rb
109
123
  - spec/payment_provider_spec.rb
110
124
  - spec/spec_helper.rb
111
- has_rdoc: