geo_redirect 0.5.1 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb805fe570535898e17ea6ce307d9678fca06592
4
- data.tar.gz: f778479e3b46d3f14370918f92cbd609aa399b8d
3
+ metadata.gz: bb2df8b52f6869e6ed852973fa5774550c54f76c
4
+ data.tar.gz: b7e9c851cc2a67003efec2d8705eafc227176633
5
5
  SHA512:
6
- metadata.gz: 26a9a0e3ec64548126373b48582e968ee617eeebab97d509679cbbe5b0e1b536d2865d1d43167adc6ae18ad601e411f2de81dba508342830449f12ccdf641604
7
- data.tar.gz: 3d3cb9208831106ff865db2cd66840eaef622d43869a2660bdcc8ce790520f5e6eb7c9fc3d97a7bd0b1e7f91f894d79c836e26972019d0d828c7171ea821804d
6
+ metadata.gz: 2a654e3b640ef5c8eae208f5908e4c638816f933f42c31137b1c0c4c4a0108100282b72506d16c5f413354898c2323520cf2e5d2f54d25c8347b6bd540045e9b
7
+ data.tar.gz: 4ded31b14b6aa4c474a26f632af865bc7ec61ee75030aad98b6c0b39ef89d6321e8718e962bbdf61b88d2abaa9ec07c3b3a51c0a027c582608ab9c98301e4972
data/README.md CHANGED
@@ -90,21 +90,27 @@ and unzip it into `db/` in your project, **or** you could use the following
90
90
  It'd be a good idea to use this task on your (Capistrano or whatever) deployment
91
91
  scripts.
92
92
 
93
- ### Excluding certain URLs from redirection
93
+ ### Scoping redirects for certain URLs
94
94
 
95
- You may want certain URLs in your app to be ignored by GeoRedirect and to
96
- respond normally to *all* incoming requests. You can configure this with the
97
- `:exclude` option.
95
+ By default, GeoRedirect runs on *all* incoming requests and redirects
96
+ accordingly.
97
+
98
+ You may want GeoRedirect to either ignore some URLs (a blacklist), or only run
99
+ the redirection logic over a certain list of URLs (a whitelist). This can be
100
+ done through the `:exclude` and `:include` options.
101
+
102
+ Rails.application.middleware.use GeoRedirect::Middleware, include: '/'
103
+
104
+ will only redirect incoming requests to 'example.org/', and ignore any other
105
+ paths.
98
106
 
99
107
  Rails.application.middleware.use GeoRedirect::Middleware, exclude: '/excluded_path'
100
-
101
- The value of the option can be a string or an array of strings which represent
102
- the path(s) to be excluded. Note that each string must be an exact match of the
103
- path portion of the URL, including the leading slash.
104
-
105
- Rails.application.middleware.use GeoRedirect::Middleware, exclude: '/exclude'
106
-
107
- will match `www.myapp.com/exclude` but not `www.myapp.com/other/path/exclude`
108
+
109
+ will ignore requests to 'example.org/excluded_path' but redirect all other
110
+ incoming requests.
111
+
112
+ The value for both options can be either a single path string or an array of
113
+ paths. All should include the leading slash.
108
114
 
109
115
  ### Custom paths
110
116
 
@@ -6,16 +6,14 @@ module GeoRedirect
6
6
  attr_accessor :db, :config
7
7
 
8
8
  def initialize(app, options = {})
9
- # Some defaults
10
- options[:db] ||= DEFAULT_DB_PATH
11
- options[:config] ||= DEFAULT_CONFIG_PATH
12
-
13
9
  @app = app
14
10
 
15
- @logger = init_logger(options[:logfile]) if options[:logfile]
16
- @db = init_db(options[:db])
17
- @config = init_config(options[:config])
18
- @excludes = Array(options[:exclude])
11
+ @logger = init_logger(options[:logfile])
12
+ @db = init_db(options[:db] || DEFAULT_DB_PATH)
13
+ @config = init_config(options[:config] || DEFAULT_CONFIG_PATH)
14
+
15
+ @include_paths = Array(options[:include])
16
+ @exclude_paths = Array(options[:exclude])
19
17
 
20
18
  log 'Initialized middleware'
21
19
  end
@@ -63,15 +61,22 @@ module GeoRedirect
63
61
 
64
62
  def skip_redirect?
65
63
  url = URI.parse(@request.url)
66
- query_includes_skip_geo?(url) || path_excluded?(url)
64
+ query_includes_skip_geo?(url) ||
65
+ path_not_whitelisted?(url) ||
66
+ path_blacklisted?(url)
67
67
  end
68
68
 
69
69
  def query_includes_skip_geo?(url)
70
70
  Rack::Utils.parse_query(url.query).key? 'skip_geo'
71
71
  end
72
72
 
73
- def path_excluded?(url)
74
- @excludes.any? { |exclude| url.path == exclude }
73
+ def path_not_whitelisted?(url)
74
+ @include_paths.length > 0 &&
75
+ !@include_paths.any? { |exclude| url.path == exclude }
76
+ end
77
+
78
+ def path_blacklisted?(url)
79
+ @exclude_paths.any? { |exclude| url.path == exclude }
75
80
  end
76
81
 
77
82
  def handle_force
@@ -144,7 +149,7 @@ module GeoRedirect
144
149
  end
145
150
 
146
151
  def init_logger(path)
147
- Logger.new(path)
152
+ Logger.new(path) if path
148
153
  rescue Errno::EINVAL, Errno::EACCES
149
154
  nil
150
155
  end
@@ -1,3 +1,3 @@
1
1
  module GeoRedirect
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6'
3
3
  end
@@ -50,7 +50,7 @@ describe GeoRedirect::Middleware do
50
50
  end
51
51
 
52
52
  describe '#log' do
53
- describe 'with valid logfile path' do
53
+ context 'with valid logfile path' do
54
54
  before { mock_app }
55
55
 
56
56
  it 'initiates a log file' do
@@ -68,6 +68,11 @@ describe GeoRedirect::Middleware do
68
68
  mock_app logfile: '/no_such_file'
69
69
  expect(@app.instance_variable_get(:"@logger")).to be_nil
70
70
  end
71
+
72
+ it 'ignores empty logfile option' do
73
+ mock_app logfile: nil
74
+ expect(@app.instance_variable_get(:"@logger")).to be_nil
75
+ end
71
76
  end
72
77
 
73
78
  describe '#host_by_country' do
@@ -157,22 +162,22 @@ describe GeoRedirect::Middleware do
157
162
  end
158
163
  end
159
164
 
160
- describe 'without session memory' do
161
- describe 'for a foreign source' do
165
+ context 'without session memory' do
166
+ context 'for a foreign source' do
162
167
  let(:country_code) { 'US' }
163
168
  it { is_expected.to redirect_to :us }
164
169
  it { is_expected.to remember :us }
165
170
  it { is_expected.to remember_country 'US' }
166
171
  end
167
172
 
168
- describe 'for a local source' do
173
+ context 'for a local source' do
169
174
  let(:country_code) { 'IL' }
170
175
  it { is_expected.to not_redirect }
171
176
  it { is_expected.to remember :il }
172
177
  it { is_expected.to remember_country 'IL' }
173
178
  end
174
179
 
175
- describe 'for an unknown source' do
180
+ context 'for an unknown source' do
176
181
  let(:country_code) { 'SOMEWHERE OVER THE RAINBOW' }
177
182
  it { is_expected.to redirect_to :default }
178
183
  it { is_expected.to remember :default }
@@ -180,7 +185,7 @@ describe GeoRedirect::Middleware do
180
185
  end
181
186
  end
182
187
 
183
- describe 'with valid session memory' do
188
+ context 'with valid session memory' do
184
189
  let(:request_session) { :default }
185
190
  let(:country_code) { 'US' }
186
191
  it { is_expected.to redirect_to :default }
@@ -188,7 +193,7 @@ describe GeoRedirect::Middleware do
188
193
  it { is_expected.to remember_country 'US' }
189
194
  end
190
195
 
191
- describe 'with invalid session memory' do
196
+ context 'with invalid session memory' do
192
197
  let(:request_session) { 'foo' }
193
198
  let(:country_code) { 'US' }
194
199
 
@@ -201,7 +206,7 @@ describe GeoRedirect::Middleware do
201
206
  it { is_expected.to remember_country 'US' }
202
207
  end
203
208
 
204
- describe 'with forced redirect flag' do
209
+ context 'with forced redirect flag' do
205
210
  let(:country_code) { 'US' }
206
211
  let(:request_args) { { redirect: 1 } }
207
212
 
@@ -214,7 +219,7 @@ describe GeoRedirect::Middleware do
214
219
  it { is_expected.to remember_country nil }
215
220
  end
216
221
 
217
- describe 'with skip flag' do
222
+ context 'with skip flag' do
218
223
  let(:country_code) { 'US' }
219
224
  let(:request_args) { { skip_geo: true } }
220
225
  it { is_expected.to not_redirect }
@@ -222,45 +227,90 @@ describe GeoRedirect::Middleware do
222
227
  it { is_expected.to remember_country nil }
223
228
  end
224
229
 
225
- describe 'with no recognizable IP' do
230
+ context 'with no recognizable IP' do
226
231
  let(:country_code) { nil }
227
232
  it { is_expected.to not_redirect }
228
233
  it { is_expected.to remember nil }
229
234
  it { is_expected.to remember_country nil }
230
235
  end
231
236
 
232
- describe 'with an exclude option set' do
233
- let(:app_options) { { exclude: ['/exclude_me', '/exclude_me/too'] } }
234
-
235
- context 'when the request URL matches one of the excluded paths' do
236
- let(:country_code) { 'US' }
237
- let(:request_path) { '/exclude_me?query_param=value' }
237
+ describe 'include/exclude logic' do
238
+ let(:country_code) { 'US' }
238
239
 
240
+ shared_examples :skips_redirect do
239
241
  it { is_expected.to not_redirect }
240
242
  it { is_expected.to remember nil }
241
243
  it { is_expected.to remember_country nil }
242
244
  end
243
245
 
244
- context 'when the request URL does not match one of the excluded paths' do
245
- let(:country_code) { 'US' }
246
- let(:request_path) { '/dont_exclude_me?query_param=value' }
247
-
246
+ shared_examples :does_not_skip_redirect do
248
247
  it { is_expected.to redirect_to :us }
249
248
  it { is_expected.to remember :us }
250
249
  it { is_expected.to remember_country 'US' }
251
250
  end
252
- end
253
251
 
254
- describe 'with a single excluded path' do
255
- let(:app_options) { { exclude: '/exclude_me' } }
252
+ context 'with an include option' do
253
+ let(:app_options) { { include: include_value } }
256
254
 
257
- context 'when the request URL matches one of the excluded paths' do
258
- let(:country_code) { 'US' }
259
- let(:request_path) { '/exclude_me?query_param=value' }
255
+ context 'when include is an array of paths' do
256
+ let(:include_value) { %w(/include_me /include_me/too) }
260
257
 
261
- it { is_expected.to not_redirect }
262
- it { is_expected.to remember nil }
263
- it { is_expected.to remember_country nil }
258
+ context 'when request URL matches one of the included paths' do
259
+ let(:request_path) { '/include_me?query_param=value' }
260
+ it_behaves_like :does_not_skip_redirect
261
+ end
262
+
263
+ context 'when request URL does not match any of the included paths' do
264
+ let(:request_path) { '/dont_include_me?query_param=value' }
265
+ it_behaves_like :skips_redirect
266
+ end
267
+ end
268
+
269
+ context 'when include is a single path' do
270
+ let(:include_value) { '/include_me' }
271
+
272
+ context 'when request URL matches one of the included paths' do
273
+ let(:request_path) { '/include_me?query_param=value' }
274
+ it_behaves_like :does_not_skip_redirect
275
+ end
276
+
277
+ context 'when request URL does not match any of the included paths' do
278
+ let(:request_path) { '/dont_include_me?query_param=value' }
279
+ it_behaves_like :skips_redirect
280
+ end
281
+ end
282
+ end
283
+
284
+ context 'with an exclude option' do
285
+ let(:app_options) { { exclude: exclude_value } }
286
+
287
+ context 'when exclude is an array of paths' do
288
+ let(:exclude_value) { %w(/exclude_me /exclude_me/too) }
289
+
290
+ context 'when request URL matches one of the excluded paths' do
291
+ let(:request_path) { '/exclude_me?query_param=value' }
292
+ it_behaves_like :skips_redirect
293
+ end
294
+
295
+ context 'when request URL does not match any of the excluded paths' do
296
+ let(:request_path) { '/dont_exclude_me?query_param=value' }
297
+ it_behaves_like :does_not_skip_redirect
298
+ end
299
+ end
300
+
301
+ context 'when exclude is a single path' do
302
+ let(:exclude_value) { '/exclude_me' }
303
+
304
+ context 'when request URL matches the excluded path' do
305
+ let(:request_path) { '/exclude_me?query_param=value' }
306
+ it_behaves_like :skips_redirect
307
+ end
308
+
309
+ context 'when request URL does not match any of the excluded paths' do
310
+ let(:request_path) { '/dont_exclude_me?query_param=value' }
311
+ it_behaves_like :does_not_skip_redirect
312
+ end
313
+ end
264
314
  end
265
315
  end
266
316
  end
metadata CHANGED
@@ -1,125 +1,125 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_redirect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sagie Maoz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-21 00:00:00.000000000 Z
11
+ date: 2016-02-01 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
103
  version: 0.33.0
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
110
  version: 0.33.0
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
117
  version: 1.3.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
124
  version: 1.3.0
125
125
  description: Geo-location based redirector
@@ -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.4.8
173
+ rubygems_version: 2.5.1
174
174
  signing_key:
175
175
  specification_version: 4
176
176
  summary: Rack middleware to redirect clients to hostnames based on geo-location
@@ -183,4 +183,3 @@ test_files:
183
183
  - spec/spec_helper.rb
184
184
  - spec/support/geo_redirect.rb
185
185
  - spec/support/rake.rb
186
- has_rdoc: