rack-routing 0.0.6 → 0.0.7
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 +4 -4
- data/lib/rack/route.rb +38 -34
- data/lib/rack/router.rb +27 -23
- data/lib/rack/shared.rb +8 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 775f85f9f806f2508607c2eca0643ca70f686f0f
|
4
|
+
data.tar.gz: 43cf5f06c4af528e01e9dc328391a5f06c96098c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a945d9c2d6a49038513510ef617012a118d09be013ccf78963bd3a45c3623328313a41f0d2ab9514a81e20cc3643dadf176572686fb6a911950bed7f89d7b94
|
7
|
+
data.tar.gz: 67a56c5e40f2cac7e2da709d724d75d4ed7ae2dc8471780682feab9c293b8e4912bf79cda513a06678ee02d1ab763f1239ca1543e82371e4d5149146c8c20fc6
|
data/lib/rack/route.rb
CHANGED
@@ -1,36 +1,40 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
module Rack
|
2
|
+
module Routing
|
3
|
+
class Route
|
4
|
+
def initialize path
|
5
|
+
@my_parts = path.split( '/' ).reject{| p | String.blank?( p )}.map( &:strip )
|
6
|
+
end
|
7
|
+
|
8
|
+
def match? parts
|
9
|
+
return false unless parts.count == @my_parts.count
|
10
|
+
|
11
|
+
indexes = ( 0..@my_parts.count - 1 ).to_a
|
12
|
+
indexes.delete param_index
|
13
|
+
|
14
|
+
indexes.all? do |i|
|
15
|
+
parts[ i ].to_s == @my_parts[ i ]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def params_for parts
|
20
|
+
return {} unless param_index
|
21
|
+
|
22
|
+
{ @my_parts[ param_index ].sub( /\A:/, '' ).to_sym => parts[ param_index ]}
|
23
|
+
end
|
24
|
+
|
25
|
+
def routing_method
|
26
|
+
return "#{ @my_parts[ 0 ].downcase }_root".to_sym if @my_parts.count == 1
|
27
|
+
|
28
|
+
@my_parts.reject{ |p| p[ 0 ] == ':' }.map( &:downcase ).join( '_' ).to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
def param_index
|
32
|
+
@my_parts.index{ |p| p[ 0 ] == ':' }
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
"#{ self.class } @my_parts:#{ @my_parts } count:#{ @my_parts.count }"
|
37
|
+
end
|
14
38
|
end
|
15
39
|
end
|
16
|
-
|
17
|
-
def params_for parts
|
18
|
-
return {} unless param_index
|
19
|
-
|
20
|
-
{ @my_parts[ param_index ].sub( /\A:/, '' ).to_sym => parts[ param_index ]}
|
21
|
-
end
|
22
|
-
|
23
|
-
def routing_method
|
24
|
-
return "#{ @my_parts[ 0 ].downcase }_root".to_sym if @my_parts.count == 1
|
25
|
-
|
26
|
-
@my_parts.reject{ |p| p[ 0 ] == ':' }.map( &:downcase ).join( '_' ).to_sym
|
27
|
-
end
|
28
|
-
|
29
|
-
def param_index
|
30
|
-
@my_parts.index{ |p| p[ 0 ] == ':' }
|
31
|
-
end
|
32
|
-
|
33
|
-
def to_s
|
34
|
-
"#{ self.class } @my_parts:#{ @my_parts } count:#{ @my_parts.count }"
|
35
|
-
end
|
36
|
-
end
|
40
|
+
end
|
data/lib/rack/router.rb
CHANGED
@@ -1,33 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Rack
|
2
|
+
module Routing
|
3
|
+
class Router
|
4
|
+
VALID_HTTP_METHODS = [ :GET, :POST, :PUT, :DELETE ]
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
class << self
|
7
|
+
def for env
|
8
|
+
http_method = env[ 'REQUEST_METHOD' ].to_sym
|
7
9
|
|
8
|
-
|
10
|
+
raise 'Invalid HTTP method' unless VALID_HTTP_METHODS.include?( http_method )
|
9
11
|
|
10
|
-
|
12
|
+
parts = [ http_method ] + env[ 'PATH_INFO' ].split( '/' ).reject{| p | String.blank?( p )}
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
matched_route = ROUTES.find do |route|
|
15
|
+
route.match?( parts )
|
16
|
+
end
|
17
|
+
|
18
|
+
return { method: :not_found, params:{} } unless matched_route
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
params = matched_route.params_for( parts )
|
21
|
+
|
22
|
+
{ method: matched_route.routing_method,
|
23
|
+
params:params }
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def load_routes
|
27
|
+
lines = ::File.read( ROUTES_FILE )
|
28
|
+
.split( "\n" )
|
29
|
+
.reject{| l | String.blank?( l )}
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
+
lines.map do |line|
|
32
|
+
Route.new( line )
|
33
|
+
end
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
data/lib/rack/shared.rb
CHANGED