From 136651e0795a6e8523af1102e20cbdd76c59d7a1 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Fri, 31 May 2013 14:33:30 +0200 Subject: geo_ip_country model added --- app/models/geo_ip_country.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/models/geo_ip_country.rb (limited to 'app/models') diff --git a/app/models/geo_ip_country.rb b/app/models/geo_ip_country.rb new file mode 100644 index 0000000..add95d3 --- /dev/null +++ b/app/models/geo_ip_country.rb @@ -0,0 +1,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 -- cgit v1.2.3