geoip-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in geoip-rails.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,122 @@
1
+ Implements efficient MaxMind GeoIP database queries in Rails. Read more about the techinque at [this fantastic article](http://jcole.us/blog/archives/2007/11/24/on-efficiently-geo-referencing-ips-with-maxmind-geoip-and-mysql-gis).
2
+
3
+ ## Install
4
+
5
+ Add gem dependency to your Gemfile:
6
+
7
+ gem 'geoip-rails'
8
+
9
+ ## Usage
10
+
11
+ In your Rails app, `Geoip::Location` model is available. To get the location information for the user IP-address, call find_by_ip on it:
12
+
13
+ Geoip::Location.find_by_ip('123.123.123.123')
14
+ # => #<Geoip::Location id: 33447, country: "EE", region: "18", city: "Tartu", postal_code: 0, latitude: 58.3661, longitude: 26.7361, metro_code: 0, area_code: 0>
15
+
16
+
17
+ ## TODO
18
+
19
+ See [GitHub issues list](https://github.com/priithaamer/geoip-rails/issues).
20
+
21
+ ## Database schema
22
+
23
+ CREATE DATABASE geoip;
24
+
25
+ CREATE TABLE blocks (
26
+ ip_poly POLYGON NOT NULL,
27
+ ip_from INT UNSIGNED NOT NULL,
28
+ ip_to INT UNSIGNED NOT NULL,
29
+ location_id INT(11) NOT NULL,
30
+ SPATIAL INDEX (ip_poly)
31
+ ) ENGINE = MyISAM DEFAULT CHARSET = utf8;
32
+
33
+ CREATE TABLE locations (
34
+ id INT(10) UNSIGNED NOT NULL,
35
+ country CHAR(2) NOT NULL,
36
+ region CHAR(2) NOT NULL,
37
+ city VARCHAR(50) NOT NULL,
38
+ postal_code INT(10) UNSIGNED DEFAULT NULL,
39
+ latitude FLOAT DEFAULT NULL,
40
+ longitude FLOAT DEFAULT NULL,
41
+ metro_code INT(10) UNSIGNED DEFAULT NULL,
42
+ area_code INT(10) UNSIGNED DEFAULT NULL,
43
+ PRIMARY KEY (id)
44
+ ) ENGINE = MyISAM DEFAULT CHARSET = utf8;
45
+
46
+ ## Import database from CSV files
47
+
48
+ USE geoip;
49
+
50
+ CREATE TABLE blocks_new (
51
+ ip_poly POLYGON NOT NULL,
52
+ ip_from INT UNSIGNED NOT NULL,
53
+ ip_to INT UNSIGNED NOT NULL,
54
+ location_id INT(11) NOT NULL,
55
+ SPATIAL INDEX (ip_poly)
56
+ ) ENGINE = MyISAM DEFAULT CHARSET = utf8;
57
+
58
+ CREATE TABLE locations_new (
59
+ id INT(10) UNSIGNED NOT NULL,
60
+ country CHAR(2) NOT NULL,
61
+ region CHAR(2) NOT NULL,
62
+ city VARCHAR(50) NOT NULL,
63
+ postal_code INT(10) UNSIGNED DEFAULT NULL,
64
+ latitude FLOAT DEFAULT NULL,
65
+ longitude FLOAT DEFAULT NULL,
66
+ metro_code INT(10) UNSIGNED DEFAULT NULL,
67
+ area_code INT(10) UNSIGNED DEFAULT NULL,
68
+ PRIMARY KEY (id)
69
+ ) ENGINE = MyISAM DEFAULT CHARSET = utf8;
70
+
71
+ LOAD DATA LOCAL INFILE "/tmp/GeoLiteCity-Blocks.csv"
72
+ INTO TABLE blocks_new
73
+ FIELDS
74
+ TERMINATED BY ","
75
+ ENCLOSED BY '\"'
76
+ IGNORE 2 LINES
77
+ (
78
+ @start, @end, @location_id
79
+ )
80
+ SET
81
+ ip_from := @start,
82
+ ip_to := @end,
83
+ ip_poly := GEOMFROMWKB(POLYGON(LINESTRING(
84
+ POINT(@start, -1),
85
+ POINT(@end, -1),
86
+ POINT(@end, 1),
87
+ POINT(@start, 1),
88
+ POINT(@start, -1)
89
+ ))),
90
+ location_id := @location_id
91
+ ;
92
+
93
+ LOAD DATA LOCAL INFILE "/tmp/GeoLiteCity-Location.csv"
94
+ INTO TABLE locations_new
95
+ FIELDS
96
+ TERMINATED BY ","
97
+ ENCLOSED BY '\"'
98
+ IGNORE 2 LINES
99
+ (
100
+ @id, @country, @region, @city, @postal_code, @latitude, @longitude, @metro_code, @area_code
101
+ )
102
+ SET
103
+ id := @id,
104
+ country := @country,
105
+ region := @region,
106
+ city := @city,
107
+ postal_code := @postal_code,
108
+ latitude := @latitude,
109
+ longitude := @longitude,
110
+ metro_code := @metro_code,
111
+ area_code := @area_code
112
+ ;
113
+
114
+ RENAME TABLE blocks TO blocks_old;
115
+ RENAME TABLE locations TO locations_old;
116
+ RENAME TABLE blocks_new TO blocks;
117
+ RENAME TABLE locations_new TO locations;
118
+
119
+ DROP TABLE blocks_old;
120
+ DROP TABLE locations_old;
121
+
122
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,9 @@
1
+ module Geoip
2
+ class Base < ActiveRecord::Base
3
+
4
+ self.abstract_class = true
5
+
6
+ establish_connection(ActiveRecord::Base.configurations["geoip_#{::Rails.env}"])
7
+
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module Geoip
2
+ class Block < Geoip::Base
3
+
4
+ belongs_to :location
5
+
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Geoip
2
+ class Location < Geoip::Base
3
+
4
+ has_many :blocks
5
+
6
+ def self.find_by_ip(ip)
7
+ self.joins(:blocks).where('MBRCONTAINS(ip_poly, POINTFROMWKB(POINT(INET_ATON(?), 0)))', ip).first
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'geoip-rails/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'geoip-rails'
7
+ s.version = Geoip::Rails::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Priit Haamer']
10
+ s.email = ['priit@fraktal.ee']
11
+ s.homepage = 'https://github.com/priithaamer/geoip-rails'
12
+ s.summary = %q{MySQL database backed interface to MaxMind's GeoLite City GeoIP information}
13
+ s.description = %q{Provides ultra-fast access to GeoIP database through Rails models}
14
+
15
+ s.rubyforge_project = 'geoip-rails'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+ end
@@ -0,0 +1 @@
1
+ require 'geoip-rails/engine' if defined?(::Rails) && ::Rails::VERSION::MAJOR >= 3
@@ -0,0 +1,6 @@
1
+ module Geoip
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Geoip
2
+ module Rails
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ namespace :geoip do
2
+
3
+ desc 'Download GeoLiteCity database from MaxMind'
4
+ task :download do
5
+ # wget -O /tmp/GeoLiteCity.zip http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/GeoLiteCity_20110301.zip
6
+ # unzip /tmp/GeoLiteCity.zip
7
+ # mv /tmp/GeoLiteCity_20110301/*.* /tmp
8
+ end
9
+
10
+ desc 'Update database'
11
+ task :updatedb do
12
+ puts 'Update database stub'
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geoip-rails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Priit Haamer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-20 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Provides ultra-fast access to GeoIP database through Rails models
23
+ email:
24
+ - priit@fraktal.ee
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.markdown
35
+ - Rakefile
36
+ - app/models/geoip/base.rb
37
+ - app/models/geoip/block.rb
38
+ - app/models/geoip/location.rb
39
+ - geoip-rails.gemspec
40
+ - lib/geoip-rails.rb
41
+ - lib/geoip-rails/engine.rb
42
+ - lib/geoip-rails/version.rb
43
+ - lib/tasks/geoip-rails.rake
44
+ has_rdoc: true
45
+ homepage: https://github.com/priithaamer/geoip-rails
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: geoip-rails
74
+ rubygems_version: 1.4.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: MySQL database backed interface to MaxMind's GeoLite City GeoIP information
78
+ test_files: []
79
+