data models for countries, regions, and cities tables.
This commit is contained in:
parent
557412992e
commit
2dd8ad0be8
|
|
@ -128,6 +128,9 @@ require "jam_ruby/models/geo_ip_locations"
|
|||
require "jam_ruby/models/score"
|
||||
require "jam_ruby/models/get_work"
|
||||
require "jam_ruby/models/playable_play"
|
||||
require "jam_ruby/models/country"
|
||||
require "jam_ruby/models/region"
|
||||
require "jam_ruby/models/city"
|
||||
|
||||
include Jampb
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
module JamRuby
|
||||
class City < ActiveRecord::Base
|
||||
|
||||
self.table_name = 'cities'
|
||||
|
||||
def self.get_all(country, region)
|
||||
self.where(countrycode: country).where(region: region).order('city asc').all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
module JamRuby
|
||||
class Country < ActiveRecord::Base
|
||||
|
||||
self.table_name = 'countries'
|
||||
|
||||
def self.get_all()
|
||||
self.order('countryname asc').all
|
||||
end
|
||||
|
||||
def self.import_from_iso3166(file)
|
||||
|
||||
# File iso3166.csv
|
||||
# Format:
|
||||
# countrycode,countryname
|
||||
|
||||
# what this does is not replace the contents of the table, but rather update the specified rows with the names.
|
||||
# any rows not specified have the countryname reset to be the same as the countrycode.
|
||||
|
||||
self.transaction do
|
||||
self.connection.execute "update #{self.table_name} set countryname = countrycode"
|
||||
|
||||
File.open(file, 'r:ISO-8859-1') do |io|
|
||||
saved_level = ActiveRecord::Base.logger ? ActiveRecord::Base.logger.level : 0
|
||||
count = 0
|
||||
|
||||
ncols = 2
|
||||
|
||||
csv = ::CSV.new(io, {encoding: 'ISO-8859-1', headers: false})
|
||||
csv.each do |row|
|
||||
raise "file does not have expected number of columns (#{ncols}): #{row.length}" unless row.length == ncols
|
||||
|
||||
countrycode = row[0]
|
||||
countryname = row[1]
|
||||
|
||||
stmt = "UPDATE #{self.table_name} SET countryname = #{MaxMindIsp.quote_value(countryname)} WHERE countrycode = #{MaxMindIsp.quote_value(countrycode)}"
|
||||
self.connection.execute stmt
|
||||
count += 1
|
||||
|
||||
if ActiveRecord::Base.logger and ActiveRecord::Base.logger.level < Logger::INFO
|
||||
ActiveRecord::Base.logger.debug "... logging updates to #{self.table_name} suspended ..."
|
||||
ActiveRecord::Base.logger.level = Logger::INFO
|
||||
end
|
||||
end
|
||||
|
||||
if ActiveRecord::Base.logger
|
||||
ActiveRecord::Base.logger.level = saved_level
|
||||
ActiveRecord::Base.logger.debug "updated #{count} records in #{self.table_name}"
|
||||
end
|
||||
end # file
|
||||
end # transaction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
module JamRuby
|
||||
class Region < ActiveRecord::Base
|
||||
|
||||
self.table_name = 'regions'
|
||||
|
||||
def self.get_all(country)
|
||||
self.where(countrycode: country).order('regionname asc').all
|
||||
end
|
||||
|
||||
def self.import_from_xx_region(countrycode, file)
|
||||
|
||||
# File xx_region.csv
|
||||
# Format:
|
||||
# region,regionname
|
||||
|
||||
# what this does is not replace the contents of the table, but rather update the specifies rows with the names.
|
||||
# any rows not specified are left alone. the parameter countrycode denote the country of the region (when uppercased)
|
||||
|
||||
raise "countrycode (#{MaxMindIsp.quote_value(countrycode)}) is missing or invalid (it must be two characters)" unless countrycode and countrycode.length == 2
|
||||
countrycode = countrycode.upcase
|
||||
|
||||
self.transaction do
|
||||
self.connection.execute "update #{self.table_name} set regionname = region where countrycode = #{MaxMindIsp.quote_value(countrycode)}"
|
||||
|
||||
File.open(file, 'r:ISO-8859-1') do |io|
|
||||
saved_level = ActiveRecord::Base.logger ? ActiveRecord::Base.logger.level : 0
|
||||
count = 0
|
||||
|
||||
ncols = 2
|
||||
|
||||
csv = ::CSV.new(io, {encoding: 'ISO-8859-1', headers: false})
|
||||
csv.each do |row|
|
||||
raise "file does not have expected number of columns (#{ncols}): #{row.length}" unless row.length == ncols
|
||||
|
||||
region = row[0]
|
||||
regionname = row[1]
|
||||
|
||||
stmt = "UPDATE #{self.table_name} SET regionname = #{MaxMindIsp.quote_value(regionname)} WHERE countrycode = #{MaxMindIsp.quote_value(countrycode)} AND region = #{MaxMindIsp.quote_value(region)}"
|
||||
self.connection.execute stmt
|
||||
count += 1
|
||||
|
||||
if ActiveRecord::Base.logger and ActiveRecord::Base.logger.level < Logger::INFO
|
||||
ActiveRecord::Base.logger.debug "... logging updates to #{self.table_name} suspended ..."
|
||||
ActiveRecord::Base.logger.level = Logger::INFO
|
||||
end
|
||||
end
|
||||
|
||||
if ActiveRecord::Base.logger
|
||||
ActiveRecord::Base.logger.level = saved_level
|
||||
ActiveRecord::Base.logger.debug "updated #{count} records in #{self.table_name}"
|
||||
end
|
||||
end # file
|
||||
end # transaction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -24,13 +24,25 @@ namespace :db do
|
|||
JamIsp.import_from_max_mind ENV['file']
|
||||
end
|
||||
|
||||
desc "Import a iso3166 country database (countrycodes and names); run like this: rake db:import_countries file=/path/to/iso3166.csv"
|
||||
task import_countries: :environment do
|
||||
Country.import_from_iso3166 ENV['file']
|
||||
end
|
||||
|
||||
desc "Import a region database (regioncode, regionname); run like this: rake db:import_regions countrycode=XX file=/path/to/xx_region.csv"
|
||||
task import_regions: :environment do
|
||||
Region.import_from_xx_region(ENV['countrycode'], ENV['file'])
|
||||
end
|
||||
|
||||
desc "Help"
|
||||
task help: :environment do
|
||||
puts "bundle exec rake db:import_maxmind_isp file=/path/to/GeoIPISP-142.csv"
|
||||
puts "bundle exec rake db:import_maxmind_geo file=/path/to/GeoIPCity.csv"
|
||||
puts "bundle exec rake db:import_geoip_blocks file=/path/to/GeoIPCity-134-Blocks.csv"
|
||||
puts "bundle exec rake db:import_geoip_locations file=/path/to/GeoIPCity-134-Location.csv"
|
||||
puts "bundle exec rake db:import_jam_isp file=/path/to/GeoIPISP.csv"
|
||||
puts "bundle exec rake db:import_maxmind_isp file=/path/to/GeoIPISP-142.csv # geo-142"
|
||||
puts "bundle exec rake db:import_maxmind_geo file=/path/to/GeoIPCity.csv # geo-139"
|
||||
puts "bundle exec rake db:import_geoip_blocks file=/path/to/GeoIPCity-134-Blocks.csv # geo-134"
|
||||
puts "bundle exec rake db:import_geoip_locations file=/path/to/GeoIPCity-134-Location.csv # geo-134"
|
||||
puts "bundle exec rake db:import_jam_isp file=/path/to/GeoIPISP.csv # geo-124"
|
||||
puts "bundle exec rake db:import_countries file=/path/to/iso3166.csv # db/geodata"
|
||||
puts "bundle exec rake db:import_regions countrycode=XX file=/path/to/xx_region.csv # db/geodata, both of them"
|
||||
end
|
||||
|
||||
desc "Create a fake set of maxmind data"
|
||||
|
|
|
|||
Loading…
Reference in New Issue