br_railties 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1387716231d6d5e589720bf3a1169a73edd385b292f52c6690a81bf6e605dd1
4
- data.tar.gz: db197dc1a471b122c68f778273d6187c0e40790e7489cd6e684048eadde0b59e
3
+ metadata.gz: 1c0d2d5bfb3a9703e2dde3709a1a827700e1dcd67c349bc3c35437d43c62d390
4
+ data.tar.gz: aa2636502b775df9af1d1bf9ff4cd00584f1c6f4cbe1cd145a75d94d9918f0ad
5
5
  SHA512:
6
- metadata.gz: 2bd568cbd3edec5084025b0b4a9e0a26deb7e2584cb78d5d7a6062ef57344135d0d6bb1ef56e245afd91b0228afef4cd165cc2db2d091bf81d6ee0e778e0a7a5
7
- data.tar.gz: e8f99d523080795006a28fb9ccbe95fd35c56f7aac423c840bbb632dfdbda3e4f13034d9b66797d276e00b742b169c6eb9ca2dd043012339f1d4a233228dcdec
6
+ metadata.gz: d7bb87fa4da7a55bcb5ba24094ff5534e14a0a2866d2c9615247af515bc14ad59c00277902cdf3fc3e5e430350e26d4720fcc14e71f2d205c18b04486c45b973
7
+ data.tar.gz: 376312801c0a5044991c19e4cd66e3277b8537d7d300b7f5c2d53c7eafff4e9b989b31c1b8291d569f7c7622d771f2ee60ed59a0d54002beb110e72436060c4d
@@ -4,6 +4,7 @@ module BrRailties
4
4
  class FederalUnit < ActiveRecord::Base
5
5
  validates :acronym, presence: true, uniqueness: { case_sensitive: false }, length: { in: 2..2 }
6
6
  validates :name, presence: true, uniqueness: { case_sensitive: false }
7
+ validates :ibge_code, presence: true, uniqueness: true, numericality: { greater_than: 0 }
7
8
 
8
9
  def acronym=(value)
9
10
  self[:acronym] = value.to_s.upcase
@@ -6,5 +6,6 @@ module BrRailties
6
6
 
7
7
  validates :federal_unit, presence: true
8
8
  validates :name, presence: true, uniqueness: { case_sensitive: false, scope: [:federal_unit] }
9
+ validates :ibge_code, presence: true, uniqueness: true, numericality: { greater_than: 0 }
9
10
  end
10
11
  end
@@ -5,10 +5,8 @@ module BrRailties
5
5
  isolate_namespace BrRailties
6
6
 
7
7
  initializer :append_migrations do |app|
8
- unless app.root.to_s.match root.to_s
9
- config.paths['db/migrate'].expanded.each do |expanded_path|
10
- app.config.paths['db/migrate'] << expanded_path
11
- end
8
+ config.paths['db/migrate'].expanded.each do |expanded_path|
9
+ app.config.paths['db/migrate'] << expanded_path
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module BrRailties
6
+ module Ibge
7
+ module Import
8
+ class FederalUnits
9
+ SOURCE_URL = 'https://servicodados.ibge.gov.br/api/v1/localidades/estados'
10
+
11
+ def initialize
12
+ run
13
+ end
14
+
15
+ private
16
+
17
+ def run
18
+ source_data.each do |fu_source_data|
19
+ import(fu_source_data.except('regiao'))
20
+ end
21
+ end
22
+
23
+ def source_data
24
+ JSON.parse(Net::HTTP.get(URI.parse(SOURCE_URL)))
25
+ end
26
+
27
+ def import(raw)
28
+ data = { acronym: raw['sigla'], name: raw['nome'], ibge_code: raw['id'] }
29
+ record = record_by_data(data)
30
+ record.attributes = data
31
+ ::Rails.logger.info("Importing: #{record.attributes}")
32
+ record.save!
33
+ end
34
+
35
+ def record_by_data(data)
36
+ ::BrRailties::FederalUnit.find_by(ibge_code: data[:ibge_code]) ||
37
+ ::BrRailties::FederalUnit.find_by(acronym: data[:acronym]) ||
38
+ ::BrRailties::FederalUnit.new
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module BrRailties
6
+ module Ibge
7
+ module Import
8
+ class Municipalities
9
+ SOURCE_URL = 'https://servicodados.ibge.gov.br/api/v1/localidades/municipios'
10
+
11
+ def initialize
12
+ run
13
+ end
14
+
15
+ private
16
+
17
+ def run
18
+ source_data.each do |municipality_source_data|
19
+ import(municipality_source_data)
20
+ end
21
+ end
22
+
23
+ def source_data
24
+ JSON.parse(Net::HTTP.get(URI.parse(SOURCE_URL)))
25
+ end
26
+
27
+ def import(raw)
28
+ data = { federal_unit: find_federal_unit(raw), name: raw['nome'], ibge_code: raw['id'] }
29
+ record = record_by_data(data)
30
+ record.attributes = data
31
+ ::Rails.logger.info("Importing: #{record.attributes}")
32
+ record.save!
33
+ end
34
+
35
+ def find_federal_unit(raw)
36
+ raw = raw['microrregiao']['mesorregiao']['UF']
37
+ ::BrRailties::FederalUnit.find_by(ibge_code: raw['id']) ||
38
+ ::BrRailties::FederalUnit.find_by(acronym: raw['sigla']) ||
39
+ raise("Federal unit not found (Raw data: #{raw})")
40
+ end
41
+
42
+ def record_by_data(data)
43
+ ::BrRailties::Municipality.find_by(ibge_code: data[:ibge_code]) ||
44
+ ::BrRailties::Municipality.find_by(name: data[:name],
45
+ federal_unit: data[:federal_unit]) ||
46
+ ::BrRailties::Municipality.new
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrRailties
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/br_railties.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_scaffold'
4
+
3
5
  require 'br_railties/engine'
6
+ require 'br_railties/ibge/import/federal_units'
7
+ require 'br_railties/ibge/import/municipalities'
4
8
 
5
9
  module BrRailties
6
10
  def self.table_name_prefix
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :br_railties do
4
+ namespace :ibge do
5
+ namespace :import do
6
+ desc 'Importa unidades federativas do cadastro do IBGE.'
7
+ task federal_units: :environment do |_t, _args|
8
+ ::BrRailties::Ibge::Import::FederalUnits.new
9
+ end
10
+
11
+ desc 'Importa municípios do cadastro do IBGE.'
12
+ task municipalities: :environment do |_t, _args|
13
+ ::BrRailties::Ibge::Import::Municipalities.new
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: br_railties
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-26 00:00:00.000000000 Z
11
+ date: 2019-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_scaffold
@@ -30,14 +30,20 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 4.0.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: 5.0.0
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
41
  - - ">="
39
42
  - !ruby/object:Gem::Version
40
- version: '0'
43
+ version: 4.0.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: 5.0.0
41
47
  description:
42
48
  email:
43
49
  executables: []
@@ -56,7 +62,10 @@ files:
56
62
  - config/routes.rb
57
63
  - lib/br_railties.rb
58
64
  - lib/br_railties/engine.rb
65
+ - lib/br_railties/ibge/import/federal_units.rb
66
+ - lib/br_railties/ibge/import/municipalities.rb
59
67
  - lib/br_railties/version.rb
68
+ - lib/tasks/br_railties.rake
60
69
  homepage: https://github.com/esquilo-azul/br_railties
61
70
  licenses:
62
71
  - MIT