Sha256: 170f929262b1a1284120b9bd0f54a13047d51e626dd563b5c055520c4df8a1dc

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

# -*- encoding : utf-8 -*-

module IpToCountry
  class Geoip < ActiveRecord::Base

    ###################
    ### Validations ###
    ###################

    validates :ip_from_string, presence: true
    validates :ip_to_string, presence: true
    validates :ip_from, presence: true, uniqueness: true, numericality: :only_integer
    validates :ip_to, presence: true, uniqueness: true, numericality: :only_integer
    validates :country_code, presence: true
    validates :country_name, presence: true

    #####################
    ### Class methods ###
    #####################

    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

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ip_to_country-0.2.0 app/models/ip_to_country/geoip.rb
ip_to_country-0.1.0 app/models/ip_to_country/geoip.rb