rack-geoipcountry 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 rack-geoipcountry.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,33 @@
1
+ Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address
2
+
3
+ The database can be downloaded from:
4
+
5
+ http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
6
+
7
+ Usage
8
+ =====
9
+
10
+ use Rack::GeoIPCountry, :db => "path/to/GeoIP.dat"
11
+
12
+ By default all requests are looked up and the X_GEOIP_* headers are added to the request
13
+ The headers can then be read in the application. The country name is added to the
14
+ request header as X_GEOIP_COUNTRY, eg: X_GEOIP_COUNTRY: United Kingdom
15
+
16
+ The full set of GEOIP request headers is below:
17
+
18
+ * X_GEOIP_COUNTRY_ID - The GeoIP country-ID as an integer, if not found set to 0
19
+ * X_GEOIP_COUNTRY_CODE - The ISO3166-1 two-character country code, if not found set to --
20
+ * X_GEOIP_COUNTRY_CODE3 - The ISO3166-2 three-character country code, if not found set to --
21
+ * X_GEOIP_COUNTRY - The ISO3166 English-language name of the country, if not found set to N/A
22
+ * X_GEOIP_CONTINENT - The two-character continent code, if not found set to --
23
+
24
+ You can use the included Mapping class to trigger lookup only for certain requests by specifying matching path prefix in options, eg:
25
+
26
+ use Rack::GeoIPCountry::Mapping, :prefix => '/video_tracking'
27
+
28
+ The above will lookup IP addresses only for requests matching /video_tracking etc.
29
+
30
+ License
31
+ =======
32
+
33
+ MIT License - Karol Hosiawa ( http://twitter.com/hosiawak )
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
1
+ require 'rack/geoipcountry'
@@ -0,0 +1,61 @@
1
+ require 'geoip'
2
+
3
+ module Rack
4
+ # Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address
5
+ # The database can be downloaded from:
6
+ # http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
7
+ #
8
+ # Usage:
9
+ # use Rack::GeoIPCountry, :db => "path/to/GeoIP.dat"
10
+ #
11
+ # By default all requests are looked up and the X_GEOIP_* headers are added to the request
12
+ # The headers can then be read in the application
13
+ # The country name is added to the request header as X_GEOIP_COUNTRY, eg:
14
+ # X_GEOIP_COUNTRY: United Kingdom
15
+ #
16
+ # The full set of GEOIP request headers is below:
17
+ # X_GEOIP_COUNTRY_ID - The GeoIP country-ID as an integer, if not found set to 0
18
+ # X_GEOIP_COUNTRY_CODE - The ISO3166-1 two-character country code, if not found set to --
19
+ # X_GEOIP_COUNTRY_CODE3 - The ISO3166-2 three-character country code, if not found set to --
20
+ # X_GEOIP_COUNTRY - The ISO3166 English-language name of the country, if not found set to N/A
21
+ # X_GEOIP_CONTINENT - The two-character continent code, if not found set to --
22
+ #
23
+ #
24
+ # You can use the included Mapping class to trigger lookup only for certain requests by specifying matching path prefix in options, eg:
25
+ # use Rack::GeoIPCountry::Mapping, :prefix => '/video_tracking'
26
+ # The above will lookup IP addresses only for requests matching /video_tracking etc.
27
+ #
28
+ # MIT License - Karol Hosiawa ( http://twitter.com/hosiawak )
29
+ class GeoIPCountry
30
+ def initialize(app, options = {})
31
+ options[:db] ||= 'GeoIP.dat'
32
+ @db = GeoIP.new(options[:db])
33
+ @app = app
34
+ end
35
+
36
+ def call(env)
37
+ res = @db.country(env['REMOTE_ADDR'])
38
+ env['X_GEOIP_COUNTRY_ID'] = res[2]
39
+ env['X_GEOIP_COUNTRY_CODE'] = res[3]
40
+ env['X_GEOIP_COUNTRY_CODE3'] = res[4]
41
+ env['X_GEOIP_COUNTRY'] = res[5]
42
+ env['X_GEOIP_CONTINENT'] = res[6]
43
+ @app.call(env)
44
+ end
45
+
46
+ class Mapping
47
+ def initialize(app, options = {})
48
+ @app, @prefix = app, /^#{options.delete(:prefix)}/
49
+ @geoip_country = GeoIPCountry.new(app, options)
50
+ end
51
+
52
+ def call(env)
53
+ if env['PATH_INFO'] =~ @prefix
54
+ @geoip_country.call(env)
55
+ else
56
+ @app.call(env)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module GeoIPCountry
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/geoipcountry/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-geoipcountry"
7
+ s.version = Rack::GeoIPCountry::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Karol Hosiawa", "Thomas Maurer"]
10
+ s.email = ["tma@freshbit.ch"]
11
+ s.homepage = "http://coderack.org/users/hosiawak/middlewares/36-geoip-country"
12
+ s.summary = %q{Rack middleware for Geo IP country lookup}
13
+ s.description = %q{Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address}
14
+ s.license = 'MIT'
15
+
16
+ s.rubyforge_project = "rack-geoipcountry"
17
+
18
+ s.add_dependency 'geoip'
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-geoipcountry
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Karol Hosiawa
14
+ - Thomas Maurer
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-14 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: geoip
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address
37
+ email:
38
+ - tma@freshbit.ch
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.markdown
49
+ - Rakefile
50
+ - lib/rack-geoipcountry.rb
51
+ - lib/rack/geoipcountry.rb
52
+ - lib/rack/geoipcountry/version.rb
53
+ - rack-geoipcountry.gemspec
54
+ has_rdoc: true
55
+ homepage: http://coderack.org/users/hosiawak/middlewares/36-geoip-country
56
+ licenses:
57
+ - MIT
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: rack-geoipcountry
84
+ rubygems_version: 1.5.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Rack middleware for Geo IP country lookup
88
+ test_files: []
89
+