eba 2.0.1 → 2.0.2

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: 9139840c48d380ebc7f4bd5eb6b85a67e4ddfa2f
4
- data.tar.gz: 390f470ad98f6b9c505e6987d8bd4f0412966541
3
+ metadata.gz: c56777e9040d1597b33becb8043eb514fa610ebc
4
+ data.tar.gz: 382aa6bf66a61e127f215535594b9b59f5433f0d
5
5
  SHA512:
6
- metadata.gz: cd9bff1a14300d59f1de9631353710ba86b3a12ee2b9bd9f5dd7c6fc8b39fae50daeea998d5f5c573361eed7e4c00b57eeead6e5684153447e6adcf5529259d5
7
- data.tar.gz: c2e8642c495dcf63e6b6b85a098f785b8a77e42f111595e948c4e20c5a9224ea7b0e790eb70c4b91baafa3a49162aba99b6821b2d119644f531bb4a0f1b8e969
6
+ metadata.gz: 4dc1de3cb1aa0d03cc2990dda14c2dcb5c01667b8e2fce69698e008252c9cfecbea67a70f421378ed776ccc6b7f9eafc8444000be8022b9bb9a7e55c1ce88c14
7
+ data.tar.gz: efde2e490fde9f0c2fe212e4853f8e2a2d37d92b9712eddf749bce4d5277990c18e7a5d3ff2c9071b753137624bbb38f29f00e55a11cffebf9316999278f05f2
@@ -21,6 +21,7 @@ class BCB < Helper
21
21
 
22
22
  def initialize(path_to_ca_certificate)
23
23
  @ca = path_to_ca_certificate
24
+ @attempts = 0
24
25
  connect_to_service()
25
26
  end
26
27
 
@@ -88,56 +89,69 @@ class BCB < Helper
88
89
  # VALOR = VALUe
89
90
 
90
91
  return build_bcb_data(xmlResult.search("NOME").text.sub("-_1532_-", "&"),
91
- series_code,
92
- xmlResult.search("PERIODICIDADE").text,
93
- xmlResult.search("UNIDADE").text.sub("-_1532_-", "&"),
94
- xmlResult.search("DIA").text,
95
- xmlResult.search("MES").text,
96
- xmlResult.search("ANO").text,
97
- xmlResult.search("VALOR").text, false)
92
+ series_code,
93
+ xmlResult.search("PERIODICIDADE").text,
94
+ xmlResult.search("UNIDADE").text.sub("-_1532_-", "&"),
95
+ xmlResult.search("DIA").text,
96
+ xmlResult.search("MES").text,
97
+ xmlResult.search("ANO").text,
98
+ xmlResult.search("VALOR").text, false)
98
99
  end
99
100
 
100
101
  # Ensure that date is in the format dd/MM/YYY
101
- def get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s)
102
+ def get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s, slice_size=50)
102
103
  result = nil
103
- data_collection = Array.new()
104
+ data_collection = []
104
105
 
105
106
  # This request has a limit of series he can get at a time, thus
106
107
  # it's way simpler to break a composite requests in various smaller
107
108
  # requests. The Data_bcb class serves as a way to organize such data
108
109
  # and allow the developer to easily identify which series each data
109
110
  # object pertains.
111
+
112
+ if slice_size > 50 then
113
+ slice_size = 50
114
+ end
115
+
110
116
  array_of_codes.each_slice(50).to_a.each do |array|
111
117
  hash = hash_by_periodicity(array)
112
118
 
113
119
  hash.each do |periodicity, array|
114
- codes = []
115
- data_array = []
120
+ code_x_data = {}
116
121
 
117
122
  array.each do |data|
118
- data_array << data
119
- codes << data.pk
123
+ code_x_data[data.pk] = data
120
124
  end
121
125
 
122
126
  # Build the message from the start of the historical series
123
- message = { in0: {long: codes},
124
- in1: min_date,
125
- in2: max_date}
127
+ message = {in0: {long: code_x_data.keys},
128
+ in1: min_date,
129
+ in2: max_date}
126
130
 
127
131
  result = send_message(message)
128
-
132
+
129
133
  if result != nil then
130
134
  i = 0
131
135
 
132
136
  result.css("SERIE").each do |serie|
133
- extract_an_item(serie, codes[i], data_array[i], data_collection)
137
+ extract_an_item(serie, code_x_data, data_collection)
134
138
  i = i + 1
135
139
  end
136
140
  end
137
141
  end
138
142
  end
139
143
 
140
- return data_collection
144
+ if data_collection.size == 0 && array_of_codes.size > 0 && @attempts < 5 then
145
+ @attempts = @attempts + 1
146
+
147
+ puts "BCB WARNING: No data returned for #{array_of_codes}. Trying again #{@attempts}/5"
148
+ sleep(1 * @attempts)
149
+
150
+ data_collection = get_all_data_for_array(array_of_codes, min_date, max_date)
151
+ @attemps = 0
152
+ end
153
+
154
+ data_collection
141
155
  end
142
156
 
143
157
  def send_message(message)
@@ -35,44 +35,43 @@ class Data_bcb
35
35
  end
36
36
 
37
37
  def is_valid?
38
-
39
38
  if @name == nil or @name == '' then
40
- puts "Found invalid name! Value is '#{@name}'"
39
+ puts "BCB ERROR: Found invalid name! Value is '#{@name}' for #{@pk}."
41
40
  return false
42
41
  end
43
42
 
44
43
  if @periodicity == nil or @periodicity == '' or @periodicity.length > 1 then
45
- puts "Found invalid periodicity! Value is '#{@periodicity}'"
44
+ puts "BCB ERROR: Found invalid periodicity! Value is '#{@periodicity}' for #{@pk}."
46
45
  return false
47
46
  end
48
47
 
49
48
  if @unit == nil or @unit == '' then
50
- puts "Found invalid unit! Value is '#{@unit}'"
49
+ puts "BCB ERROR: Found invalid unit! Value is '#{@unit}' for #{@pk}."
51
50
  return false
52
51
  end
53
52
 
54
53
  if @date == nil or @date == '' then
55
- puts "Found invalid date! Value is '#{@date}'"
54
+ puts "BCB ERROR: Found invalid date! Value is '#{@date}' for #{@pk}."
56
55
  return false
57
56
  else
58
57
  if !(DateTime.parse(@date).to_date != nil rescue false) then
59
- puts "Found invalid date! Value is '#{@date}'"
58
+ puts "BCB ERROR: Found invalid date! Value is '#{@date}' for #{@pk}."
60
59
  return false
61
60
  end
62
61
  end
63
62
 
64
63
  if @value == nil then
65
- puts "Found invalid value! Value is '#{@value}'"
64
+ puts "BCB ERROR: Found invalid value! Value is '#{@value}' for #{@pk}."
66
65
  return false
67
66
  else
68
67
  if !(@value.to_f != nil rescue false) then
69
- puts "Found invalid value! Value is '#{@value}'"
68
+ puts "BCB ERROR: Found invalid value! Value is '#{@value}' for #{@pk}."
70
69
  return false
71
70
  end
72
71
  end
73
72
 
74
73
  if @pk == nil or @pk <= 0 then
75
- puts "Found invalid pk! Value is '#{@pk}'"
74
+ puts "BCB ERROR: Found invalid pk! Value is '#{@pk}'"
76
75
  return false
77
76
  end
78
77
 
@@ -42,33 +42,39 @@ class Helper < Encoder
42
42
  return result
43
43
  end
44
44
 
45
- def extract_an_item(serie, code, base_data, data_collection)
45
+ def extract_an_item(serie, code_x_data_hash, collection)
46
46
  # recover identifying data from the getLastValue method,
47
47
  # as the get_valores_series_xml desn't have identifying data
48
- # as series name, periodicity, etc.
49
-
50
- if serie.inspect["name=\"ID\" value=\"#{code}\""] != nil then
51
- serie.css("ITEM").each do |item|
52
- dia = "01"
53
- mes = "1"
54
- ano = "1"
55
- data = item.css("DATA").text.split("/")
48
+ # as series name, periodicity, etc.
49
+ if serie.to_s.inspect["SERIE ID"] != nil then
50
+ code = serie.to_s.match(/SERIE ID=\"([0-9]+)\"/)[1].to_i
51
+ base_data = code_x_data_hash[code]
56
52
 
57
- if base_data.periodicity == 'D' then
58
- dia = data[0]
59
- mes = data[1]
60
- ano = data[2]
61
- else
62
- mes = data[0]
63
- ano = data[1]
64
- end
53
+ if base_data != nil then
54
+ serie.css("ITEM").each do |item|
55
+ dia = "01"
56
+ mes = "1"
57
+ ano = "1"
58
+ data = item.css("DATA").text.split("/")
65
59
 
66
- data_collection << build_bcb_data(base_data.name, code,
67
- base_data.periodicity,
68
- base_data.unit,
69
- dia, mes, ano,
70
- item.css("VALOR").text,
71
- base_data.seasonally_adjusted)
60
+ if base_data.periodicity == 'D' then
61
+ dia = data[0]
62
+ mes = data[1]
63
+ ano = data[2]
64
+ else
65
+ mes = data[0]
66
+ ano = data[1]
67
+ end
68
+
69
+ collection << build_bcb_data(base_data.name, code,
70
+ base_data.periodicity,
71
+ base_data.unit,
72
+ dia, mes, ano,
73
+ item.css("VALOR").text,
74
+ base_data.seasonally_adjusted)
75
+ end
76
+ else
77
+ puts "ERROR BCB: Failure do locate #{code} in the data collection #{code_x_data_hash.keys}"
72
78
  end
73
79
  end
74
80
  end
@@ -5,7 +5,7 @@ module Eba
5
5
  # ff - commits on feature
6
6
  # hh - commits on hotfix
7
7
 
8
- VERSION = "2.0.1"
8
+ VERSION = "2.0.2"
9
9
 
10
10
  #Version 1.0.1
11
11
  #
@@ -89,4 +89,7 @@ module Eba
89
89
 
90
90
  #Version 2.0.1
91
91
  # Removes useless certificate and supplies only CA.
92
+
93
+ #Version 2.0.2
94
+ # Improves reliability for getting data and identifying it.
92
95
  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: 2.0.1
4
+ version: 2.0.2
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: 2019-01-31 00:00:00.000000000 Z
11
+ date: 2019-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement