eba 1.10.2 → 1.11.0

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: c529de9a64d7728b5c6e203dcc0fd428587866e9
4
- data.tar.gz: 569a5cbecb3b8c50503682f247ea090aeb890cdb
3
+ metadata.gz: 2026c412008409324969c24f09c9fa6b0eaf4f20
4
+ data.tar.gz: 0a431597ac2a25aff61ecf8ead7b3014671d22a5
5
5
  SHA512:
6
- metadata.gz: 69211d896f0081ac80c217feb0def3c065ca6555baf3a64cb5feb442976ce1f01d951f9b17718ef5642e85f6baacf0e7a1681f58f569f342ddd7e55e98802054
7
- data.tar.gz: 7be34be91dc8372ddbf888ea9121794dc114138d514f3c34cf6a33d59f64af09c8e2d1b7c124470b3553ef647e69020f139918b58063df22f0e751a4dce9d23f
6
+ metadata.gz: 940c9aae47a31650a110bc12e938256e107a37d5664037395dd893cf5d6bce5e77ec889fd92cd5253b275c8ac7d4223ac9d36e540fad9eb6b5557fd29fa0c485
7
+ data.tar.gz: 5d4f61453fb3b7ec6efc3cba12f0967d99669adc05093d6007f4c9072f85f59751b056d71ceeed5e9eeffa3623563ef1128a6c6223f60fe2bb097b074d630d5b
@@ -2,11 +2,10 @@ require "savon"
2
2
  require "net/https"
3
3
  require "nokogiri"
4
4
  require "date"
5
- require_relative "encoder"
6
- require_relative "data"
7
5
 
8
- class BCB < Encoder
6
+ require_relative "helper"
9
7
 
8
+ class BCB < Helper
10
9
  ################################################################################################
11
10
  # #
12
11
  # You MUST supply a valid certificate in order for the connection to work! #
@@ -27,6 +26,7 @@ class BCB < Encoder
27
26
 
28
27
  def initialize(path_to_certificate)
29
28
  @pub_key = path_to_certificate
29
+
30
30
  connect_to_service()
31
31
  end
32
32
 
@@ -44,87 +44,44 @@ class BCB < Encoder
44
44
  return @service.operations
45
45
  end
46
46
 
47
- # Removes all invalid series from an array.
48
- #
49
- # An invalid series has last value.
50
- def purge_invalid_series(array_of_codes)
51
- result = []
52
-
47
+ def hash_last_value_with_code(array_of_codes)
48
+ result = {}
49
+
53
50
  array_of_codes.each do |code|
54
- if get_last_value(code) != nil
55
- result << code
56
- end
51
+ result[code] = get_last_value(code)
57
52
  end
58
53
 
59
- return result
54
+ result
60
55
  end
61
56
 
62
57
  def hash_by_periodicity(array_of_codes)
63
- purged_array_of_codes = purge_invalid_series(array_of_codes)
58
+ last_values = hash_last_value_with_code(array_of_codes)
59
+ purged_array_of_codes = purge_invalid_series(array_of_codes, last_values)
64
60
  result = {}
65
61
 
66
62
  purged_array_of_codes.each do |code|
67
- dado = get_last_value(code)
68
-
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
63
+ dado = last_values[code]
75
64
 
76
- result[dado.periodicity] << dado
65
+ if not result.key? dado.periodicity then
66
+ result[dado.periodicity] = []
77
67
  end
78
- end
79
-
80
- return result
81
- end
82
68
 
83
- def build_bcb_data(name, code, periodicity, unit, day, month, year, value, is_unseasoned)
84
- if is_unseasoned == nil then
85
- is_unseasoned = name.include? " - com ajuste sazonal"
86
-
87
- if is_unseasoned then
88
- name.slice! " - com ajuste sazonal"
89
- end
69
+ result[dado.periodicity] << dado
90
70
  end
91
71
 
92
- encoded_name = encode(name)
93
- encoded_periodicity = encode(periodicity)
94
- encoded_unit = encode(unit)
95
- encoded_day = day
96
- encoded_month = month
97
- encoded_year = year
98
- encoded_value = encode(value)
99
-
100
- return Data_bcb.new(encoded_name, code, encoded_periodicity,
101
- encoded_unit, encoded_day, encoded_month,
102
- encoded_year, encoded_value, is_unseasoned)
72
+ return result
103
73
  end
104
74
 
75
+
105
76
  def get_last_value(series_code)
106
77
  begin
107
78
  response = @service.call(:get_ultimo_valor_xml, message: {in0: "#{series_code}"})
108
-
109
79
  rescue Exception => e
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 ||
116
- e.message.to_s["Failed to open TCP connection"] != nil ||
117
- e.message.to_s["Socket closed"] != nil then
118
- puts "Will have to try last value again for #{series_code}, webservice dropped the ball.\nError: #{e.message}"
119
-
120
- else
121
- puts "Error requesting last value.\nMessage: #{e.message}\nTrace: #{e.backtrace}"
122
- end
123
-
124
- return nil
80
+ return Data_bcb.invalid_data()
125
81
  end
126
82
 
127
- response = response.to_hash[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return].sub("&", "-_1532_-")
83
+ response = response.to_hash[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return].sub("&",
84
+ "-_1532_-")
128
85
 
129
86
  xmlResult = Nokogiri::XML(response)
130
87
 
@@ -139,13 +96,13 @@ class BCB < Encoder
139
96
  # VALOR = VALUe
140
97
 
141
98
  return build_bcb_data(xmlResult.search("NOME").text.sub("-_1532_-", "&"),
142
- series_code,
143
- xmlResult.search("PERIODICIDADE").text,
144
- xmlResult.search("UNIDADE").text.sub("-_1532_-", "&"),
145
- xmlResult.search("DIA").text,
146
- xmlResult.search("MES").text,
147
- xmlResult.search("ANO").text,
148
- xmlResult.search("VALOR").text, nil)
99
+ series_code,
100
+ xmlResult.search("PERIODICIDADE").text,
101
+ xmlResult.search("UNIDADE").text.sub("-_1532_-", "&"),
102
+ xmlResult.search("DIA").text,
103
+ xmlResult.search("MES").text,
104
+ xmlResult.search("ANO").text,
105
+ xmlResult.search("VALOR").text, false)
149
106
  end
150
107
 
151
108
  # Ensure that date is in the format dd/MM/YYY
@@ -177,7 +134,7 @@ class BCB < Encoder
177
134
 
178
135
  result = send_message(message)
179
136
 
180
- if result != [] then
137
+ if result != nil then
181
138
  i = 0
182
139
 
183
140
  result.css("SERIE").each do |serie|
@@ -191,55 +148,17 @@ class BCB < Encoder
191
148
  return data_collection
192
149
  end
193
150
 
194
- def extract_an_item(serie, code, base_data, data_collection)
195
- # recover identifying data from the getLastValue method,
196
- # as the get_valores_series_xml desn't have identifying data
197
- # as series name, periodicity, etc.
198
-
199
- if serie.inspect["name=\"ID\" value=\"#{code}\""] != nil then
200
- serie.css("ITEM").each do |item|
201
- dia = "01"
202
- mes = "1"
203
- ano = "1"
204
- data = item.css("DATA").text.split("/")
205
-
206
- if base_data.periodicity == 'D' then
207
- dia = data[0]
208
- mes = data[1]
209
- ano = data[2]
210
- else
211
- mes = data[0]
212
- ano = data[1]
213
- end
214
-
215
- data_collection << build_bcb_data(base_data.name, code,
216
- base_data.periodicity,
217
- base_data.unit,
218
- dia, mes, ano,
219
- item.css("VALOR").text,
220
- base_data.seasonally_adjusted)
221
- end
222
- end
223
- end
224
-
225
151
  def send_message(message)
152
+ result = nil
153
+
226
154
  # try and catch, as some series can be discontinued or a code may be broken
227
155
  begin
228
156
  response = @service.call(:get_valores_series_xml, message: message)
229
157
  result = Nokogiri::XML(response.to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return])
230
158
  rescue Exception => erro
231
- #The interval is empty, therefore an empty array should be returned.
232
- if erro.to_s.include? "Value(s) not found" then
233
- puts "Some of the codes might be invalid. Requested: #{message[:in0][:long]}."
234
- return []
235
-
236
- elsif erro.message.to_s["Socket closed"] != nil then
237
- puts "\n\nSocket closed for message #{message[:in0][:long]}, try again."
238
-
239
- else
240
- 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"
241
- return []
242
- end
159
+ result = nil
243
160
  end
161
+
162
+ result
244
163
  end
245
164
  end
@@ -30,6 +30,10 @@ class Data_bcb
30
30
  return @name + @periodicity.to_s + @unit
31
31
  end
32
32
 
33
+ def self.invalid_data()
34
+ Data_bcb.new(nil, nil, nil, nil, 1, 1, 1900, 0, false)
35
+ end
36
+
33
37
  def is_valid?
34
38
 
35
39
  if @name == nil or @name == '' then
@@ -0,0 +1,75 @@
1
+ require_relative "data"
2
+ require_relative "encoder"
3
+
4
+ class Helper < Encoder
5
+ def initialize()
6
+ end
7
+
8
+ def build_bcb_data(name, code, periodicity, unit, day, month, year, value, is_unseasoned)
9
+ if name[" - com ajuste sazonal"] != nil then
10
+ is_unseasoned = true
11
+
12
+ if is_unseasoned then
13
+ name.slice!(" - com ajuste sazonal")
14
+ end
15
+ end
16
+
17
+ encoded_name = encode(name)
18
+ encoded_periodicity = encode(periodicity)
19
+ encoded_unit = encode(unit)
20
+ encoded_day = day
21
+ encoded_month = month
22
+ encoded_year = year
23
+ encoded_value = encode(value)
24
+
25
+ return Data_bcb.new(encoded_name, code, encoded_periodicity,
26
+ encoded_unit, encoded_day, encoded_month,
27
+ encoded_year, encoded_value, is_unseasoned)
28
+ end
29
+
30
+ # Removes all invalid series from an array.
31
+ #
32
+ # An invalid series has last value.
33
+ def purge_invalid_series(array_of_codes, hash_last_values)
34
+ result = []
35
+
36
+ array_of_codes.each do |code|
37
+ if hash_last_values[code] != nil then
38
+ result << code
39
+ end
40
+ end
41
+
42
+ return result
43
+ end
44
+
45
+ def extract_an_item(serie, code, base_data, data_collection)
46
+ # recover identifying data from the getLastValue method,
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("/")
56
+
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
65
+
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)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -5,7 +5,7 @@ module Eba
5
5
  # ff - commits on feature
6
6
  # hh - commits on hotfix
7
7
 
8
- VERSION = "1.10.2"
8
+ VERSION = "1.11.0"
9
9
 
10
10
  #Version 1.0.1
11
11
  #
@@ -79,4 +79,8 @@ module Eba
79
79
 
80
80
  #Version 1.10.2
81
81
  # Adds "Socket closed" error to last value expcetion handling.
82
+
83
+ #Versuib 1.11.0
84
+ # Refactors and simplifies interactions with target webservice. Methods no longer return nil,
85
+ # but an invalid Data_bcb object.
82
86
  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.10.2
4
+ version: 1.11.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-08-23 00:00:00.000000000 Z
11
+ date: 2018-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,7 @@ files:
116
116
  - lib/eba/bcb.rb
117
117
  - lib/eba/data.rb
118
118
  - lib/eba/encoder.rb
119
+ - lib/eba/helper.rb
119
120
  - lib/eba/version.rb
120
121
  homepage: https://github.com/rCamposCruz/eba
121
122
  licenses: