rack-routing 0.0.6 → 0.0.7

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: 3c39e9a75ddf65ae69be35a7168d6b233d993c5d
4
- data.tar.gz: 391e0ebacf9c3494660ea1be48a7d9eaac70b460
3
+ metadata.gz: 775f85f9f806f2508607c2eca0643ca70f686f0f
4
+ data.tar.gz: 43cf5f06c4af528e01e9dc328391a5f06c96098c
5
5
  SHA512:
6
- metadata.gz: 1c8446a2b59eb3a2c69e861b51e466d311eb6b5d166b8af1b75424a9efb6ecbeca5894fa6a301825fbda2ad295c87b7e88f932a3d828db21fa5be8ee59514044
7
- data.tar.gz: 58c414a7c5318180f90c2b06fb113bc032b70af490960efdceac6fad49f31a6a4457b1bff67c24207d1ecaf69485476e59aac0ac0088623de797877dd5ae0d0e
6
+ metadata.gz: 0a945d9c2d6a49038513510ef617012a118d09be013ccf78963bd3a45c3623328313a41f0d2ab9514a81e20cc3643dadf176572686fb6a911950bed7f89d7b94
7
+ data.tar.gz: 67a56c5e40f2cac7e2da709d724d75d4ed7ae2dc8471780682feab9c293b8e4912bf79cda513a06678ee02d1ab763f1239ca1543e82371e4d5149146c8c20fc6
@@ -1,36 +1,40 @@
1
- class Route
2
- def initialize path
3
- @my_parts = path.split( '/' ).reject{| p | blank?( p )}.map( &:strip )
4
- end
5
-
6
- def match? parts
7
- return false unless parts.count == @my_parts.count
8
-
9
- indexes = ( 0..@my_parts.count - 1 ).to_a
10
- indexes.delete param_index
11
-
12
- indexes.all? do |i|
13
- parts[ i ].to_s == @my_parts[ i ]
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
@@ -1,33 +1,37 @@
1
- class Router
2
- VALID_HTTP_METHODS = [ :GET, :POST, :PUT, :DELETE ]
1
+ module Rack
2
+ module Routing
3
+ class Router
4
+ VALID_HTTP_METHODS = [ :GET, :POST, :PUT, :DELETE ]
3
5
 
4
- class << self
5
- def for env
6
- http_method = env[ 'REQUEST_METHOD' ].to_sym
6
+ class << self
7
+ def for env
8
+ http_method = env[ 'REQUEST_METHOD' ].to_sym
7
9
 
8
- raise 'Invalid HTTP method' unless VALID_HTTP_METHODS.include?( http_method )
10
+ raise 'Invalid HTTP method' unless VALID_HTTP_METHODS.include?( http_method )
9
11
 
10
- parts = [ http_method ] + env[ 'PATH_INFO' ].split( '/' ).reject{| p | blank?( p )}
12
+ parts = [ http_method ] + env[ 'PATH_INFO' ].split( '/' ).reject{| p | String.blank?( p )}
11
13
 
12
- matched_route = ROUTES.find do |route|
13
- route.match?( parts )
14
- end
15
-
16
- return { method: :not_found, params:{} } unless matched_route
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
- params = matched_route.params_for( parts )
19
-
20
- { method: matched_route.routing_method,
21
- params:params }
22
- end
20
+ params = matched_route.params_for( parts )
21
+
22
+ { method: matched_route.routing_method,
23
+ params:params }
24
+ end
23
25
 
24
- def load_routes
25
- lines = File.read( ROUTES_FILE )
26
- .split( "\n" )
27
- .reject{| l | blank?( l )}
26
+ def load_routes
27
+ lines = ::File.read( ROUTES_FILE )
28
+ .split( "\n" )
29
+ .reject{| l | String.blank?( l )}
28
30
 
29
- lines.map do |line|
30
- Route.new( line )
31
+ lines.map do |line|
32
+ Route.new( line )
33
+ end
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -1,3 +1,9 @@
1
- def blank? str
2
- str.nil? || str == ''
1
+ module Rack
2
+ module Routing
3
+ class String
4
+ def self.blank? str
5
+ str.nil? || str == ''
6
+ end
7
+ end
8
+ end
3
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-routing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Ulmer