Class: BCB

Inherits:
Helper show all
Defined in:
lib/eba/bcb.rb

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
# File 'lib/eba/bcb.rb', line 22

def initialize(path_to_ca_certificate)
  @ca = path_to_ca_certificate
  connect_to_service()
end

Instance Method Details

#connect_to_serviceObject



27
28
29
30
31
# File 'lib/eba/bcb.rb', line 27

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) ⇒ Object

Ensure that date is in the format dd/MM/YYY



101
102
103
104
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
# File 'lib/eba/bcb.rb', line 101

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
      message = { in0: {long: codes}, 
                  in1: min_date, 
                  in2: max_date}

      result = send_message(message)

      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



68
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
# File 'lib/eba/bcb.rb', line 68

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eba/bcb.rb', line 49

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



39
40
41
42
43
44
45
46
47
# File 'lib/eba/bcb.rb', line 39

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_operationsObject

List of all operations available for the webservice, useful for expanding the gem.



35
36
37
# File 'lib/eba/bcb.rb', line 35

def list_operations()
  return @service.operations
end

#send_message(message) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/eba/bcb.rb', line 143

def send_message(message)
  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: 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