eba 1.9.8 → 1.10.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eba/bcb.rb +85 -53
  3. data/lib/eba/version.rb +5 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f4398411b85da2cdfcfb9ea03cd4f88443d996c
4
- data.tar.gz: 35f835445073fc8bcbc096149951c6e66a561ee9
3
+ metadata.gz: 6c8f8bfc9230658756c334083f0d367a98bfd6ea
4
+ data.tar.gz: 440a724553efb904de4814e2e6f10fea1774bc14
5
5
  SHA512:
6
- metadata.gz: 2832fffc06dee526bf80d7be522f92242add3fc8ee558b53c93b6682b7d6c9e0a6dfe7634e07511eaa726dc2d9aa2dd4d823eb752de5d589131879e16f0aad89
7
- data.tar.gz: 36bbccafa125cf2655f1523107997ef98c44a1a81c6fa26bccfe4aaaf8751cf620914e2ea0e56459a35e9314d415bcf33772f632e28a97285d13697699c62cbf
6
+ metadata.gz: b4efdde2ef97014e1b721f6b7289169698fa95136e4c842b47a1b0af4041314f410c92ca7ecec9a4313945ac2e3b71844c52fd81f7d70e6c36a320838913638e
7
+ data.tar.gz: 0ecf43867304d3234f38cf99db52ce327b7106314fa49702eb6f6e18d62063ff72901c2a4817ba364bbbdd015669935b395de6968191b20dde374f9ccb486d54
@@ -65,12 +65,16 @@ class BCB < Encoder
65
65
 
66
66
  purged_array_of_codes.each do |code|
67
67
  dado = get_last_value(code)
68
-
69
- if not result.key? dado.periodicity then
70
- result[dado.periodicity] = []
71
- end
72
68
 
73
- result[dado.periodicity] << dado
69
+ if dado == nil then
70
+ puts "No valid last value for #{code}."
71
+ else
72
+ if not result.key? dado.periodicity then
73
+ result[dado.periodicity] = []
74
+ end
75
+
76
+ result[dado.periodicity] << dado
77
+ end
74
78
  end
75
79
 
76
80
  return result
@@ -101,8 +105,21 @@ class BCB < Encoder
101
105
  def get_last_value(series_code)
102
106
  begin
103
107
  response = @service.call(:get_ultimo_valor_xml, message: {in0: "#{series_code}"})
108
+
104
109
  rescue => e
105
- puts "Error requesting last value. Error: #{e}\nTrace: #{e.backtrace}"
110
+ if e.message.to_s["No such operation 'getUltimoValorXML'"] != nil ||
111
+ e.message.to_s["Server.userException"] != nil then
112
+ #This error is expetected, it only means that the code is invalid.
113
+
114
+ elsif e.message.to_s["nil:NilClass"] != nil ||
115
+ e.message.to_s["Connection reset by peer"] != nil then
116
+ sleep(1)
117
+ get_last_value(series_code)
118
+
119
+ else
120
+ puts "Error requesting last value.\nMessage: #{e.message}\nTrace: #{e.backtrace}"
121
+ end
122
+
106
123
  return nil
107
124
  end
108
125
 
@@ -133,7 +150,7 @@ class BCB < Encoder
133
150
  # Ensure that date is in the format dd/MM/YYY
134
151
  def get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s)
135
152
  result = nil
136
- data_collection = Array.new()
153
+ data_collection = Array.new()
137
154
 
138
155
  # This request has a limit of series he can get at a time, thus
139
156
  # it's way simpler to break a composite requests in various smaller
@@ -157,58 +174,73 @@ class BCB < Encoder
157
174
  in1: min_date,
158
175
  in2: max_date}
159
176
 
160
- # try and catch, as some series can be discontinued or a code may be broken
161
- begin
162
- response = @service.call(:get_valores_series_xml, message: message)
163
- result = Nokogiri::XML(response.to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return])
164
- rescue Exception => erro
165
- #The interval is empty, therefore an empty array should be returned.
166
- if erro.to_s.include? "Value(s) not found" then
167
- return []
168
- else
169
- puts "\n\nError requesting! #{erro}\nTrace: #{erro.backtrace}\n\n"
170
- end
171
- end
177
+ result = send_message(message)
178
+
179
+ if result != [] then
180
+ i = 0
172
181
 
173
- i = 0
174
-
175
- result.css("SERIE").each do |serie|
176
- # recover identifying data from the getLastValue method,
177
- # as the get_valores_series_xml desn't have identifying data
178
- # as series name, periodicity, etc.
179
- base_data = data_array[i]
180
- comp = 'name="ID" value="' + codes[i].to_s + '"'
181
-
182
- if serie.inspect.include? comp
183
- serie.css("ITEM").each do |item|
184
- dia = "01"
185
- mes = "1"
186
- ano = "1"
187
- data = item.css("DATA").text.split("/")
188
-
189
- if base_data.periodicity == 'D' then
190
- dia = data[0]
191
- mes = data[1]
192
- ano = data[2]
193
- else
194
- mes = data[0]
195
- ano = data[1]
196
- end
197
-
198
- data_collection << build_bcb_data(base_data.name, codes[i],
199
- base_data.periodicity,
200
- base_data.unit,
201
- dia, mes, ano,
202
- item.css("VALOR").text,
203
- base_data.seasonally_adjusted)
204
- end
182
+ result.css("SERIE").each do |serie|
183
+ extract_an_item(serie, codes[i], data_array[i], data_collection)
184
+ i = i + 1
205
185
  end
206
-
207
- i = i + 1
208
186
  end
209
187
  end
210
188
  end
211
189
 
212
190
  return data_collection
213
191
  end
192
+
193
+ def extract_an_item(serie, code, base_data, data_collection)
194
+ # recover identifying data from the getLastValue method,
195
+ # as the get_valores_series_xml desn't have identifying data
196
+ # as series name, periodicity, etc.
197
+
198
+ if serie.inspect["name=\"ID\" value=\"#{code}\""] != nil then
199
+ serie.css("ITEM").each do |item|
200
+ dia = "01"
201
+ mes = "1"
202
+ ano = "1"
203
+ data = item.css("DATA").text.split("/")
204
+
205
+ if base_data.periodicity == 'D' then
206
+ dia = data[0]
207
+ mes = data[1]
208
+ ano = data[2]
209
+ else
210
+ mes = data[0]
211
+ ano = data[1]
212
+ end
213
+
214
+ data_collection << build_bcb_data(base_data.name, code,
215
+ base_data.periodicity,
216
+ base_data.unit,
217
+ dia, mes, ano,
218
+ item.css("VALOR").text,
219
+ base_data.seasonally_adjusted)
220
+ end
221
+ end
222
+ end
223
+
224
+ def send_message(message)
225
+ # try and catch, as some series can be discontinued or a code may be broken
226
+ begin
227
+ response = @service.call(:get_valores_series_xml, message: message)
228
+ result = Nokogiri::XML(response.to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return])
229
+ rescue Exception => erro
230
+ #The interval is empty, therefore an empty array should be returned.
231
+ if erro.to_s.include? "Value(s) not found" then
232
+ puts "Some of the codes might be invalid. Requested: #{message[:in0][:long]}."
233
+ return []
234
+
235
+ elsif erro.message.to_s["Socket closed"] != nil then
236
+ sleep(1)
237
+ puts "\n\nSocket closed for message #{message[:in0][:long]}, trying again."
238
+ send_message(message)
239
+
240
+ else
241
+ puts "\n\nError requesting all data for the range [#{min_date}, #{max_date}] for codes #{message[:in0][:long]}! #{erro}\nTrace: #{erro.backtrace}\nMessage: #{erro.message}\n"
242
+ return []
243
+ end
244
+ end
245
+ end
214
246
  end
@@ -5,7 +5,7 @@ module Eba
5
5
  # ff - commits on feature
6
6
  # hh - commits on hotfix
7
7
 
8
- VERSION = "1.9.8"
8
+ VERSION = "1.10.0"
9
9
 
10
10
  #Version 1.0.1
11
11
  #
@@ -69,4 +69,8 @@ module Eba
69
69
 
70
70
  #Version 1.9.8
71
71
  # Improves logging by adding trace.
72
+
73
+ #Version 1.10.0
74
+ # Agressively handles connection errors with BCB webservice, due to detecting a huge
75
+ # ammount of hangups and such and such erros whilst using the gem.
72
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.8
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Campos Cruz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-18 00:00:00.000000000 Z
11
+ date: 2018-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement