ibge 0.1.0

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: 6bb4210540e49df0ba3f713e8b414c58d4e7b422
4
+ data.tar.gz: 7dfaab976499b7e3ed845d4577450a29a1a90b8d
5
+ SHA512:
6
+ metadata.gz: 3dd4c04e05bc163d669871436d886c4dc6f3c7a5c7cabf968cac1690dccd7c1e966b365409296ddb594e03ae42fdadaa6256da827be86c73861f55b6a35888a9
7
+ data.tar.gz: 9b34cfd88f22b2fa56671f5b91f37c47365c31a8652326a0a28535f1a11ec5c82b754a097f6a5025288726f162ac357ea6d4fb710595459a46d2b88f4a21ef4a
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ script: "bundle exec rake spec"
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ibge.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bruno Arueira
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,50 @@
1
+ # Ibge
2
+
3
+ [![Build Status](https://travis-ci.org/brunoarueira/ibge.svg?branch=master)](https://travis-ci.org/brunoarueira/ibge)
4
+ [![Code Climate](https://codeclimate.com/github/brunoarueira/ibge/badges/gpa.svg)](https://codeclimate.com/github/brunoarueira/ibge)
5
+
6
+ Unofficial gem with a bunch of data collected by IBGE. The data collected are responsibility of IBGE (Instituto Brasileiro de Geografia e Estatística, in english Brazilian Institute of Geography and Statistics) and this gem makes an integration with ruby more easy.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ibge'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ibge
23
+
24
+ ## Usage
25
+
26
+ For now, the gem supports 6 types of information: State (without abbreviation yet), Meso region, Micro region, Municipality, District and Sub district.
27
+
28
+ To read any type, the readers has the same API like below:
29
+
30
+ ```ruby
31
+ # For state
32
+ Ibge::Reader::State.read #=> [{ code: '11', state_name: 'Rondônia' }, ... { code: '53', state_name: 'Distrito Federal' }]
33
+
34
+ # For municipality
35
+ Ibge::Reader::Municipality.read #=> [{:state_code=>"11", :meso_region_code=>"01", :micro_region_code=>"001", :code=>"00205", :full_code=>"1100205", :name=>"Porto Velho"},
36
+ # ...,
37
+ # {:state_code=>"53", :meso_region_code=>"01", :micro_region_code=>"001", :code=>"00108", :full_code=>"5300108", :name=>"Brasília"}]
38
+ ```
39
+
40
+ ## Maintainer
41
+
42
+ - Bruno Arueira
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/[my-github-username]/ibge/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
Binary file
data/ibge.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ibge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.authors = ["Bruno Arueira"]
8
+ spec.email = ["contato@brunoarueira.com"]
9
+ spec.description = %q{Unofficial gem with a bunch of data collected by ibge}
10
+ spec.summary = %q{Unofficial gem with a bunch of data collected by ibge}
11
+ spec.homepage = "http://github.com/brunoarueira/ibge"
12
+
13
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ spec.name = "ibge"
17
+ spec.version = Ibge::VERSION
18
+ spec.require_paths = ["lib"]
19
+ spec.license = "MIT"
20
+
21
+ spec.add_dependency "spreadsheet", "~> 1.0"
22
+
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "3.2.0"
25
+ end
@@ -0,0 +1,78 @@
1
+ require 'spreadsheet'
2
+
3
+ module Ibge
4
+ module Reader
5
+ class Base
6
+ class << self
7
+ attr_reader :defined_columns
8
+
9
+ def columns(columns = {})
10
+ @defined_columns = columns
11
+ end
12
+ end
13
+
14
+ attr_reader :filename
15
+
16
+ def self.read(options = {})
17
+ new(options).read
18
+ end
19
+
20
+ def initialize(options = {})
21
+ @filename = options.fetch(:filename, default_filename)
22
+ end
23
+
24
+ def read
25
+ transformed_rows = rows[1..-1].map do |row|
26
+ transform(row.compact.values_at(*column_range), column_range)
27
+ end
28
+
29
+ transformed_rows.uniq.sort_by { |hash| data_sort(hash) }
30
+ end
31
+
32
+ protected
33
+
34
+ def default_filename
35
+ File.join DATA_PATH, 'DTB_2014_subdistrito.xls'
36
+ end
37
+
38
+ def spreadsheet
39
+ @spreadsheet ||= Spreadsheet.open filename
40
+ end
41
+
42
+ def sheet
43
+ @selected_sheet ||= spreadsheet.worksheets.first
44
+ end
45
+
46
+ def rows
47
+ @rows ||= sheet.rows
48
+ end
49
+
50
+ def column_keys
51
+ self.class.defined_columns.keys.map(&:to_s).map(&:to_i)
52
+ end
53
+
54
+ def column_range
55
+ column_keys.compact.sort.uniq
56
+ end
57
+
58
+ def transform(row, column_range)
59
+ row.each_with_index.inject({}) do |item, (raw_data, index)|
60
+ column_index = column_range[index]
61
+
62
+ item.merge!(self.class.defined_columns[column_index.to_s.to_sym] => raw_data)
63
+ end
64
+ end
65
+
66
+ def data_sort(hash)
67
+ keys = hash.keys
68
+ order_values = []
69
+
70
+ (keys.length - 1).times.each do |index|
71
+ order_values << hash[keys[index]]
72
+ end
73
+
74
+ order_values
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,13 @@
1
+ module Ibge
2
+ module Reader
3
+ class District < Base
4
+ columns "0": :state_code,
5
+ "2": :meso_region_code,
6
+ "4": :micro_region_code,
7
+ "6": :municipality_code,
8
+ "9": :code,
9
+ "10": :full_code,
10
+ "11": :name
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Ibge
2
+ module Reader
3
+ class MesoRegion < Base
4
+ columns "0": :state_code,
5
+ "2": :code,
6
+ "3": :name
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Ibge
2
+ module Reader
3
+ class MicroRegion < Base
4
+ columns "0": :state_code,
5
+ "2": :meso_region_code,
6
+ "4": :code,
7
+ "5": :name
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Ibge
2
+ module Reader
3
+ class Municipality < Base
4
+ columns "0": :state_code,
5
+ "2": :meso_region_code,
6
+ "4": :micro_region_code,
7
+ "6": :code,
8
+ "7": :full_code,
9
+ "8": :name
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Ibge
2
+ module Reader
3
+ class State < Base
4
+ columns "0": :code,
5
+ "1": :state_name
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module Ibge
2
+ module Reader
3
+ class SubDistrict < Base
4
+ columns "0": :state_code,
5
+ "2": :meso_region_code,
6
+ "4": :micro_region_code,
7
+ "6": :municipality_code,
8
+ "9": :district_code,
9
+ "12": :code,
10
+ "13": :full_code,
11
+ "14": :name
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Ibge
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ibge.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'spreadsheet'
2
+
3
+ require 'ibge/version'
4
+
5
+ module Ibge
6
+ DATA_PATH = File.expand_path(File.join(__dir__, '..', 'data'))
7
+
8
+ module Reader
9
+ autoload :Base, 'ibge/reader/base'
10
+ autoload :State, 'ibge/reader/state'
11
+ autoload :MesoRegion, 'ibge/reader/meso_region'
12
+ autoload :MicroRegion, 'ibge/reader/micro_region'
13
+ autoload :Municipality, 'ibge/reader/municipality'
14
+ autoload :District, 'ibge/reader/district'
15
+ autoload :SubDistrict, 'ibge/reader/sub_district'
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::Base do
4
+ class Reader < Ibge::Reader::Base
5
+ columns "0": :code
6
+ end
7
+
8
+ subject { Reader }
9
+
10
+ describe '.columns' do
11
+ it { should respond_to :columns }
12
+
13
+ it 'maps the column index to a recognized column name' do
14
+ expect(subject.defined_columns).to eq "0": :code
15
+ end
16
+ end
17
+
18
+ describe '.read' do
19
+ context 'without pass a :filename as option' do
20
+ it 'reads the file inside data folder' do
21
+ expect(subject.read).to include code: "11"
22
+ end
23
+ end
24
+
25
+ context 'passing a :filename' do
26
+ it 'reads the file provided' do
27
+ filename = "spec/fixtures/DTB_example_subdistrito.xls"
28
+
29
+ expect(subject.read(filename: filename)).to include code: "11"
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#filename' do
35
+ it 'sets the default to file inside data folder' do
36
+ expect(subject.new.filename).to include 'data/DTB_2014_subdistrito.xls'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::District do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :state_code,
8
+ "2": :meso_region_code,
9
+ "4": :micro_region_code,
10
+ "6": :municipality_code,
11
+ "9": :code,
12
+ "10": :full_code,
13
+ "11": :name
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::MesoRegion do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :state_code,
8
+ "2": :code,
9
+ "3": :name
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::MicroRegion do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :state_code,
8
+ "2": :meso_region_code,
9
+ "4": :code,
10
+ "5": :name
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::Municipality do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :state_code,
8
+ "2": :meso_region_code,
9
+ "4": :micro_region_code,
10
+ "6": :code,
11
+ "7": :full_code,
12
+ "8": :name
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::State do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :code, "1": :state_name
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibge::Reader::SubDistrict do
4
+ subject { described_class }
5
+
6
+ it 'should have defined columns' do
7
+ expect(subject.defined_columns).to include "0": :state_code,
8
+ "2": :meso_region_code,
9
+ "4": :micro_region_code,
10
+ "6": :municipality_code,
11
+ "9": :district_code,
12
+ "12": :code,
13
+ "13": :full_code,
14
+ "14": :name
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'ibge'
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ibge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Arueira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spreadsheet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
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
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ description: Unofficial gem with a bunch of data collected by ibge
56
+ email:
57
+ - contato@brunoarueira.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - data/DTB_2014_subdistrito.xls
71
+ - ibge.gemspec
72
+ - lib/ibge.rb
73
+ - lib/ibge/reader/base.rb
74
+ - lib/ibge/reader/district.rb
75
+ - lib/ibge/reader/meso_region.rb
76
+ - lib/ibge/reader/micro_region.rb
77
+ - lib/ibge/reader/municipality.rb
78
+ - lib/ibge/reader/state.rb
79
+ - lib/ibge/reader/sub_district.rb
80
+ - lib/ibge/version.rb
81
+ - spec/fixtures/DTB_example_subdistrito.xls
82
+ - spec/ibge/reader/base_spec.rb
83
+ - spec/ibge/reader/district_spec.rb
84
+ - spec/ibge/reader/meso_region_spec.rb
85
+ - spec/ibge/reader/micro_region_spec.rb
86
+ - spec/ibge/reader/municipality_spec.rb
87
+ - spec/ibge/reader/state_spec.rb
88
+ - spec/ibge/reader/sub_district_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: http://github.com/brunoarueira/ibge
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Unofficial gem with a bunch of data collected by ibge
114
+ test_files:
115
+ - spec/fixtures/DTB_example_subdistrito.xls
116
+ - spec/ibge/reader/base_spec.rb
117
+ - spec/ibge/reader/district_spec.rb
118
+ - spec/ibge/reader/meso_region_spec.rb
119
+ - spec/ibge/reader/micro_region_spec.rb
120
+ - spec/ibge/reader/municipality_spec.rb
121
+ - spec/ibge/reader/state_spec.rb
122
+ - spec/ibge/reader/sub_district_spec.rb
123
+ - spec/spec_helper.rb