ceps 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/README.md +2 -0
- data/Rakefile +2 -43
- data/lib/ceps/railtie.rb +16 -0
- data/lib/ceps/version.rb +1 -1
- data/lib/ceps.rb +33 -0
- data/lib/tasks/geolocalize.rake +41 -0
- data/lib/tasks/import.rake +46 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a8733b593bf45104acf53df0259b11255014a96
|
4
|
+
data.tar.gz: 79641e0bd8e6f86a3baabf597905dce309e6936d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 046101f7a4f959c219334bc345a1181478d44d97bedbc0adcbcd66b1f10a7750556a41b78ce5c6e5ed1a27c2dbbe2df90259229c7d13c34182607802495b65ca
|
7
|
+
data.tar.gz: c3efb9e8bf6255af3f72156de8e62eac21dda0a7a72248784aab7f5c574bb1cc1a64166ee5a52b7b970d273417f7cd4e5f4a30a15fa12630fa195b95fdee9994
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,8 @@ CEPs
|
|
4
4
|
Ceps is a collection of all sorts of useful information about every brazilian CEP packaged as pretty little cep objects.
|
5
5
|
|
6
6
|
[](https://travis-ci.org/theprogramer/ceps)
|
7
|
+
[](https://codeclimate.com/github/theprogramer/ceps)
|
8
|
+
[](https://codeclimate.com/github/theprogramer/ceps/coverage)
|
7
9
|
|
8
10
|
Installation
|
9
11
|
------------
|
data/Rakefile
CHANGED
@@ -4,52 +4,11 @@ require 'bundler/gem_tasks'
|
|
4
4
|
require 'rake'
|
5
5
|
require 'rspec/core/rake_task'
|
6
6
|
|
7
|
+
Dir.glob('lib/tasks/*.rake').each { |r| load r}
|
8
|
+
|
7
9
|
desc 'Run all examples'
|
8
10
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
9
11
|
t.rspec_opts = %w(--color --warnings)
|
10
12
|
end
|
11
13
|
|
12
14
|
task default: [:spec]
|
13
|
-
|
14
|
-
|
15
|
-
desc 'Import MDB file'
|
16
|
-
task :import do
|
17
|
-
|
18
|
-
require 'mdb'
|
19
|
-
require_relative 'lib/ceps/mdb'
|
20
|
-
require 'ruby-progressbar'
|
21
|
-
|
22
|
-
db = Mdb.open ARGV[1]
|
23
|
-
puts "Loading routes"
|
24
|
-
puts "This should take some time!"
|
25
|
-
routes = db["LOG_LOGRADOURO"]
|
26
|
-
puts "Loading neighborhoods"
|
27
|
-
neighborhoods = db["LOG_BAIRRO"]
|
28
|
-
puts "Loading cities"
|
29
|
-
cities = db["LOG_LOCALIDADE"]
|
30
|
-
puts "Loading states"
|
31
|
-
states = db["LOG_FAIXA_UF"]
|
32
|
-
|
33
|
-
ceps = {}
|
34
|
-
puts "Generating new hash"
|
35
|
-
progressbar = ProgressBar.create title: "Progress",
|
36
|
-
starting_at: 0,
|
37
|
-
total: routes.count,
|
38
|
-
format: "%c from %C |%b>>%i| %a (%e) %p%%"
|
39
|
-
routes.each do |route|
|
40
|
-
cep = Correios::Mdb.decript route[:LOG_KEY_DNE]
|
41
|
-
ceps[cep.to_sym] = {
|
42
|
-
location: route[:LOG_NO],
|
43
|
-
type: route[:LOG_TIPO_LOGRADOURO],
|
44
|
-
city: cities.select{|c| c[:LOC_NU_SEQUENCIAL] == route[:LOC_NU_SEQUENCIAL].to_s}.first[:LOC_NO],
|
45
|
-
state: states.select{|s| s[:UFE_SG] == route[:UFE_SG]}.first[:UFE_NO],
|
46
|
-
neighborhood: neighborhoods.select{|n| n[:BAI_NU_SEQUENCIAL] == route[:BAI_NU_SEQUENCIAL_INI].to_s}.first[:BAI_NO]
|
47
|
-
}
|
48
|
-
progressbar.increment
|
49
|
-
progressbar.refresh
|
50
|
-
end
|
51
|
-
puts "Saving new ceps.yaml"
|
52
|
-
File.open('ceps.yml','w') do |h|
|
53
|
-
h.write ceps.to_yaml
|
54
|
-
end
|
55
|
-
end
|
data/lib/ceps/railtie.rb
ADDED
data/lib/ceps/version.rb
CHANGED
data/lib/ceps.rb
CHANGED
@@ -1,7 +1,40 @@
|
|
1
1
|
require 'ceps/version'
|
2
2
|
|
3
3
|
require 'correios'
|
4
|
+
require 'ceps/railtie' if defined?(Rails)
|
4
5
|
|
5
6
|
class Cep < Correios::Cep
|
6
7
|
|
8
|
+
class << self
|
9
|
+
attr_accessor :configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
self.configuration ||= Configuration.new
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class Configuration
|
19
|
+
|
20
|
+
attr_accessor :data_file_name
|
21
|
+
|
22
|
+
attr_accessor :data_file_path
|
23
|
+
|
24
|
+
attr_accessor :data_file
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@data_file_name = 'ceps.yaml'
|
28
|
+
if defined?(Rails)
|
29
|
+
@data_file_path = File.join(Rails.root, "lib", "data")
|
30
|
+
else
|
31
|
+
@data_file_path = File.join(
|
32
|
+
File.dirname(
|
33
|
+
File.dirname(__FILE__)),
|
34
|
+
"lib", "data")
|
35
|
+
end
|
36
|
+
|
37
|
+
@data_file = File.join @data_file_path, @data_file_name
|
38
|
+
|
39
|
+
end
|
7
40
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
namespace :ceps do
|
2
|
+
|
3
|
+
desc "Geolocalize ceps.yml file"
|
4
|
+
task :geolocalize do
|
5
|
+
require 'geocoder'
|
6
|
+
require 'retryable'
|
7
|
+
require 'yaml'
|
8
|
+
require 'ceps'
|
9
|
+
|
10
|
+
# raise on geocoding errors such as query limit exceeded
|
11
|
+
Geocoder.configure(always_raise: :all)
|
12
|
+
puts "Loading routes"
|
13
|
+
puts "This should take some time!"
|
14
|
+
ceps = YAML.load_file(ARGV[1] || Cep.configure.data_file) || {}
|
15
|
+
puts "Geolocalize new hash"
|
16
|
+
ceps.each do |cep, value|
|
17
|
+
query = "#{ceps[cep][:type]} #{ceps[cep][:location]}, #{ceps[cep][:neighborhood]} - #{ceps[cep][:city]} - #{ceps[cep][:state]} - cep: #{cep}"
|
18
|
+
begin
|
19
|
+
Retryable.retryable(tries: 5, sleep: lambda { |n| 2**n }) do
|
20
|
+
geodata = Geocoder.search(query).first
|
21
|
+
if geodata.empty?
|
22
|
+
raise "Empty geodata"
|
23
|
+
else
|
24
|
+
ceps[cep][:latitude] = geodata["location"]["lat"]
|
25
|
+
ceps[cep][:longitude] = geodata["location"]["lng"]
|
26
|
+
puts geodata["location"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
rescue => e
|
30
|
+
warn "Attempts exceeded for query #{query}, last error was #{e.message}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
puts "Saving new ceps.yaml"
|
35
|
+
File.open(Cep.configure.data_file,'w') do |h|
|
36
|
+
h.write ceps.to_yaml
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
namespace :ceps do
|
2
|
+
|
3
|
+
desc 'Import MDB file'
|
4
|
+
task :import do
|
5
|
+
|
6
|
+
require 'mdb'
|
7
|
+
require 'yaml'
|
8
|
+
require_relative 'lib/ceps/mdb'
|
9
|
+
require 'ruby-progressbar'
|
10
|
+
|
11
|
+
db = Mdb.open ARGV[1]
|
12
|
+
puts "Loading routes"
|
13
|
+
puts "This should take some time!"
|
14
|
+
routes = db["LOG_LOGRADOURO"]
|
15
|
+
puts "Loading neighborhoods"
|
16
|
+
neighborhoods = db["LOG_BAIRRO"]
|
17
|
+
puts "Loading cities"
|
18
|
+
cities = db["LOG_LOCALIDADE"]
|
19
|
+
puts "Loading states"
|
20
|
+
states = db["LOG_FAIXA_UF"]
|
21
|
+
|
22
|
+
ceps = {}
|
23
|
+
puts "Generating new hash"
|
24
|
+
progressbar = ProgressBar.create title: "Progress",
|
25
|
+
starting_at: 0,
|
26
|
+
total: routes.count,
|
27
|
+
format: "%c from %C |%b>>%i| %a (%e) %p%%"
|
28
|
+
routes.each do |route|
|
29
|
+
cep = Correios::Mdb.decript route[:LOG_KEY_DNE]
|
30
|
+
ceps[cep.to_sym] = {
|
31
|
+
location: route[:LOG_NO],
|
32
|
+
type: route[:LOG_TIPO_LOGRADOURO],
|
33
|
+
city: cities.select{|c| c[:LOC_NU_SEQUENCIAL] == route[:LOC_NU_SEQUENCIAL].to_s}.first[:LOC_NO],
|
34
|
+
state: states.select{|s| s[:UFE_SG] == route[:UFE_SG]}.first[:UFE_NO],
|
35
|
+
neighborhood: neighborhoods.select{|n| n[:BAI_NU_SEQUENCIAL] == route[:BAI_NU_SEQUENCIAL_INI].to_s}.first[:BAI_NO]
|
36
|
+
}
|
37
|
+
progressbar.increment
|
38
|
+
progressbar.refresh
|
39
|
+
end
|
40
|
+
puts "Saving new ceps.yaml"
|
41
|
+
File.open(Cep.configure.data_file,'w') do |h|
|
42
|
+
h.write ceps.to_yaml
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ceps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thiago Miranda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06
|
11
|
+
date: 2015-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -42,10 +42,13 @@ files:
|
|
42
42
|
- lib/ceps.rb
|
43
43
|
- lib/ceps/cep.rb
|
44
44
|
- lib/ceps/mdb.rb
|
45
|
+
- lib/ceps/railtie.rb
|
45
46
|
- lib/ceps/setup.rb
|
46
47
|
- lib/ceps/version.rb
|
47
48
|
- lib/correios.rb
|
48
49
|
- lib/data/ceps.yaml
|
50
|
+
- lib/tasks/geolocalize.rake
|
51
|
+
- lib/tasks/import.rake
|
49
52
|
- spec/cep_spec.rb
|
50
53
|
- spec/setup_spec.rb
|
51
54
|
- spec/spec_helper.rb
|