Module: IpToCountry::Loader

Defined in:
lib/ip_to_country/loader.rb

Constant Summary collapse

ARCHIVE_SOURCE =

Constants ###

'http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip'.freeze
ARCHIVE_NAME =
'GeoIPCountryCSV.zip'.freeze
EXTRACTED_IP_FILE =
'/tmp/GeoIPCountryWhois.csv'.freeze

Class Method Summary collapse

Class Method Details

.clean_tmp_filesObject



63
64
65
66
# File 'lib/ip_to_country/loader.rb', line 63

def self.clean_tmp_files
  FileUtils.rm(EXTRACTED_IP_FILE)
  FileUtils.rm("/tmp/#{ARCHIVE_NAME}")
end

.create_tempo_tableObject

Create a tempo geoips table to load ips.



33
34
35
36
37
38
39
# File 'lib/ip_to_country/loader.rb', line 33

def self.create_tempo_table
  sql = <<-__SQL__
    DROP TABLE IF EXISTS geoips_tempo;
    CREATE TABLE geoips_tempo AS SELECT * FROM geoips WHERE ip_from = -1;
  __SQL__
  ActiveRecord::Base.connection.execute(sql)
end

.download_and_unzip_ipsObject



26
27
28
29
30
# File 'lib/ip_to_country/loader.rb', line 26

def self.download_and_unzip_ips
  system("wget -N #{ARCHIVE_SOURCE}")
  FileUtils.mv(ARCHIVE_NAME, "/tmp")
  system("unzip -j /tmp/#{ARCHIVE_NAME} -d /tmp")
end

.load_csv_in_tempo_tableObject

Load IPs and countries matching from a CSV file.



42
43
44
45
46
47
48
49
50
51
# File 'lib/ip_to_country/loader.rb', line 42

def self.load_csv_in_tempo_table
  raw  = ActiveRecord::Base.connection.raw_connection
  raw.exec("COPY geoips_tempo FROM STDIN WITH(FORMAT CSV)")
  File.open(EXTRACTED_IP_FILE).each do |line|
    raw.put_copy_data line
  end
  raw.put_copy_end
  while res = raw.get_result do; end # very important to do this after a copy
  ActiveRecord::Base.connection_pool.checkin(ActiveRecord::Base.connection)
end

.populate!Object

Class methods ###



18
19
20
21
22
23
24
# File 'lib/ip_to_country/loader.rb', line 18

def self.populate!
  download_and_unzip_ips
  create_tempo_table
  load_csv_in_tempo_table
  rename_tempo_into_geoip
  clean_tmp_files
end

.rename_tempo_into_geoipObject



53
54
55
56
57
58
59
60
61
# File 'lib/ip_to_country/loader.rb', line 53

def self.rename_tempo_into_geoip
  sql = <<-__SQL__
    ALTER TABLE geoips RENAME TO tmp;
    ALTER TABLE geoips_tempo RENAME TO geoips;
    ALTER TABLE tmp RENAME TO geoips_tempo;
    DROP TABLE geoips_tempo;
  __SQL__
  ActiveRecord::Base.connection.execute(sql)
end