Class: BCB
Instance Method Summary
collapse
Methods inherited from Helper
#build_bcb_data, #extract_an_item, #purge_invalid_series
Methods inherited from Encoder
#encode
Constructor Details
#initialize(path_to_ca_certificate) ⇒ BCB
# You MUST supply a valid certificate in order for the connection to work! # The certificate you are looking for is located in this webpage: # www.bcb.gov.br/estabilidadefinanceira/certificacaodigital # # You MUST supply ‘Cadeia de CAs de *.bcb.gov.br (validação estendida - 2018)’ file as the CA. # # With all this done, you will be able to access freely this # service, without much hassle. Don’t forget to upvote such an useful answer. # #
22
23
24
25
26
|
# File 'lib/eba/bcb.rb', line 22
def initialize(path_to_ca_certificate)
@ca = path_to_ca_certificate
@attempts = 0
connect_to_service()
end
|
Instance Method Details
#connect_to_service ⇒ Object
28
29
30
31
32
|
# File 'lib/eba/bcb.rb', line 28
def connect_to_service()
@service = Savon.client({wsdl: "https://www3.bcb.gov.br/wssgs/services/FachadaWSSGS?wsdl",
ssl_ca_cert_file: @ca,
headers: {'Accept-Encoding' => 'gzip, deflate'}})
end
|
#get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s, slice_size = 50) ⇒ Object
Ensure that date is in the format dd/MM/YYY
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/eba/bcb.rb', line 106
def get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s, slice_size=50)
result = nil
data_collection = []
if slice_size > 50 then
slice_size = 50
end
array_of_codes.each_slice(50).to_a.each do |array|
hash = hash_by_periodicity(array)
hash.each do |periodicity, array|
code_x_data = {}
array.each do |data|
code_x_data[data.pk] = data
end
scry_for_interval(min_date, max_date, code_x_data).each do |data|
data_collection << data
end
end
end
data_collection
end
|
#get_last_value(series_code) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/eba/bcb.rb', line 69
def get_last_value(series_code)
begin
response = @service.call(:get_ultimo_valor_xml, message: {in0: "#{series_code}"})
rescue HTTP::ConnectionError => e
puts "BCB Warning: hangup from source trying to get last value. Waiting and trying again."
sleep(1)
return get_last_value(series_code)
rescue OpenSSL::SSL::SSLError => e
puts "BCB Warning: hangup from source trying to get last value. Waiting and trying again."
sleep(1)
return get_last_value(series_code)
rescue Exception => e
puts "BCB ERROR: #{e.message}\n#{e.backtrace}\n#{e.class}"
return Data_bcb.invalid_data()
end
response = response.to_hash[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return].sub("&",
"-_1532_-")
xmlResult = Nokogiri::XML(response)
build_bcb_data(xmlResult)
end
|
#hash_by_periodicity(array_of_codes) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/eba/bcb.rb', line 50
def hash_by_periodicity(array_of_codes)
last_values = hash_last_value_with_code(array_of_codes)
purged_array_of_codes = purge_invalid_series(array_of_codes, last_values)
result = {}
purged_array_of_codes.each do |code|
dado = last_values[code]
if not result.key? dado.periodicity then
result[dado.periodicity] = []
end
result[dado.periodicity] << dado
end
return result
end
|
#hash_last_value_with_code(array_of_codes) ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/eba/bcb.rb', line 40
def hash_last_value_with_code(array_of_codes)
result = {}
array_of_codes.each do |code|
result[code] = get_last_value(code)
end
result
end
|
#list_operations ⇒ Object
List of all operations available for the webservice, useful for expanding the gem.
36
37
38
|
# File 'lib/eba/bcb.rb', line 36
def list_operations()
return @service.operations
end
|
#scry_for_interval(min_date, max_date, code_x_data) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/eba/bcb.rb', line 139
def scry_for_interval(min_date, max_date, code_x_data)
dc = []
message = {in0: {long: code_x_data.keys},
in1: min_date,
in2: max_date}
result = send_message(message)
if result != nil then
result.css("SERIE").each do |serie|
(serie, code_x_data, dc)
end
if dc.size == 0 && code_x_data.keys.size > 0 then
result.css("SERIE").each do |serie|
(serie, code_x_data, dc)
end
end
end
dc
end
|
#send_message(message) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/eba/bcb.rb', line 164
def send_message(message)
result = nil
begin
response = @service.call(:get_valores_series_xml, message: message)
result = Nokogiri::XML(response.to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return])
rescue HTTP::ConnectionError => e
puts "BCB Warning: hangup from source trying to get all data. Waiting and trying again."
sleep(1)
result = send_message(message)
rescue OpenSSL::SSL::SSLError => e
puts "BCB Warning: hangup from source trying to get last value. Waiting and trying again."
sleep(1)
result = sent_message(message)
rescue Exception => erro
result = nil
end
result
end
|