bank_scrap 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9c667caab2fefb56490b67d4e33be414f969ea7
4
- data.tar.gz: af3751b08dfd95655f0cd86f9eaf0f13e186a6cf
3
+ metadata.gz: 5728e4928e376190709d30ebacdcedb8fef94113
4
+ data.tar.gz: 911e7754d1e4d5c5c188ec25c1e19e3e39854b64
5
5
  SHA512:
6
- metadata.gz: 946cf536efb91eebb2b69d23b88d18b10d9a89cf73adc29fe6f23207388ed4078b36b829d45d049af76a6fff8d2ac8bf8532942dd95dfb6b531ac9e8f9a0f158
7
- data.tar.gz: e63351124eaf1920397fcf116d0359d9a897948f61cab3ad84329ff7a4ea01336cf935e08a1d0a648e12d56bcf2504096f0f41d8a053df405ff90e08bdaf945e
6
+ metadata.gz: e215a5088f8c0e212d8b2e51f1e5a51a7018ba6c6fb1706d2f6b3dfb36bb40d6bc20096331b653e34bc105dd8203ca0f7975f8bced8543ffd229b2503a40ef41
7
+ data.tar.gz: fd9c1b0500c2c9932ed4460bbcfd31ea8a960b438eb21a299f037865736aa341b8535a4a8b2e83f6163b8e5f9f75ea0540adecc1c46ad86eac68ccf1179d6965
@@ -5,15 +5,15 @@ require 'tempfile'
5
5
 
6
6
  module BankScrap
7
7
  class Ing < Bank
8
-
9
8
  BASE_ENDPOINT = 'https://ing.ingdirect.es/'
10
9
  LOGIN_ENDPOINT = BASE_ENDPOINT + 'genoma_login/rest/session'
11
10
  POST_AUTH_ENDPOINT = BASE_ENDPOINT + 'genoma_api/login/auth/response'
12
11
  CLIENT_ENDPOINT = BASE_ENDPOINT + 'genoma_api/rest/client'
13
12
  PRODUCTS_ENDPOINT = BASE_ENDPOINT + 'genoma_api/rest/products'
14
13
 
15
- SAMPLE_WIDTH = 30
16
- SAMPLE_HEIGHT = 30
14
+ SAMPLE_WIDTH = 30
15
+ SAMPLE_HEIGHT = 30
16
+ SAMPLE_ROOT_PATH = '/ing/numbers'
17
17
 
18
18
  def initialize(user, password, log: false, debug: false, extra_args:)
19
19
  @dni = user
@@ -28,8 +28,8 @@ module BankScrap
28
28
  super
29
29
  end
30
30
 
31
- def get_balance
32
- log 'get_balance'
31
+ def balances
32
+ log 'get_balances'
33
33
  balances = {}
34
34
  total_balance = 0
35
35
  @accounts.each do |account|
@@ -41,23 +41,17 @@ module BankScrap
41
41
  balances
42
42
  end
43
43
 
44
- def raw_accounts_data
45
- @raw_accounts_data
46
- end
47
-
48
44
  def fetch_accounts
49
45
  log 'fetch_accounts'
50
- set_headers({
51
- "Accept" => '*/*',
46
+ set_headers(
47
+ 'Accept' => '*/*',
52
48
  'Content-Type' => 'application/json; charset=utf-8'
53
- })
49
+ )
54
50
 
55
51
  @raw_accounts_data = JSON.parse(get(PRODUCTS_ENDPOINT))
56
52
 
57
- @raw_accounts_data.collect do |account|
58
- if account['iban']
59
- build_account(account)
60
- end
53
+ @raw_accounts_data.map do |account|
54
+ build_account(account) if account['iban']
61
55
  end.compact
62
56
  end
63
57
 
@@ -67,9 +61,9 @@ module BankScrap
67
61
  # The API allows any limit to be passed, but we better keep
68
62
  # being good API citizens and make a loop with a short limit
69
63
  params = {
70
- fromDate: start_date.strftime("%d/%m/%Y"),
71
- toDate: end_date.strftime("%d/%m/%Y"),
72
- limit: 25,
64
+ fromDate: start_date.strftime('%d/%m/%Y'),
65
+ toDate: end_date.strftime('%d/%m/%Y'),
66
+ limit: 25,
73
67
  offset: 0
74
68
  }
75
69
 
@@ -77,7 +71,9 @@ module BankScrap
77
71
  loop do
78
72
  request = get("#{PRODUCTS_ENDPOINT}/#{account.id}/movements", params)
79
73
  json = JSON.parse(request)
80
- transactions += json['elements'].collect { |transaction| build_transaction(transaction, account) }
74
+ transactions += json['elements'].map do |transaction|
75
+ build_transaction(transaction, account)
76
+ end
81
77
  params[:offset] += 25
82
78
  break if (params[:offset] > json['total']) || json['elements'].blank?
83
79
  end
@@ -93,12 +89,12 @@ module BankScrap
93
89
  end
94
90
 
95
91
  def login
96
- set_headers({
97
- "Accept" => 'application/json, text/javascript, */*; q=0.01',
92
+ set_headers(
93
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
98
94
  'Content-Type' => 'application/json; charset=utf-8'
99
- })
95
+ )
100
96
 
101
- param = {
97
+ params = {
102
98
  loginDocument: {
103
99
  documentType: 0,
104
100
  document: @dni.to_s
@@ -108,74 +104,76 @@ module BankScrap
108
104
  device: 'desktop'
109
105
  }
110
106
 
111
- response = JSON.parse(post(LOGIN_ENDPOINT, param.to_json))
112
- positions = response['pinPositions']
113
- pinpad = response['pinpad']
114
-
115
- pinpad_numbers_paths = save_pinpad_numbers(pinpad)
116
- pinpad_numbers = recognize_pinpad_numbers(pinpad_numbers_paths)
107
+ response = JSON.parse(post(LOGIN_ENDPOINT, params.to_json))
108
+ current_pinpad_paths = save_pinpad_numbers(response['pinpad'])
109
+ pinpad_numbers = recognize_pinpad_numbers(current_pinpad_paths)
117
110
 
118
- get_correct_positions(pinpad_numbers, positions)
111
+ get_correct_positions(pinpad_numbers, response['pinPositions'])
119
112
  end
120
113
 
121
114
  def pass_pinpad(positions)
122
- set_headers({
123
- "Accept" => 'application/json, text/javascript, */*; q=0.01',
124
- 'Content-Type' => 'application/json; charset=utf-8'
125
- })
126
-
127
- param = "{\"pinPositions\": #{positions}}"
128
- response = put(LOGIN_ENDPOINT, param)
115
+ response = put(LOGIN_ENDPOINT, { pinPositions: positions }.to_json)
129
116
  JSON.parse(response)['ticket']
130
117
  end
131
118
 
132
119
  def post_auth(ticket)
133
- set_headers({
134
- "Accept" => 'application/json, text/javascript, */*; q=0.01',
135
- 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
136
- })
120
+ set_headers(
121
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
122
+ 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
123
+ )
137
124
 
138
- param = "ticket=#{ticket}&device=desktop"
139
- post(POST_AUTH_ENDPOINT, param)
125
+ params = "ticket=#{ticket}&device=desktop"
126
+ post(POST_AUTH_ENDPOINT, params)
140
127
  end
141
128
 
142
129
  def save_pinpad_numbers(pinpad)
143
- pinpad_numbers_paths = []
144
- pinpad.each_with_index do |digit,index|
130
+ current_pinpad_paths = []
131
+ pinpad.each_with_index do |digit, index|
145
132
  tmp = Tempfile.new(["pinpad_number_#{index}", '.png'])
146
- File.open(tmp.path, 'wb'){ |f| f.write(Base64.decode64(digit)) }
147
- pinpad_numbers_paths << tmp.path
133
+ File.open(tmp.path, 'wb') { |f| f.write(Base64.decode64(digit)) }
134
+ current_pinpad_paths << tmp.path
148
135
  end
149
136
 
150
- pinpad_numbers_paths
137
+ current_pinpad_paths
151
138
  end
152
139
 
153
- def recognize_pinpad_numbers(pinpad_numbers_paths)
154
- pinpad_numbers = []
155
- pinpad_images = Magick::ImageList.new(*pinpad_numbers_paths)
140
+ def recognize_pinpad_numbers(current_pinpad_paths)
141
+ real_numbers = []
142
+ current_numbers = Magick::ImageList.new(*current_pinpad_paths)
156
143
  0.upto(9) do |i|
157
- single_number = pinpad_images[i]
158
- differences = []
144
+ current_number_img = current_numbers[i]
145
+ pixel_diffs = []
159
146
  0.upto(9) do |j|
160
- pinpad_pixels_sample = single_number.get_pixels(0,0, SAMPLE_WIDTH, SAMPLE_HEIGHT)
161
-
162
- img = Magick::ImageList.new(File.join(File.dirname(__FILE__), "/ing/numbers/pinpad#{j}.png")).first
163
- number_pixels_sample = img.get_pixels(0, 0, SAMPLE_WIDTH, SAMPLE_HEIGHT)
164
- diff = 0
165
- pinpad_pixels_sample.each_with_index do |pixel, index|
166
- sample_pixel = number_pixels_sample[index]
167
- diff += (pixel.red - sample_pixel.red).abs +
168
- (pixel.green - sample_pixel.green).abs +
169
- (pixel.blue - sample_pixel.blue).abs
170
- end
171
- differences << diff
147
+ sample_number_img = Magick::ImageList.new(sample_number_path(j)).first
148
+ pixel_diffs << images_diff(sample_number_img, current_numbers[i])
172
149
  end
150
+ real_numbers << pixel_diffs.each_with_index.min.last
151
+ end
152
+ real_numbers
153
+ end
173
154
 
174
- real_number = differences.each_with_index.min.last
175
- pinpad_numbers << real_number
155
+ def sample_number_path(number)
156
+ File.join(File.dirname(__FILE__), "#{SAMPLE_ROOT_PATH}/#{number}.png")
157
+ end
158
+
159
+ def images_diff(sample_number_img, current_number_img)
160
+ diff = 0
161
+ sample_pixels = pixels_from_coordinates(sample_number_img, 0, 0)
162
+ current_pixels = pixels_from_coordinates(current_number_img, 0, 0)
163
+ current_pixels.each_with_index do |pixel, index|
164
+ sample_pixel = sample_pixels[index]
165
+ red_diff = (pixel.red - sample_pixel.red).abs
166
+ green_diff = (pixel.green - sample_pixel.green).abs
167
+ blue_diff = (pixel.blue - sample_pixel.blue).abs
168
+
169
+ diff += red_diff + green_diff + blue_diff
176
170
  end
177
171
 
178
- pinpad_numbers
172
+ diff
173
+ end
174
+
175
+ def pixels_from_coordinates(img, x, y)
176
+ img.get_pixels(x, y, SAMPLE_WIDTH, SAMPLE_HEIGHT)
179
177
  end
180
178
 
181
179
  def get_correct_positions(pinpad_numbers, positions)
@@ -212,7 +210,7 @@ module BankScrap
212
210
  id: data['uuid'],
213
211
  amount: data['amount'],
214
212
  currency: data['EUR'],
215
- effective_date: data['effectiveDate'],
213
+ effective_date: Date.strptime(data['effectiveDate'], "%d/%m/%Y"),
216
214
  description: data['description'],
217
215
  balance: data['balance']
218
216
  )
@@ -1,3 +1,3 @@
1
1
  module BankScrap
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bank_scrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Sánchez
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-01-29 00:00:00.000000000 Z
14
+ date: 2015-02-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -186,16 +186,16 @@ files:
186
186
  - lib/bank_scrap/banks/bankinter.rb
187
187
  - lib/bank_scrap/banks/bbva.rb
188
188
  - lib/bank_scrap/banks/ing.rb
189
- - lib/bank_scrap/banks/ing/numbers/pinpad0.png
190
- - lib/bank_scrap/banks/ing/numbers/pinpad1.png
191
- - lib/bank_scrap/banks/ing/numbers/pinpad2.png
192
- - lib/bank_scrap/banks/ing/numbers/pinpad3.png
193
- - lib/bank_scrap/banks/ing/numbers/pinpad4.png
194
- - lib/bank_scrap/banks/ing/numbers/pinpad5.png
195
- - lib/bank_scrap/banks/ing/numbers/pinpad6.png
196
- - lib/bank_scrap/banks/ing/numbers/pinpad7.png
197
- - lib/bank_scrap/banks/ing/numbers/pinpad8.png
198
- - lib/bank_scrap/banks/ing/numbers/pinpad9.png
189
+ - lib/bank_scrap/banks/ing/numbers/0.png
190
+ - lib/bank_scrap/banks/ing/numbers/1.png
191
+ - lib/bank_scrap/banks/ing/numbers/2.png
192
+ - lib/bank_scrap/banks/ing/numbers/3.png
193
+ - lib/bank_scrap/banks/ing/numbers/4.png
194
+ - lib/bank_scrap/banks/ing/numbers/5.png
195
+ - lib/bank_scrap/banks/ing/numbers/6.png
196
+ - lib/bank_scrap/banks/ing/numbers/7.png
197
+ - lib/bank_scrap/banks/ing/numbers/8.png
198
+ - lib/bank_scrap/banks/ing/numbers/9.png
199
199
  - lib/bank_scrap/cli.rb
200
200
  - lib/bank_scrap/config.rb
201
201
  - lib/bank_scrap/locale/en.yml