Class: Antispam::Iplocator

Inherits:
ApplicationRecord show all
Defined in:
app/models/antispam/iplocator.rb

Class Method Summary collapse

Class Method Details

.countries_suspected_of_spamObject



32
33
34
# File 'app/models/antispam/iplocator.rb', line 32

def self.countries_suspected_of_spam
  %w[CN IN RU BR ID PH TH VN SG NG UA PK BD EG TR ZA MX MA KE]
end

.get_country(ip_integer) ⇒ Object



4
5
6
7
8
# File 'app/models/antispam/iplocator.rb', line 4

def self.get_country(ip_integer)
  ip = Iplocator.find_by("? > ip_from AND ? < ip_to",ip_integer,ip_integer)
  return nil if ip.nil?
  ip.country_code
end

.importObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/antispam/iplocator.rb', line 10

def self.import
  require 'csv'
  file_path = File.expand_path('../../../../ip-to-country.csv', __FILE__)
  csv_data = CSV.read(file_path, headers: false)
  csv_data.each do |row|
    begin
      Iplocator.create(
        ip_from: IPAddr.new(row[0]).to_i,
        ip_to: IPAddr.new(row[1]).to_i,
        country_code: row[2]
      )
    rescue
      puts "Error importing row: #{row.inspect}"
    end
  end
  puts "Imported #{csv_data.length} rows."
end

.ip_to_string(ip) ⇒ Object



28
29
30
# File 'app/models/antispam/iplocator.rb', line 28

def self.ip_to_string(ip)
  ip.split(".").map{|x|x.to_i.to_s(16)}.join("_")
end

.trusted_countriesObject



35
36
37
# File 'app/models/antispam/iplocator.rb', line 35

def self.trusted_countries
  %w[US DE GB CA AU JP FR NL]
end