Replace the country table with a static XML countries file
Storing the countries in the database is overkill, so just use a copy of the geonames countryInfo data that is loaded into a hash the first time it is used.
This commit is contained in:
parent
933b091330
commit
42b329ed82
6 changed files with 4798 additions and 66 deletions
38
lib/country.rb
Normal file
38
lib/country.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class Country
|
||||
attr_reader :code, :min_lat, :max_lat, :min_lon, :max_lon
|
||||
|
||||
def initialize(code, min_lat, max_lat, min_lon, max_lon)
|
||||
@code = code
|
||||
@min_lat = min_lat
|
||||
@max_lat = max_lat
|
||||
@min_lon = min_lon
|
||||
@max_lon = max_lon
|
||||
end
|
||||
|
||||
def self.find_by_code(code)
|
||||
countries[code]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.countries
|
||||
@@countries ||= load_countries
|
||||
end
|
||||
|
||||
def self.load_countries
|
||||
countries = Hash.new
|
||||
xml = REXML::Document.new(File.read("config/countries.xml"))
|
||||
|
||||
xml.elements.each("geonames/country") do |ele|
|
||||
code = ele.get_text("countryCode").to_s
|
||||
minlon = ele.get_text("west").to_s
|
||||
minlat = ele.get_text("south").to_s
|
||||
maxlon = ele.get_text("east").to_s
|
||||
maxlat = ele.get_text("north").to_s
|
||||
|
||||
countries[code] = Country.new(code, minlat.to_f, maxlat.to_f, minlon.to_f, maxlon.to_f)
|
||||
end
|
||||
|
||||
countries
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue