route_downcaser 1.0.1 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YWRjOGIwNDZmMzcyODY2NDExNTUyYmRkYWZlNDEwNjI4ZDA1NWE2OA==
5
- data.tar.gz: !binary |-
6
- YjkxZmI2MDk3MDEyODJiOTI1N2ViMjZlYTA3YWY5ZmMzYzUyZTRhYg==
2
+ SHA1:
3
+ metadata.gz: 80487f0b652b00f07ebcf03598a66e2c03f97540
4
+ data.tar.gz: 10c96abacc02b01dbefa2867133a7a6a0460fec1
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MTAzYzJmYTc5ZTBhOGZmNTFhYzNkYjBkOTI2NjdiMGRiYTc1MWNlY2E4ODhk
10
- MWRiN2M5YmZhOGJiZmQ4ZWJkYmNjNzhkMTRlMGNiMDdjM2I5NzE2ZjUzOTA3
11
- MTc3ZDVhZmY2NmIzZjkzMjM2YzZiOGVmOTAwOGUyZjcyMjJmMzA=
12
- data.tar.gz: !binary |-
13
- NWZlZjJiMzM2MWEwNDA4ZDFjMWNlYTczODQxNTM1NmMxOGQxOGE4OGUyZmY3
14
- MTEzMzMyY2I5YTBmODBiODU4MWRmOGUyMGQ5OGI5MzMzZTI1ODJkNGUzMGZi
15
- NTNjMTA5MWMzYzMxZGI3NjZmMmQyYTI2MjIzNTNjNjlhMzg0MmM=
6
+ metadata.gz: f3d0aa4181a49067a556d33b0f3ebf3ddace5e25a0adf4c714b2162f5b34c144191b4139f8d09a8dd7a306e0c5f729bbf43c3840d68c5ffedaa254aac33eaead
7
+ data.tar.gz: 3129784c5b341d4aedc92f03cf08a3d5e71614f55acc12b8f293995f005bb982fae5054e11a5f90ceb28cd7b88f4ee074a375bb9424191b38136b90416fa6b4d
data/README.rdoc CHANGED
@@ -100,6 +100,10 @@ All it really does is to take the path and downcase it before dispatching. Query
100
100
 
101
101
  == Changelog
102
102
 
103
+ === 1.1.0
104
+
105
+ Refactored main code and tests for much better structure
106
+
103
107
  === 1.0.1
104
108
 
105
109
  Refactored parts of the code to work with Rails 4.2.0
@@ -1,55 +1,78 @@
1
1
  module RouteDowncaser
2
+
2
3
  class DowncaseRouteMiddleware
3
4
  def initialize(app)
4
5
  @app = app
5
6
  end
6
7
 
7
8
  def call(env)
8
- env = env.clone
9
- if env['REQUEST_URI']
10
- if RouteDowncaser.redirect == true && !exclude_patterns_match?(env['REQUEST_URI'])
11
- if path(env) != path(env).downcase
12
- return [301, {'Location' => downcased_uri(env), 'Content-Type' => 'text/html'}, []]
13
- end
14
- else
15
- env['REQUEST_URI'] = downcased_uri(env)
16
- end
9
+ new_env = env.clone
10
+
11
+ # Don't touch anything, if uri/path is part of exclude_patterns
12
+ if exclude_patterns_match?(new_env['REQUEST_URI']) or exclude_patterns_match?(new_env['PATH_INFO'])
13
+ @app.call(new_env)
14
+ return
17
15
  end
18
16
 
19
- if exclude_patterns_match?(env['PATH_INFO'])
20
- pieces = env['PATH_INFO'].split('/')
21
- env['PATH_INFO'] = pieces.slice(0..-2).join('/').downcase + '/' + pieces.last
22
- elsif env['PATH_INFO']
23
- env['PATH_INFO'] = env['PATH_INFO'].downcase
17
+ # Downcase request_uri and/or path_info if applicable
18
+ if new_env['REQUEST_URI'].present?
19
+ new_env['REQUEST_URI'] = downcased_uri(new_env['REQUEST_URI'])
24
20
  end
25
21
 
26
- @app.call(env)
22
+ if new_env['PATH_INFO'].present?
23
+ new_env['PATH_INFO'] = downcased_uri(new_env['PATH_INFO'])
24
+ end
25
+
26
+ # If redirect configured, then return redirect request,
27
+ # if either request_uri or path_info has changed
28
+ if RouteDowncaser.redirect
29
+ if new_env["REQUEST_URI"].present? and new_env["REQUEST_URI"] != env["REQUEST_URI"]
30
+ return redirect_header(new_env["REQUEST_URI"])
31
+ end
32
+
33
+ if new_env["PATH_INFO"].present? and new_env["PATH_INFO"] != env["PATH_INFO"]
34
+ return redirect_header(new_env["PATH_INFO"])
35
+ end
36
+ end
37
+
38
+ # Default just move to next chain in Rack callstack
39
+ # calling with downcased uri if needed
40
+ @app.call(new_env)
27
41
  end
28
42
 
29
43
  private
30
44
 
31
- def uri_items(env)
32
- env['REQUEST_URI'].split('?')
45
+ def exclude_patterns_match?(uri)
46
+ uri.match(Regexp.union(RouteDowncaser.exclude_patterns)) if uri and RouteDowncaser.exclude_patterns
33
47
  end
34
48
 
35
- def path(env)
36
- uri_items(env).first
49
+ def downcased_uri(uri)
50
+ if has_querystring?(uri)
51
+ "#{path(uri).downcase}?#{querystring(uri)}"
52
+ else
53
+ path(uri).downcase
54
+ end
37
55
  end
38
56
 
39
- def querystring?(env)
40
- uri_items(env).length > 1
57
+ def path(uri)
58
+ uri_items(uri).first
41
59
  end
42
60
 
43
- def downcased_uri(env)
44
- if querystring?(env)
45
- "#{path(env).downcase!}?#{uri_items(env)[1]}"
46
- else
47
- path(env).downcase!
48
- end
61
+ def querystring(uri)
62
+ uri_items(uri).last
49
63
  end
50
64
 
51
- def exclude_patterns_match?(uri)
52
- uri.match(Regexp.union(RouteDowncaser.exclude_patterns)) if uri
65
+ def has_querystring?(uri)
66
+ uri_items(uri).length > 1
67
+ end
68
+
69
+ def uri_items(uri)
70
+ uri.split('?')
71
+ end
72
+
73
+ def redirect_header(uri)
74
+ [301, {'Location' => uri, 'Content-Type' => 'text/html'}, []]
53
75
  end
54
76
  end
77
+
55
78
  end
@@ -1,3 +1,3 @@
1
1
  module RouteDowncaser
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end