banco_central 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a36e500aeff4984b6185cecd696a15f8e9e69a2
4
+ data.tar.gz: e270fb5b16fdc440b703ff937f22a9eeee160242
5
+ SHA512:
6
+ metadata.gz: 3fedc03f32483bcc6b1cc7e3a6f6da7d479e52f746f85ce79e52f6554b6692de2ee390bce218f6536323ff1abb411b6b4028a4167dbcbfe0cde32c873b303d24
7
+ data.tar.gz: 1b5df7a09c2aca72b224a97412267bed3a87b0d7d5b7417ef9e10f321e6429337e38848a0af900e8cd8acbb429dbbb4deb9abe44e38e1ae331d50ebbe937e425
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at hgtesta@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in banco_central.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Henrique G. Testa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # BancoCentral
2
+
3
+ A handy Ruby library to consume Central Bank of Brazil (Banco Central do Brasil) WebService. The WebService provided by the BC has lots of updated economic indicators, but it fails short in documentation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'banco_central'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install banco_central
20
+
21
+ ## Usage
22
+
23
+ ### last
24
+
25
+ Get the last value of the an indicator, and also the name, unit, date and periodicity. This method calls `GetUltimoValorXml` method from the WebService.
26
+
27
+ ```ruby
28
+ BancoCentral.last(:dolar)
29
+ # => {: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}
30
+ ```
31
+
32
+ Note that `:dolar` is a convenient label that is translated to a number before the WebService call. There are thousands of indicators, some deprecated, but just a few dozen labels. The code below is the equivalent of the above one.
33
+
34
+ ```ruby
35
+ BancoCentral.last(1)
36
+ ```
37
+
38
+ Check `BancoCentral::Labels` for a list of available labels, or take a look at the config/labels.yml file.
39
+
40
+ ### find
41
+
42
+ Get the indicator's value for a specific date. It calls `GetValor` method from the WebService and returns only a float number.
43
+
44
+ ```ruby
45
+ BancoCentral.find(:ipca, "1/1/2010")
46
+ # => 0.75
47
+ ```
48
+
49
+ ### all
50
+
51
+ Get all the values of the indicator. This method calls `GetValoresSeriesXMLResponse` method from the WebService.
52
+
53
+ ```ruby
54
+ BancoCentral.all(:ipca)
55
+ # => {"1/1980"=>"6.62", "2/1980"=>"4.62", "3/1980"=>"6.04", ...
56
+ ```
57
+
58
+ You can also specify an initial or final date:
59
+
60
+ ```ruby
61
+ BancoCentral.all(:ipca, start: "1/1/2010")
62
+ BancoCentral.all(:ipca, finish: "1/5/2015")
63
+ BancoCentral.all(:ipca, start: "1/1/2013", finish: "1/12/2013")
64
+ ```
65
+
66
+ It is also possible to ask for multiple indicators in the same request. In that case, the result hash will contain a hash for each indicator.
67
+ ```ruby
68
+ BancoCentral.all([:importacoes, :exportacoes])
69
+ # => {2946=>{"1/2000"=>"3453879475", "2/2000"=>"4124889858", ...},
70
+ # 3034=>{"1/2000"=>"3568862639", "2/2000"=>"4046750398", ...}}
71
+ ```
72
+
73
+ The indicators must have the same periodicity.
74
+
75
+ ### Logging
76
+
77
+ To specify the log level use the `:log_level` option. Valid values are `:fatal`, `:error`, `:warn`, `:info`, `:debug`. In practice, it will affect the output of Savon calls.
78
+ ```ruby
79
+ BancoCentral.all(:ipca, log_level: :debug)
80
+ ```
81
+
82
+ The default logger is STDOUT. To use a different one just use the `:logger` option.
83
+ ```ruby
84
+ BancoCentral.all(:ipca, log_level: :debug, logger: @logger)
85
+ ```
86
+
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hgtesta/banco_central. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
91
+
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
96
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'banco_central/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "banco_central"
8
+ spec.version = BancoCentral::VERSION
9
+ spec.authors = ["Henrique G. Testa"]
10
+ spec.email = ["hgtesta@gmail.com"]
11
+
12
+ spec.summary = %q{Fetch social and economic indicators from Central Bank of Brazil (Banco Central do Brasil).}
13
+ spec.description = %q{Fetch social and economic indicators from Central Bank of Brazil (Banco Central do Brasil).}
14
+ spec.homepage = "http://github.com/hgtesta/banco_central"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "savon", "~> 2.11"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.12"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency "activesupport", "~> 5.0"
36
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "banco_central"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config/labels.yml ADDED
@@ -0,0 +1,48 @@
1
+
2
+ wsdl_uri: "https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl"
3
+
4
+ labels:
5
+
6
+ # Miscellany
7
+ bovespa: 7
8
+ poupanca: 25
9
+ pib: 4380
10
+ inpc: 188
11
+ igpm: 189
12
+ ipca: 433
13
+ selic: 4189
14
+ selic_diaria: 11
15
+ meta_selic: 432
16
+ importacoes: 3034
17
+ exportacoes: 2946
18
+ salario_minimo: 1619
19
+ ouro: 4
20
+
21
+ # Currency
22
+ dolar: 1
23
+ dolar_venda: 1
24
+ dolar_compra: 10813
25
+ euro: 21619
26
+ euro_venda: 21619
27
+ euro_compra: 21620
28
+ iene: 21621
29
+ iene_venda: 21621
30
+ iene_compra: 21622
31
+ libra: 21623
32
+ libra_venda: 21623
33
+ libra_compra: 21624
34
+ franco_suico: 21625
35
+ franco_suico_venda: 21625
36
+ franco_suico_compra: 21626
37
+ coroa_dinamarquesa: 21627
38
+ coroa_dinamarquesa_venda: 21627
39
+ coroa_dinamarquesa_compra: 21628
40
+ coroa_norueguesa: 21629
41
+ coroa_norueguesa_venda: 21629
42
+ coroa_norueguesa_compra: 21630
43
+ dolar_australiano: 21633
44
+ dolar_australiano_venda: 21633
45
+ dolar_australiano_compra: 21634
46
+ dolar_canadense: 21635
47
+ dolar_canadense_venda: 21635
48
+ dolar_canadense_compra: 21636
@@ -0,0 +1,3 @@
1
+ module BancoCentral
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,122 @@
1
+ require "banco_central/version"
2
+ require "savon"
3
+ require "yaml"
4
+
5
+ # BancoCentral.last(:ipca)
6
+ # BancoCentral.all(:ipca)
7
+ # BancoCentral.all(:ipca, start: "1/7/2014")
8
+ # BancoCentral.all(:ipca, finish: "1/8/2014")
9
+ # BancoCentral.all(:ipca, start: "1/7/2014", finish: "1/8/2014")
10
+ # BancoCentral.all([:importacoes, :exportacoes])
11
+ # BancoCentral.find(:dolar, date: "3/7/2014")
12
+ # BancoCentral::LABELS
13
+ module BancoCentral
14
+
15
+ CONFIG = YAML.load_file(File.join File.dirname(__dir__), "config/labels.yml")
16
+
17
+ WSDL_URI = CONFIG["wsdl_uri"]
18
+
19
+ LABELS = CONFIG["labels"]
20
+
21
+ class << self
22
+
23
+ def find(id, date, log_level: nil, logger: nil)
24
+ client(log_level, logger).call(
25
+ :get_valor,
26
+ message: {
27
+ in0: get_id(id),
28
+ in1: date
29
+ }
30
+ ).to_hash[:multi_ref].to_f
31
+ end
32
+
33
+ def last(id, log_level: nil, logger: nil)
34
+ xml = client(log_level, logger).call(
35
+ :get_ultimo_valor_xml,
36
+ message: {
37
+ in0: get_id(id)
38
+ }
39
+ ).to_hash[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return]
40
+
41
+ # Prevent exception when parsing if indicator's name has the & character
42
+ xml.gsub!(/&(?!(?:amp|lt|gt|quot|apos);)/, '&amp;')
43
+
44
+ hash = Nori.new.parse(xml)["resposta"]["SERIE"]
45
+
46
+ {
47
+ id: get_id(id),
48
+ name: hash["NOME"],
49
+ unit: hash["UNIDADE"],
50
+ date: parse_date(hash["DATA"]),
51
+ value: parse_value(hash["VALOR"]),
52
+ periodicity: parse_periodicity(hash["PERIODICIDADE"])
53
+ }
54
+ end
55
+
56
+ # BancoCentral.all([:ipca], start: "1/7/2014")
57
+ def all(id, start: nil, finish: nil, log_level: nil, logger: nil)
58
+
59
+ # Build request arguments
60
+ ids = id.is_a?(Array) ? id : [id]
61
+ args = {}
62
+ ids.each_with_index do |_id, i|
63
+ args["ins#{i}:int"] = get_id(_id)
64
+ end
65
+
66
+ # Request the WebService
67
+ xml = client(log_level, logger).call(
68
+ :get_valores_series_xml,
69
+ message: {
70
+ in0: args,
71
+ in1: start,
72
+ in2: finish
73
+ }
74
+ ).to_hash[:get_valores_series_xml_response][:get_valores_series_xml_return]
75
+
76
+ # Convert response XML to a hash (for one id) or a hash of hashes (more than one id)
77
+ doc = Nokogiri::XML(xml) { |config| config.noblanks }
78
+ data = {}
79
+ doc.css("SERIE").each do |serie|
80
+ array = serie.css("DATA, VALOR").map(&:text)
81
+ data[serie["ID"].to_i] = Hash[array.each_slice(2).to_a]
82
+ end
83
+
84
+ id.is_a?(Array) ? data : data[get_id(id)]
85
+ end
86
+
87
+ private
88
+
89
+ def client(log_level = nil, logger = nil)
90
+ options = {
91
+ encoding: "UTF-8",
92
+ ssl_verify_mode: :none,
93
+ wsdl: WSDL_URI
94
+ }
95
+ if log_level
96
+ options[:log] = true
97
+ options[:log_level] = log_level # one of [:debug, :info, :warn, :error, :fatal]
98
+ options[:logger] = logger if logger
99
+ end
100
+ Savon.client(options)
101
+ end
102
+
103
+ def get_id(id)
104
+ return id if id.is_a? Integer
105
+ LABELS[id.to_s] || raise(ArgumentError, 'Label not found')
106
+ end
107
+
108
+ def parse_periodicity(periodicity)
109
+ {"D" => :daily, "M" => :monthly, "T" => :quarterly, "A" => :yearly}[periodicity]
110
+ end
111
+
112
+ def parse_date(date)
113
+ Time.new(date["ANO"], date["MES"], date["DIA"])
114
+ end
115
+
116
+ def parse_value(value)
117
+ value.gsub(",", ".").to_f
118
+ end
119
+
120
+ end
121
+
122
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: banco_central
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Henrique G. Testa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Fetch social and economic indicators from Central Bank of Brazil (Banco
84
+ Central do Brasil).
85
+ email:
86
+ - hgtesta@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - banco_central.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - config/labels.yml
102
+ - lib/banco_central.rb
103
+ - lib/banco_central/version.rb
104
+ homepage: http://github.com/hgtesta/banco_central
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ allowed_push_host: https://rubygems.org
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.5.1
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Fetch social and economic indicators from Central Bank of Brazil (Banco Central
129
+ do Brasil).
130
+ test_files: []