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