Sha256: 21edf6400698c72c8ee04caedfedb06ca4b823142d7f26d8228ef1e0d7b8fd63

Contents?: true

Size: 636 Bytes

Versions: 15

Compression:

Stored size: 636 Bytes

Contents

class IpCountry < ActiveRecord::Base

  @@cache = {}

  def self.country_from_ip(ip)
    if cached = @@cache[ip]
      return cached
    end
  
    if @@cache.size > 1000
      i = 100
      @@cache.each do |k,v| 
        @@cache.delete(k)
        i += 1
        break if i > 100
      end
    end

    lookup = IpCountry.select("name, risk, inet_aton('#{ip}') as ipd, ip_from, country_code").where("ip_to >= inet_aton('#{ip}')").first
    if lookup
      if lookup.ipd <= lookup.ip_from
        lookup = IpCountry.new(:risk=>50, :name=>"Unknown") 
      else
        @@cache[ip] = lookup
      end
    end

    return lookup
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
kit_cms-2.3.22 app/models/ip_country.rb
kit_cms-2.3.20 app/models/ip_country.rb
kit_cms-2.3.19 app/models/ip_country.rb
kit_cms-2.3.18 app/models/ip_country.rb
kit_cms-2.3.17 app/models/ip_country.rb
kit_cms-2.3.16 app/models/ip_country.rb
kit_cms-2.3.15 app/models/ip_country.rb
kit_cms-2.3.14 app/models/ip_country.rb
kit_cms-2.3.13 app/models/ip_country.rb
kit_cms-2.3.11 app/models/ip_country.rb
kit_cms-2.3.10 app/models/ip_country.rb
kit_cms-2.3.9 app/models/ip_country.rb
kit_cms-2.3.8 app/models/ip_country.rb
kit_cms-2.3.7 app/models/ip_country.rb
kit_cms-2.3.6 app/models/ip_country.rb