geo_redirect 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/README.md +20 -1
- data/geo_redirect.gemspec +2 -2
- data/lib/geo_redirect/middleware.rb +25 -14
- data/lib/geo_redirect/version.rb +1 -1
- data/lib/geo_redirect.rb +2 -2
- data/lib/tasks/geo_redirect.rake +1 -1
- data/spec/lib/geo_redirect/middleware_spec.rb +36 -0
- data/spec/support/rake.rb +2 -1
- metadata +30 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f21f596f04cc22f8af21cd140c5e618c226a0f4
|
4
|
+
data.tar.gz: 210880d4a407c0e7263c282d5c421a1a3661affb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aaacd15e27321f27961ff90d82a6b9f7d637774d4f427aa67efe95b46a681c70ef28ae0ff06109d963e84b5335df9f551b4d89cd54954efb02bd006999d73b60
|
7
|
+
data.tar.gz: bb5742370da7819f41579dcec891d52890dc9bbe0e7fdac7ab85c8e26d4153a9a76fa59258067a502c5f8357b72c509d9aa84da6298e3e656fbbd35eff13ec8d
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,7 @@ URL, and by that forcing the server to host from the current domain (and saving
|
|
20
20
|
that domain to the user's session variable).
|
21
21
|
|
22
22
|
To skip geo-redirection completely, pass a `?skip_geo=true` argument (this would
|
23
|
-
avoid saving any session value and/or HTTP redirects
|
23
|
+
avoid saving any session value and/or HTTP redirects.
|
24
24
|
|
25
25
|
## Installation
|
26
26
|
|
@@ -127,6 +127,25 @@ your project:
|
|
127
127
|
config: 'geo_cfg.yml'
|
128
128
|
}
|
129
129
|
|
130
|
+
### Custom logic to skip the redirection
|
131
|
+
|
132
|
+
You can pass a block to the `:skip_if` option to provide custom logic to skip the redirection.
|
133
|
+
|
134
|
+
Rails.application.middleware.use GeoRedirect::Middleware, skip_if: ->(req) { req.bot? }
|
135
|
+
|
136
|
+
will ignore requests from bots (the code inside the block is just pseudo-code).
|
137
|
+
|
138
|
+
### Skipping
|
139
|
+
|
140
|
+
As mentioned, skipping Geo-redirection is done either by:
|
141
|
+
|
142
|
+
* `redirect=1` to force a new domain and remember it in the user's session variable.
|
143
|
+
* `skip_geo=true` for a one-time force (with no memory).
|
144
|
+
|
145
|
+
If the `:remember_when_skipping` option flag is set to true, the server will
|
146
|
+
always remember the current domain, even after skipping.
|
147
|
+
|
148
|
+
|
130
149
|
### Debugging
|
131
150
|
|
132
151
|
You can add a `logfile` path string when adding the middleware if you want it to
|
data/geo_redirect.gemspec
CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.add_development_dependency 'rack', '~> 1.6.0'
|
21
21
|
gem.add_development_dependency 'rack-test', '~> 0.6.3'
|
22
22
|
gem.add_development_dependency 'simplecov', '~> 0.9.1'
|
23
|
-
gem.add_development_dependency 'rubocop', '~> 0.
|
24
|
-
gem.add_development_dependency 'rubocop-rspec', '~> 1.
|
23
|
+
gem.add_development_dependency 'rubocop', '~> 0.37'
|
24
|
+
gem.add_development_dependency 'rubocop-rspec', '~> 1.4.0'
|
25
25
|
|
26
26
|
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
27
27
|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
@@ -7,6 +7,7 @@ module GeoRedirect
|
|
7
7
|
|
8
8
|
def initialize(app, options = {})
|
9
9
|
@app = app
|
10
|
+
@options = options
|
10
11
|
|
11
12
|
@logger = init_logger(options[:logfile])
|
12
13
|
@db = init_db(options[:db] || DEFAULT_DB_PATH)
|
@@ -22,6 +23,7 @@ module GeoRedirect
|
|
22
23
|
@request = Rack::Request.new(env)
|
23
24
|
|
24
25
|
if skip_redirect?
|
26
|
+
remember_host(request_host) if @options[:remember_when_skipping]
|
25
27
|
@app.call(env)
|
26
28
|
|
27
29
|
elsif force_redirect?
|
@@ -55,15 +57,14 @@ module GeoRedirect
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def force_redirect?
|
58
|
-
|
59
|
-
Rack::Utils.parse_query(url.query).key? 'redirect'
|
60
|
+
Rack::Utils.parse_query(request_url.query).key? 'redirect'
|
60
61
|
end
|
61
62
|
|
62
63
|
def skip_redirect?
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
query_includes_skip_geo?(request_url) ||
|
65
|
+
path_not_whitelisted?(request_url) ||
|
66
|
+
path_blacklisted?(request_url) ||
|
67
|
+
skipped_by_block?
|
67
68
|
end
|
68
69
|
|
69
70
|
def query_includes_skip_geo?(url)
|
@@ -71,7 +72,7 @@ module GeoRedirect
|
|
71
72
|
end
|
72
73
|
|
73
74
|
def path_not_whitelisted?(url)
|
74
|
-
|
75
|
+
!@include_paths.empty? &&
|
75
76
|
!@include_paths.any? { |exclude| url.path == exclude }
|
76
77
|
end
|
77
78
|
|
@@ -79,12 +80,14 @@ module GeoRedirect
|
|
79
80
|
@exclude_paths.any? { |exclude| url.path == exclude }
|
80
81
|
end
|
81
82
|
|
83
|
+
def skipped_by_block?
|
84
|
+
@options[:skip_if] && @options[:skip_if].call(@request)
|
85
|
+
end
|
86
|
+
|
82
87
|
def handle_force
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
remember_host(host)
|
87
|
-
redirect_request(url.host, true)
|
88
|
+
log 'Handling force flag'
|
89
|
+
remember_host(request_host)
|
90
|
+
redirect_request(request_url.host, true)
|
88
91
|
end
|
89
92
|
|
90
93
|
def handle_geoip
|
@@ -168,7 +171,7 @@ module GeoRedirect
|
|
168
171
|
end
|
169
172
|
|
170
173
|
def init_config(path)
|
171
|
-
YAML.load_file(path) ||
|
174
|
+
YAML.load_file(path) || raise(Errno::EINVAL)
|
172
175
|
rescue Errno::EINVAL, Errno::ENOENT, Psych::SyntaxError, SyntaxError
|
173
176
|
message = <<-ERROR
|
174
177
|
Could not load GeoRedirect config YML file.
|
@@ -185,6 +188,14 @@ module GeoRedirect
|
|
185
188
|
ip_address.split(',').first.strip
|
186
189
|
end
|
187
190
|
|
191
|
+
def request_url
|
192
|
+
@request_url ||= URI.parse(@request.url)
|
193
|
+
end
|
194
|
+
|
195
|
+
def request_host
|
196
|
+
host_by_hostname(request_url.host)
|
197
|
+
end
|
198
|
+
|
188
199
|
def country_from_request
|
189
200
|
ip = request_ip
|
190
201
|
log "Handling GeoIP lookup: IP #{ip}"
|
@@ -196,7 +207,7 @@ module GeoRedirect
|
|
196
207
|
end
|
197
208
|
|
198
209
|
def redirect_url(hostname)
|
199
|
-
url =
|
210
|
+
url = request_url.clone
|
200
211
|
url.port = nil
|
201
212
|
url.host = hostname if hostname
|
202
213
|
|
data/lib/geo_redirect/version.rb
CHANGED
data/lib/geo_redirect.rb
CHANGED
@@ -2,8 +2,8 @@ require 'geo_redirect/middleware'
|
|
2
2
|
require 'geo_redirect/version'
|
3
3
|
|
4
4
|
module GeoRedirect
|
5
|
-
DEFAULT_DB_PATH = 'db/GeoIP.dat'
|
6
|
-
DEFAULT_CONFIG_PATH = 'config/geo_redirect.yml'
|
5
|
+
DEFAULT_DB_PATH = 'db/GeoIP.dat'.freeze
|
6
|
+
DEFAULT_CONFIG_PATH = 'config/geo_redirect.yml'.freeze
|
7
7
|
|
8
8
|
# Load rake tasks
|
9
9
|
require 'geo_redirect/railtie' if defined? Rails
|
data/lib/tasks/geo_redirect.rake
CHANGED
@@ -3,7 +3,7 @@ require 'open-uri'
|
|
3
3
|
require 'zlib'
|
4
4
|
|
5
5
|
namespace :geo_redirect do
|
6
|
-
DB_URI = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'
|
6
|
+
DB_URI = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'.freeze
|
7
7
|
|
8
8
|
desc 'Fetches an updated copy of the GeoIP countries DB from MaxMind'
|
9
9
|
task :fetch_db, :db_path do |_t, args|
|
@@ -225,6 +225,24 @@ describe GeoRedirect::Middleware do
|
|
225
225
|
it { is_expected.to not_redirect }
|
226
226
|
it { is_expected.to remember nil }
|
227
227
|
it { is_expected.to remember_country nil }
|
228
|
+
|
229
|
+
context 'with remember_when_skipping option set to true' do
|
230
|
+
let(:app_options) { { remember_when_skipping: true } }
|
231
|
+
let(:country_code) { 'IL' }
|
232
|
+
let(:request_args) { { skip_geo: true } }
|
233
|
+
it { is_expected.to not_redirect }
|
234
|
+
it { is_expected.to remember :il }
|
235
|
+
it { is_expected.to remember_country nil }
|
236
|
+
end
|
237
|
+
|
238
|
+
context 'with remember_when_skipping option set to false' do
|
239
|
+
let(:app_options) { { remember_when_skipping: false } }
|
240
|
+
let(:country_code) { 'IL' }
|
241
|
+
let(:request_args) { { skip_geo: true } }
|
242
|
+
it { is_expected.to not_redirect }
|
243
|
+
it { is_expected.to remember nil }
|
244
|
+
it { is_expected.to remember_country nil }
|
245
|
+
end
|
228
246
|
end
|
229
247
|
|
230
248
|
context 'with no recognizable IP' do
|
@@ -234,6 +252,24 @@ describe GeoRedirect::Middleware do
|
|
234
252
|
it { is_expected.to remember_country nil }
|
235
253
|
end
|
236
254
|
|
255
|
+
context 'with skip_if block' do
|
256
|
+
let(:country_code) { 'US' }
|
257
|
+
|
258
|
+
context 'when returns true' do
|
259
|
+
let(:app_options) { { skip_if: ->(_req) { true } } }
|
260
|
+
it { is_expected.to not_redirect }
|
261
|
+
it { is_expected.to remember nil }
|
262
|
+
it { is_expected.to remember_country nil }
|
263
|
+
end
|
264
|
+
|
265
|
+
context 'when returns false' do
|
266
|
+
let(:app_options) { { skip_if: ->(_req) { false } } }
|
267
|
+
it { is_expected.to redirect_to :us }
|
268
|
+
it { is_expected.to remember :us }
|
269
|
+
it { is_expected.to remember_country 'US' }
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
237
273
|
describe 'include/exclude logic' do
|
238
274
|
let(:country_code) { 'US' }
|
239
275
|
|
data/spec/support/rake.rb
CHANGED
@@ -11,7 +11,8 @@ shared_context 'rake' do
|
|
11
11
|
before do
|
12
12
|
Rake.application = rake
|
13
13
|
Rake.application
|
14
|
-
|
14
|
+
.rake_require(task_path,
|
15
|
+
[File.join(File.dirname(__FILE__), '..', '..')])
|
15
16
|
|
16
17
|
Rake::Task.define_task(:environment)
|
17
18
|
end
|
metadata
CHANGED
@@ -1,127 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_redirect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sagie Maoz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02
|
11
|
+
date: 2016-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: geoip
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 3.1.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 3.1.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rack
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 1.6.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 1.6.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rack-test
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 0.6.3
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.6.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: simplecov
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - ~>
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 0.9.1
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - ~>
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.9.1
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rubocop
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - ~>
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 0.
|
103
|
+
version: '0.37'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - ~>
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 0.
|
110
|
+
version: '0.37'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rubocop-rspec
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - ~>
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: 1.
|
117
|
+
version: 1.4.0
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - ~>
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: 1.
|
124
|
+
version: 1.4.0
|
125
125
|
description: Geo-location based redirector
|
126
126
|
email:
|
127
127
|
- sagie@waze.com
|
@@ -129,10 +129,10 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
-
- .gitignore
|
133
|
-
- .rspec
|
134
|
-
- .rubocop.yml
|
135
|
-
- .travis.yml
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
136
|
- Gemfile
|
137
137
|
- LICENSE.txt
|
138
138
|
- README.md
|
@@ -160,17 +160,17 @@ require_paths:
|
|
160
160
|
- lib
|
161
161
|
required_ruby_version: !ruby/object:Gem::Requirement
|
162
162
|
requirements:
|
163
|
-
- -
|
163
|
+
- - ">="
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
167
|
requirements:
|
168
|
-
- -
|
168
|
+
- - ">="
|
169
169
|
- !ruby/object:Gem::Version
|
170
170
|
version: '0'
|
171
171
|
requirements: []
|
172
172
|
rubyforge_project:
|
173
|
-
rubygems_version: 2.
|
173
|
+
rubygems_version: 2.4.8
|
174
174
|
signing_key:
|
175
175
|
specification_version: 4
|
176
176
|
summary: Rack middleware to redirect clients to hostnames based on geo-location
|
@@ -183,3 +183,4 @@ test_files:
|
|
183
183
|
- spec/spec_helper.rb
|
184
184
|
- spec/support/geo_redirect.rb
|
185
185
|
- spec/support/rake.rb
|
186
|
+
has_rdoc:
|