3 class CreateCountries < ActiveRecord::Migration
5 create_table :countries, innodb_table do |t|
6 t.column :id, :integer_pk, :null => false
7 t.column :code, :string, :limit => 2, :null => false
8 t.column :min_lat, :double, :null => false
9 t.column :max_lat, :double, :null => false
10 t.column :min_lon, :double, :null => false
11 t.column :max_lon, :double, :null => false
14 add_index :countries, [:code], :name => "countries_code_idx", :unique => true
16 Net::HTTP.start('ws.geonames.org') do |http|
17 xml = REXML::Document.new(http.get("/countryInfo").body)
19 xml.elements.each("geonames/country") do |ele|
20 code = ele.get_text("countryCode").to_s
21 minlon = ele.get_text("bBoxWest").to_s
22 minlat = ele.get_text("bBoxSouth").to_s
23 maxlon = ele.get_text("bBoxEast").to_s
24 maxlat = ele.get_text("bBoxNorth").to_s
26 Country.create(:code => code,
27 :min_lat => minlat.to_f, :max_lat => maxlat.to_f,
28 :min_lon => minlon.to_f, :max_lon => maxlon.to_f)