Module: BancoCentral
- Defined in:
- lib/banco_central.rb,
lib/banco_central/version.rb
Overview
Fetch social and economic indicators from the Central Bank of Brazil (Banco Central do Brasil) WebService. It fires a SOAP request behind the scenes and parse the result for easy use.
Constant Summary collapse
- CONFIG =
YAML.load_file(File.join(File.dirname(__dir__), 'config/labels.yml'))
- WSDL_URI =
CONFIG['wsdl_uri']
- LABELS =
CONFIG['labels']
- VERSION =
"1.0.0"
Class Attribute Summary collapse
-
.log_level ⇒ Object
Returns the value of attribute log_level.
-
.logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
-
.all(id, start: nil, finish: nil) ⇒ Object
Get all the values of the indicator.
-
.find(id, date) ⇒ Object
Get the indicator’s value for a specific date.
-
.last(id) ⇒ Object
Get the last value of the an indicator, and also the name, unit, date and periodicity.
Class Attribute Details
.log_level ⇒ Object
Returns the value of attribute log_level.
18 19 20 |
# File 'lib/banco_central.rb', line 18 def log_level @log_level end |
.logger ⇒ Object
Returns the value of attribute logger.
19 20 21 |
# File 'lib/banco_central.rb', line 19 def logger @logger end |
Class Method Details
.all(id, start: nil, finish: nil) ⇒ Object
Get all the values of the indicator. This method calls GetValoresSeriesXMLResponse
method from the WebService.
This method accepts a string, symbol or an array of string or symbols as indicator names. In case an array is given, it will return a hash of hashes.
BancoCentral.all(:ipca)
=> {"1/1980"=>"6.62", "2/1980"=>"4.62", ... }
BancoCentral.all(:ipca, start: "1/7/2014", finish: "1/8/2014")
=> {"7/2014"=>"0.01", "8/2014"=>"0.25"}
BancoCentral.all([:importacoes, :exportacoes])
=> {
2946 => {"1/1954"=>"122603000", "2/1954"=>"125851000", ...},
3034 => {"1/1973"=>"370706000", "2/1973"=>"390279000", ...}
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/banco_central.rb', line 81 def all(id, start: nil, finish: nil) indicators_xml = all_as_xml(id, start, finish) indicators_doc = Nokogiri::XML(indicators_xml, &:noblanks) # Convert response XML to a hash (for one id) or a hash of # hashes (for more than one id) indicators = {} indicators_doc.css('SERIE').each do |serie| array = serie.css('DATA, VALOR').map(&:text) indicators[serie['ID'].to_i] = Hash[array.each_slice(2).to_a] end id.is_a?(Array) ? indicators : indicators[label_to_int(id)] end |
.find(id, date) ⇒ Object
Get the indicator’s value for a specific date. It calls GetValor
method from the WebService and returns only a float number.
BancoCentral.find(:dolar, "3/7/2014")
=> 0.75
26 27 28 29 30 31 32 33 34 |
# File 'lib/banco_central.rb', line 26 def find(id, date) client.call( :get_valor, message: { in0: label_to_int(id), in1: date } ).to_hash[:multi_ref].to_f end |
.last(id) ⇒ Object
Get the last value of the an indicator, and also the name, unit, date and periodicity. This method calls GetUltimoValorXml
method from the WebService.
BancoCentral.last(:dolar)
=> {
:id => 1,
:name => "Taxa de câmbio - Livre - Dólar americano (venda) - diário",
:unit => "u.m.c./US$",
:date => 2016-10-18 00:00:00 -0200,
:value => 3.1874,
:periodicity => :daily
}
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/banco_central.rb', line 49 def last(id) indicator_xml = sanitize(last_as_xml(id)) indicator = Nori.new.parse(indicator_xml)['resposta']['SERIE'] { id: label_to_int(id), name: indicator['NOME'], unit: indicator['UNIDADE'], date: parse_date(indicator['DATA']), value: parse_value(indicator['VALOR']), periodicity: parse_periodicity(indicator['PERIODICIDADE']) } end |