rack-geo-locale 0.0.8 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/fixtures/GeoIP.dat.gz +0 -0
- data/lib/rack/geo_locale.rb +18 -19
- data/spec/lib/rack/geo_locale_spec.rb +11 -1
- metadata +13 -12
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
Binary file
|
data/lib/rack/geo_locale.rb
CHANGED
@@ -6,7 +6,7 @@ module Rack
|
|
6
6
|
DATABASE = "tmp/geoip_database.dat"
|
7
7
|
|
8
8
|
def initialize(app)
|
9
|
-
fetch_database
|
9
|
+
@geoip = fetch_database
|
10
10
|
|
11
11
|
@app = app
|
12
12
|
end
|
@@ -30,24 +30,27 @@ module Rack
|
|
30
30
|
private
|
31
31
|
def parse_country(env)
|
32
32
|
if database?
|
33
|
-
if
|
34
|
-
|
33
|
+
if addr = env["REMOTE_ADDR"]
|
34
|
+
addr = env["HTTP_X_FORWARDED_FOR"] if env["HTTP_X_FORWARDED_FOR"]
|
35
|
+
addr = addr.split(",").first.strip
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
puts "INFO: Trying to lookup #{addr}"
|
38
|
+
|
39
|
+
result = @geoip.country(addr).country_code2
|
40
|
+
|
41
|
+
if result != "--"
|
42
|
+
puts "INFO: Found country for #{addr} #{result}"
|
41
43
|
|
42
|
-
|
44
|
+
result
|
45
|
+
else
|
46
|
+
puts "INFO: Didn't find country for #{addr}"
|
47
|
+
end
|
43
48
|
else
|
44
49
|
puts "WARNING: Didn't find env['REMOTE_ADDR']"
|
45
50
|
end
|
46
51
|
else
|
47
52
|
puts "WARNING: Didn't find geoip database."
|
48
53
|
end
|
49
|
-
|
50
|
-
nil
|
51
54
|
end
|
52
55
|
|
53
56
|
def parse_locale(env)
|
@@ -65,7 +68,7 @@ module Rack
|
|
65
68
|
end
|
66
69
|
|
67
70
|
def database?
|
68
|
-
|
71
|
+
@geoip != nil
|
69
72
|
end
|
70
73
|
|
71
74
|
def fetch_database
|
@@ -73,16 +76,12 @@ module Rack
|
|
73
76
|
puts "-> Fetching #{ENV["GEOIP_DATABASE_URI"]}"
|
74
77
|
|
75
78
|
`curl --silent -o #{DATABASE}.gz #{ENV["GEOIP_DATABASE_URI"]}`
|
76
|
-
`gunzip #{DATABASE}.gz`
|
77
|
-
else
|
78
|
-
puts "WARNING: Set the ENV['GEOIP_DATABASE_URI'] to the location of your .gz database file."
|
79
|
-
end
|
80
|
-
end
|
79
|
+
`gunzip -f #{DATABASE}.gz`
|
81
80
|
|
82
|
-
def geoip
|
83
|
-
if database?
|
84
81
|
GeoIP.new(DATABASE)
|
85
82
|
else
|
83
|
+
puts "WARNING: Set the ENV['GEOIP_DATABASE_URI'] to the location of your .gz database file."
|
84
|
+
|
86
85
|
nil
|
87
86
|
end
|
88
87
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rack/test'
|
2
|
-
require 'rack/geo_locale'
|
3
2
|
require 'geoip'
|
3
|
+
require 'rack/geo_locale'
|
4
4
|
|
5
5
|
include Rack::Test::Methods
|
6
6
|
|
@@ -24,11 +24,21 @@ describe Rack::GeoLocale do
|
|
24
24
|
last_request.env["locale.country"].should == nil
|
25
25
|
end
|
26
26
|
|
27
|
+
it "should handle only passing a HTTP_X_FORWARDED_FOR field" do
|
28
|
+
get '/', {}, {"HTTP_X_FORWARDED_FOR" => "10.0.0.1"}
|
29
|
+
last_request.env["locale.country"].should == "SE"
|
30
|
+
end
|
31
|
+
|
27
32
|
it "should take HTTP_X_FORWARDED_FOR before REMOTE_ADDR field" do
|
28
33
|
get '/', {}, {"REMOTE_ADDR" => "10.0.0.1", "HTTP_X_FORWARDED_FOR" => "10.0.0.2"}
|
29
34
|
last_request.env["locale.country"].should == "US"
|
30
35
|
end
|
31
36
|
|
37
|
+
it "should handle several IP adresses in the REMOTE_ADDR field" do
|
38
|
+
get '/', {}, {"REMOTE_ADDR" => "10.0.0.1, 127.0.0.1"}
|
39
|
+
last_request.env["locale.country"].should == "SE"
|
40
|
+
end
|
41
|
+
|
32
42
|
it "should resolve 10.0.0.1 to SE" do
|
33
43
|
get '/', {}, {"REMOTE_ADDR" => "10.0.0.1"}
|
34
44
|
last_request.env["locale.country"].should == "SE"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-geo-locale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70196899531500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70196899531500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rack-test
|
27
|
-
requirement: &
|
27
|
+
requirement: &70196899502360 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70196899502360
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: guard-rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70196899479180 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70196899479180
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rack
|
49
|
-
requirement: &
|
49
|
+
requirement: &70196899415180 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70196899415180
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: geoip
|
60
|
-
requirement: &
|
60
|
+
requirement: &70196899414340 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70196899414340
|
69
69
|
description: Simple Rack middleware for setting the locale.country via GeoIP using
|
70
70
|
the MaxMind GeoIP database, and setting the locale.languages based on the HTTP_ACCEPT_LANGUAGE
|
71
71
|
header.
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- LICENSE
|
82
82
|
- Readme.md
|
83
83
|
- VERSION
|
84
|
+
- fixtures/GeoIP.dat.gz
|
84
85
|
- lib/rack/geo_locale.rb
|
85
86
|
- rack-geo-locale.gemspec
|
86
87
|
- spec/lib/rack/geo_locale_spec.rb
|