bankscrap-sabadell 2.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c3a678be14795749c8cf88a02a7c067ec99383c
4
+ data.tar.gz: 241619ee9b454fa2737a4c5b57dbcf40d8d10722
5
+ SHA512:
6
+ metadata.gz: ba7dafb6e282acac4eeb2cfddc9fa15248b60efae55b1aa5325df45313033035661355be17d3e9afb265ccfaa4c5c0c669e1c1e829b09e212370fd6a09571c52
7
+ data.tar.gz: 9dab38b5014ef77bee0f16b17ccc16e2962a9ec61b71e884b4bb814f679107506c8d06eca8217c25eafc62c3184edd008b5da5fa324a17a39a611747b1093a90
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bankscrap-sabadell.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Bankscrap::Sabadell
2
+
3
+ [Bankscrap](https://github.com/bankscrap/bankscrap) adapter for Sabadell.
4
+
5
+ Bankscrap adapter for Banc Sabadell (Banco Sabadell): business and individual accounts.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bankscrap-sabadell'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install bankscrap-sabadell
23
+
24
+ ## Usage
25
+
26
+ ### From terminal
27
+ #### Bank account balance
28
+
29
+ $ bankscrap balance Sabadell --credentials=user:YOUR_USER --password:YOUR_PASSWORD --login_type:business|individual
30
+
31
+
32
+ #### Transactions
33
+
34
+ $ bankscrap transactions Sabadell --credentials=user:YOUR_USER --password:YOUR_PASSWORD --login_type:business|individual
35
+
36
+ ---
37
+
38
+ For more details on usage instructions please read [Bankscrap readme](https://github.com/bankscrap/bankscrap/#usage).
39
+
40
+ ### From Ruby code
41
+
42
+ ```ruby
43
+ require 'bankscrap-sabadell'
44
+ sabadell = Bankscrap::Sabadell::Bank.new(user: YOUR_USER, password: YOUR_PASSWORD, login_type: :business) # or :individual
45
+ ```
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/bankscrap/bankscrap-sabadell/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bankscrap/sabadell/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bankscrap-sabadell'
8
+ spec.version = Bankscrap::Sabadell::VERSION
9
+ spec.authors = ['Javier Cuevas']
10
+ spec.email = ['javi@diacode.com']
11
+ spec.summary = 'Sabadell adapter for Bankscrap'
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'bankscrap', '~> 2.0.5'
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'bankscrap/sabadell/bank'
2
+ require_relative 'bankscrap/sabadell/version'
@@ -0,0 +1,145 @@
1
+ require 'bankscrap'
2
+ require 'securerandom'
3
+
4
+ module Bankscrap
5
+ module Sabadell
6
+
7
+ class Bank < ::Bankscrap::Bank
8
+
9
+ # Define the endpoints for the Bank API here
10
+ BASE_ENDPOINT = 'https://www.bancsabadell.mobi/bsmobil/api'.freeze
11
+ LOGIN_ENDPOINT = '/session'.freeze
12
+ ACCOUNTS_ENDPOINT = '/products'.freeze
13
+ TRANSACTIONS_ENDPOINT = '/accounts/movements'.freeze
14
+
15
+ # It can be anything (user, birthday, ID, whatever you need).
16
+ REQUIRED_CREDENTIALS = %i(user password login_type).freeze
17
+
18
+ class InvalidLoginType < Exception; end
19
+
20
+ def initialize(credentials = {})
21
+ super do
22
+ # loginType
23
+ # 1 Individual
24
+ # 2 Business
25
+ # 3 CAL -> not supported
26
+ # 4 Secondary card -> not supported
27
+
28
+ @login_type = case @login_type.to_sym
29
+ when :individual then 1
30
+ when :business then 2
31
+ else
32
+ raise InvalidLoginType, "Invalid login_type: '#{@login_type}'"
33
+ end
34
+
35
+ add_headers('User-Agent' => 'ANDROID 6.0 Google+Nexus+6P+-+6.0.0+-+API+23+-+1440x2560 NATIVE_APP 17.2.0 STANDARD')
36
+ add_headers('Accept' => 'application/vnd.idk.bsmobil-v1720+json')
37
+ add_headers('Content-Type' => 'application/json; charset=utf-8')
38
+ end
39
+ end
40
+
41
+ # Fetch all the accounts for the given user
42
+ #
43
+ # Should returns an array of Bankscrap::Account objects
44
+ def fetch_accounts
45
+ response = get(BASE_ENDPOINT + ACCOUNTS_ENDPOINT)
46
+ json = JSON.parse(response)
47
+ json['accountProduct']['accounts'].map { |data| build_account(data) }
48
+
49
+ # TODO parse json['cardProduct'] for cards
50
+ end
51
+
52
+ # Fetch transactions for the given account.
53
+ #
54
+ # Account should be a Bankscrap::Account object
55
+ # Should returns an array of Bankscrap::Account objects
56
+ def fetch_transactions_for(account, start_date: Date.today - 1.month, end_date: Date.today)
57
+ # Example if the API expects a JSON POST request
58
+ params = {
59
+ account: account.raw_data,
60
+ dateFrom: format_date(start_date),
61
+ dateTo: format_date(end_date),
62
+ moreRequest: false
63
+ }
64
+
65
+ transactions = []
66
+ loop do
67
+ response = post(BASE_ENDPOINT + TRANSACTIONS_ENDPOINT, fields: params.to_json)
68
+ json = JSON.parse(response)
69
+ transactions += json['accountMovements'].map { |data| build_transaction(data, account) }
70
+
71
+ params[:moreRequest] = true # Switch this flag after first request
72
+ break unless json['moreElements']
73
+ end
74
+
75
+ transactions
76
+ end
77
+
78
+ private
79
+
80
+ # First request to login
81
+ def login
82
+ # Example if the API expects a JSON POST request
83
+ params = {
84
+ brand: "SAB",
85
+ contract: "",
86
+ csid: "00000000-00X0-0000-X000-000000000000",
87
+ deviceInfo: "en-US GEO() ANDROID 6.0 Google+Nexus+6P+-+6.0.0+-+API+23+-+1440x2560 NATIVE_APP 17.2.0 STANDARD",
88
+ geolocationData: "",
89
+ loginType: @login_type,
90
+ newDevice: true,
91
+ devicePrint: "",
92
+ password: @password,
93
+ requestId: "SDK",
94
+ userName: @user
95
+ }
96
+
97
+ post(BASE_ENDPOINT + LOGIN_ENDPOINT, fields: params.to_json)
98
+ end
99
+
100
+ # Build an Account object from API data
101
+ def build_account(data)
102
+ balance_cents = data['amount']['value'].scan(/-|\d/).join('').to_i
103
+ balance = Money.new(balance_cents, data['amount']['currency'])
104
+
105
+ Account.new(
106
+ bank: self,
107
+ id: data['number'],
108
+ name: "#{data['description']} #{data['iban']}",
109
+ available_balance: balance,
110
+ balance: balance,
111
+ iban: data['iban'],
112
+ description: data['description'],
113
+ raw_data: data
114
+ )
115
+ end
116
+
117
+ # Build a transaction object from API data
118
+ def build_transaction(data, account)
119
+ amount_cents = data['amount']['value'].scan(/-|\d/).join('').to_i
120
+ amount = Money.new(amount_cents, data['amount']['currency'])
121
+
122
+ balance_cents = data['balance']['value'].scan(/-|\d/).join('').to_i
123
+ balance = Money.new(balance_cents, data['balance']['currency'])
124
+
125
+ Transaction.new(
126
+ account: account,
127
+ id: data['apuntNumber'],
128
+ amount: amount,
129
+ description: data['concept'],
130
+ effective_date: parse_date(data['valueDate']),
131
+ operation_date: parse_date(data['date']),
132
+ balance: balance
133
+ )
134
+ end
135
+
136
+ def format_date(date)
137
+ date.strftime('%d-%m-%Y')
138
+ end
139
+
140
+ def parse_date(date_as_string)
141
+ Date.strptime(date_as_string, '%d-%m-%Y')
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,5 @@
1
+ module Bankscrap
2
+ module Sabadell
3
+ VERSION = '2.0.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bankscrap-sabadell
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Cuevas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bankscrap
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - javi@diacode.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bankscrap-sabadell.gemspec
68
+ - lib/bankscrap-sabadell.rb
69
+ - lib/bankscrap/sabadell/bank.rb
70
+ - lib/bankscrap/sabadell/version.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.8
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Sabadell adapter for Bankscrap
95
+ test_files: []