summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Kozak <spag@golwen.net>2013-05-31 14:33:30 +0200
committerPeter Kozak <spag@golwen.net>2013-05-31 14:33:30 +0200
commit136651e0795a6e8523af1102e20cbdd76c59d7a1 (patch)
tree204b65863b259a144147ccf3595415398a9c262b
parentf1260d478acae4a5fc34d7231530a6c6ba5ce0ba (diff)
geo_ip_country model added
-rw-r--r--app/models/geo_ip_country.rb16
-rw-r--r--db/migrate/20130531063549_create_geo_ip_countries.rb15
2 files changed, 31 insertions, 0 deletions
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
diff --git a/db/migrate/20130531063549_create_geo_ip_countries.rb b/db/migrate/20130531063549_create_geo_ip_countries.rb
new file mode 100644
index 0000000..9124756
--- /dev/null
+++ b/db/migrate/20130531063549_create_geo_ip_countries.rb
@@ -0,0 +1,15 @@
+class CreateGeoIpCountries < ActiveRecord::Migration
+ def change
+ create_table :geo_ip_countries do |t|
+ t.string :from, :limit => '15'
+ t.string :to, :limit => '15'
+ t.integer :n_from
+ t.integer :n_to
+ t.integer :country_id
+ t.string :country_code, :limit => '2'
+ t.string :country_name, :limit => '64'
+
+ t.timestamps
+ end
+ end
+end