eia 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed927fcb4b162ddc30273c63406d12164aeaa70e
4
+ data.tar.gz: 5282fbdedfb933c36dbd9cca95a2ca8aecf3ba6b
5
+ SHA512:
6
+ metadata.gz: 006b197bce6cebb3e49a03055fa5849b436a530308efd4cfc2b16b0ac17c905b2542f86216e62d3e523ff3e20eb9469dd7d30ce4d51187adc6b3cd91da81e7b8
7
+ data.tar.gz: 5a65e19e64c8fd35ba9d99b1111cf220fc449a4e8b2a280e810e4a25153bec031a6c5aad4dacfd0dfe93e53f22a45ffc1a3dd50e290ba6c4714ef48a413bd0b9
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rcampos@tendencias.com.br. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eia.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 rCamposCruz
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 rCamposCruz
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.
@@ -0,0 +1 @@
1
+ # eia
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "eia"
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(__FILE__)
@@ -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
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eia/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eia"
8
+ spec.version = Eia::VERSION
9
+ spec.authors = ["rCamposCruz"]
10
+ spec.email = ["rcampos@tendencias.com.br"]
11
+
12
+ spec.summary = %q{A gem tham aims to facilitate interactions with Brazil's IBGE WebService.}
13
+ spec.description = %q{This gem is supposed to supply a simple and intuitive interface between IBGE's SIDRA API, standardizing the output objects as a standard class. Also makes seamlessly to interact with the API, transforming the inputs for the webservice in intuitive function entries. Eia stands for Easy IBGE Acess and was developed in a partnership with Tendencias - Consultoria Econômica.}
14
+ spec.homepage = "https://github.com/rCamposCruz/eia"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "net", "~> 0.3"
28
+ spec.add_development_dependency "json", "~> 2.0"
29
+ end
@@ -0,0 +1,6 @@
1
+ require "eia/version"
2
+ require "eia/ibge"
3
+
4
+ module Eia
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,83 @@
1
+ class DataIBGE
2
+ def initialize(date, variable, location, product, unit, value, periodicity)
3
+ @date = standardize_date(date, periodicity)
4
+ @variable = variable
5
+ @location = location
6
+ @product = product
7
+ @unit = unit
8
+ @value = value
9
+
10
+ #The standard is:
11
+ # 5 : "Trimestral Móvel"
12
+ # 6 : "Ano"
13
+ @periodicity = periodicity
14
+ end
15
+
16
+ def standardize_date(date, periodicity)
17
+ #date is a four digit number
18
+ if periodicity == 6 then
19
+ return "01/01/#{date}"
20
+ elsif periodicity == 5 then
21
+ y = date[0..3]
22
+ m = date[4..5]
23
+
24
+ return "01/#{m}/#{y}"
25
+ else
26
+ puts "\nError parsing date for DataIBGE. Attempted to parse #{date}.\n"
27
+ return "ERROR"
28
+ end
29
+ end
30
+
31
+ def date
32
+ return @date
33
+ end
34
+
35
+ def variable
36
+ return @variable
37
+ end
38
+
39
+ def location
40
+ return @location
41
+ end
42
+
43
+ def product
44
+ return @product
45
+ end
46
+
47
+ def unit
48
+ return @unit
49
+ end
50
+
51
+ def value
52
+ return @value.to_f
53
+ end
54
+
55
+ def is_valid?
56
+ if @date == '' or @date == "ERROR" or @date == nil then
57
+ puts "Date found is invalid. Value is '#{@date}'."
58
+ return false
59
+ end
60
+
61
+ if @variable == '' or @variable == nil then
62
+ puts "Variable found is invalid. Value is '#{@variable}'."
63
+ return false
64
+ end
65
+
66
+ if @location == '' or @location == nil then
67
+ puts "Location found is invalid. Value is '#{@location}'."
68
+ return false
69
+ end
70
+
71
+ if @unit == '' or @unit == nil then
72
+ puts "Unit found is invalid. Value is '#{@unit}'."
73
+ return false
74
+ end
75
+
76
+ if @value == '' or @value == nil then
77
+ puts "Value found is invalid. Value is '#{@value}'."
78
+ return false
79
+ end
80
+
81
+ return true
82
+ end
83
+ end
@@ -0,0 +1,123 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require_relative 'data'
4
+
5
+ class IBGE
6
+ def initialize()
7
+ @webservice_url = 'http://api.sidra.ibge.gov.br'
8
+ end
9
+
10
+ def connected?()
11
+ response = Net::HTTP.get(URI(@webservice_url + '/values/t/1612/n1/1'))
12
+
13
+ if response.to_s == '' then
14
+ return false
15
+ else
16
+ return true
17
+ end
18
+ end
19
+
20
+ # What is expected:
21
+ # code : a single primary key which corresponds to category of series in IBGE's system;
22
+ # period : a string with any acceptable set of parameters
23
+ # variables : a list of acceptable variables the the series
24
+ # territorial_leve : a string in the form n_lvl+0|territories_0;n_lvl_1|territories_1;...
25
+ # where n_lvl_k is a number from 1 to 6 and territories_i is a valid
26
+ # string form specifying terriories
27
+ # classification : a string in the form c_lvl_k | list_of_products
28
+ def get_series(code, period, variables, territorial_level, classification)
29
+ url = @webservice_url + "/values"
30
+
31
+ if code != '' and code != nil then
32
+ url += "/t/#{code}"
33
+ end
34
+
35
+ if period != '' and period != nil then
36
+ url += "/p/#{period}"
37
+ else
38
+ url += "/p/all"
39
+ end
40
+
41
+ if variables != '' and variables != nil then
42
+ url += "/v/#{variables}"
43
+ else
44
+ url += "/v/all"
45
+ end
46
+
47
+ if territorial_level != '' and territorial_level != nil then
48
+ territorial_level.split(';').each do |word|
49
+ ws = word.split('|')
50
+ url += "/n#{ws[0]}/#{ws[1]}"
51
+ end
52
+ else
53
+ url += "/n1/all"
54
+ end
55
+
56
+ if classification != '' and classification != nil then
57
+ ws = classification.split('|')
58
+ url += "/c#{ws[0]}/#{ws[1]}"
59
+ end
60
+
61
+ return consume_json(Net::HTTP.get(URI(url)))
62
+ end
63
+
64
+ def consume_json(json_string)
65
+ begin
66
+ output = JSON.parse(json_string)
67
+ heading = output.delete_at(0)
68
+ identifier = heading["D1C"]
69
+
70
+ if identifier.include? "Trimestre Móvel" then
71
+ periodicity = 5
72
+ elsif identifier.include? "Ano" then
73
+ periodicity = 6
74
+ else
75
+ puts "Error! Unexpected case! Found is: #{identifier}. Report to the dev team."
76
+ return Array.new
77
+ end
78
+
79
+ data_array = Array.new
80
+
81
+ output.each do |hash|
82
+ data = nil
83
+ date = nil
84
+ variable = nil
85
+ product = nil
86
+ location = nil
87
+ unit = nil
88
+ val = nil
89
+
90
+ hash.each do |key, value|
91
+ case key
92
+ when "D1N" #date
93
+ date = value
94
+ when "D2N" #variable
95
+ variable = value
96
+ when "D3N" #location
97
+ location = value
98
+ when "D4N" #product
99
+ product = value
100
+ when "MN" #unit
101
+ unit = value
102
+ when "V" #value
103
+ val = value.to_f
104
+ else
105
+ #This information is discarded
106
+ end
107
+ end
108
+
109
+ data_array << DataIBGE.new(date, variable, location, product, unit, val, periodicity)
110
+ end
111
+
112
+ return data_array
113
+
114
+ rescue Exception => e
115
+ if e.to_s.include? "incompatível com a tabela" then
116
+ return Array.new
117
+ else
118
+ puts "Error parsing the JSON response: #{e}"
119
+ return nil
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,25 @@
1
+ module Eia
2
+ VERSION = "1.0.0"
3
+
4
+ #Standard for version: xx.yy.zz
5
+ # xx - release
6
+ # yy - feature
7
+ # zz - hotfix
8
+
9
+ # Version 0.1.1
10
+ # Fixes gemspec refference to net.
11
+ # Adds refference to ibge.rb in lib/eba.rb
12
+ # Fixes implementation of get in test_connection
13
+
14
+ # Version 0.2.1
15
+ # Adds rspec test for connection with the webservice
16
+
17
+ # Version 0.3.1
18
+ # Adds data standardization
19
+
20
+ # Version 1.0.0
21
+ # First release.
22
+ # Fully supports most data points from IBGE.
23
+ # Tests cover all features.
24
+ # Appropriate error treatment.
25
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eia
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - rCamposCruz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.14'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '10.0'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.3'
61
+ name: net
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.0'
75
+ name: json
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: This gem is supposed to supply a simple and intuitive interface between IBGE's SIDRA API, standardizing the output objects as a standard class. Also makes seamlessly to interact with the API, transforming the inputs for the webservice in intuitive function entries. Eia stands for Easy IBGE Acess and was developed in a partnership with Tendencias - Consultoria Econômica.
84
+ email:
85
+ - rcampos@tendencias.com.br
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - eia.gemspec
102
+ - lib/eia.rb
103
+ - lib/eia/data.rb
104
+ - lib/eia/ibge.rb
105
+ - lib/eia/version.rb
106
+ homepage: https://github.com/rCamposCruz/eia
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.8
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A gem tham aims to facilitate interactions with Brazil's IBGE WebService.
130
+ test_files: []