Class: BCB
Instance Method Summary collapse
- #connect_to_service ⇒ Object
-
#get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s) ⇒ Object
Ensure that date is in the format dd/MM/YYY.
- #get_last_value(series_code) ⇒ Object
- #hash_by_periodicity(array_of_codes) ⇒ Object
- #hash_last_value_with_code(array_of_codes) ⇒ Object
-
#initialize(path_to_certificate, path_to_ca_certificate) ⇒ BCB
constructor
# 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 both the file ‘bcb.gov.br (… - year)’ file as the certificate AND the # ‘Cadeia de CAs de *.bcb.gov.br (validação estendida - 2018)’ file as the CA.
-
#list_operations ⇒ Object
List of all operations available for the webservice, useful for expanding the gem.
- #send_message(message) ⇒ Object
Methods inherited from Helper
#build_bcb_data, #extract_an_item, #purge_invalid_series
Methods inherited from Encoder
Constructor Details
#initialize(path_to_certificate, 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 both the file ‘bcb.gov.br (… - year)’ file as the certificate AND the # ‘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. # #
23 24 25 26 27 28 |
# File 'lib/eba/bcb.rb', line 23 def initialize(path_to_certificate, path_to_ca_certificate) @cert = path_to_certificate @ca = path_to_ca_certificate connect_to_service() end |
Instance Method Details
#connect_to_service ⇒ Object
30 31 32 33 34 35 |
# File 'lib/eba/bcb.rb', line 30 def connect_to_service() @service = Savon.client({wsdl: "https://www3.bcb.gov.br/wssgs/services/FachadaWSSGS?wsdl", ssl_cert_file: @cert, 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) ⇒ Object
Ensure that date is in the format dd/MM/YYY
105 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 138 139 140 141 142 143 144 145 |
# File 'lib/eba/bcb.rb', line 105 def get_all_data_for_array(array_of_codes, min_date, max_date = Time.now.strftime('%d/%m/%Y').to_s) result = nil data_collection = Array.new() # This request has a limit of series he can get at a time, thus # it's way simpler to break a composite requests in various smaller # requests. The Data_bcb class serves as a way to organize such data # and allow the developer to easily identify which series each data # object pertains. array_of_codes.each_slice(50).to_a.each do |array| hash = hash_by_periodicity(array) hash.each do |periodicity, array| codes = [] data_array = [] array.each do |data| data_array << data codes << data.pk end # Build the message from the start of the historical series = { in0: {long: codes}, in1: min_date, in2: max_date} result = () if result != nil then i = 0 result.css("SERIE").each do |serie| extract_an_item(serie, codes[i], data_array[i], data_collection) i = i + 1 end end end end return data_collection end |
#get_last_value(series_code) ⇒ Object
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 |
# File 'lib/eba/bcb.rb', line 72 def get_last_value(series_code) begin response = @service.call(:get_ultimo_valor_xml, message: {in0: "#{series_code}"}) rescue Exception => e 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) # As it's a brazillian database it's column identifications are in portuguese, # the translation for the fields, in order, are: # NOME = NAME # PERIODICIDADE = PERIODICITY # UNIDADE = UNIT # DIA = DAY # MES = MONTH # ANO = YEAR # VALOR = VALUe return build_bcb_data(xmlResult.search("NOME").text.sub("-_1532_-", "&"), series_code, xmlResult.search("PERIODICIDADE").text, xmlResult.search("UNIDADE").text.sub("-_1532_-", "&"), xmlResult.search("DIA").text, xmlResult.search("MES").text, xmlResult.search("ANO").text, xmlResult.search("VALOR").text, false) end |
#hash_by_periodicity(array_of_codes) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/eba/bcb.rb', line 53 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/eba/bcb.rb', line 43 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.
39 40 41 |
# File 'lib/eba/bcb.rb', line 39 def list_operations() return @service.operations end |
#send_message(message) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/eba/bcb.rb', line 147 def () result = nil # try and catch, as some series can be discontinued or a code may be broken begin response = @service.call(:get_valores_series_xml, message: ) result = Nokogiri::XML(response.to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return]) rescue Exception => erro result = nil end result end |