-
Notifications
You must be signed in to change notification settings - Fork 49
/
br_populate.rb
33 lines (28 loc) · 879 Bytes
/
br_populate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'net/http'
require 'net/https' # for ruby 1.8.7
require 'json'
module BRPopulate
def self.states
http = Net::HTTP.new('raw.githubusercontent.com', 443); http.use_ssl = true
JSON.parse http.get('/celsodantas/br_populate/master/states.json').body
end
def self.capital?(city, state)
city["name"] == state["capital"]
end
def self.populate
states.each do |state|
region_obj = Region.find_or_create_by(:name => state["region"])
state_obj = State.new(:acronym => state["acronym"], :name => state["name"], :region => region_obj)
state_obj.save
state["cities"].each do |city|
c = City.new
c.name = city["name"]
c.state = state_obj
c.capital = capital?(city, state)
c.save
puts "Adicionando a cidade #{c.name} ao estado #{c.state.name}"
end
end
end
end
BRPopulate.populate