Class: IpToCountry::Geoip

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ip_to_country/geoip.rb

Class Method Summary collapse

Class Method Details

.by_ip(ip) ⇒ Object

Class methods ###



21
22
23
24
# File 'app/models/ip_to_country/geoip.rb', line 21

def self.by_ip(ip)
  where("ip_from = (select max(ip_from) from geoips where ip_from <= ?) and ip_to = (select min(ip_to)
    from geoips where ip_to >= ?)", ip_to_integer(ip), ip_to_integer(ip)).first
end

.ip_to_integer(ip) ⇒ Object



26
27
28
29
# File 'app/models/ip_to_country/geoip.rb', line 26

def self.ip_to_integer(ip)
  (o1, o2, o3, o4) = ip.try(:split, '.')
  (16777216 * o1.to_i) + (65536 * o2.to_i) + (256 * o3.to_i) + o4.to_i
end