summaryrefslogtreecommitdiff
path: root/app/models/geo_ip_country.rb
blob: add95d37a2a24332087c2ec58e4bbce3dbff083f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class GeoIpCountry < ActiveRecord::Base
  attr_accessible :country_code, :country_id, :country_name, :from, :n_from, :n_to, :to

  def self.ip_to_i(ip_address)
    octets = ip_address.split('.')
    return (octets[0].to_i * 2**24) + (octets[1].to_i * 2**16) + (octets[2].to_i * 2**8) + octets[3].to_i
  end

  def self.find_by_ip(ip_address)
    GeoIpCountry.where(GeoIpCountry.ip_to_i(ip_address).to_s + ' BETWEEN n_from AND n_to').first
  end

  def to_s
    self.country_name
  end
end