rack-routing 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rack/route.rb +36 -0
- data/lib/rack/router.rb +34 -0
- data/lib/rack/routing.rb +3 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5180644b69907f5515bd74d8b6cfe964c45e8e18
|
4
|
+
data.tar.gz: 27a3c2296bb33036c41de0b663e76919f0699490
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cd91f8f0daaf547f6642fcb9cb3859f6de1c54ac439f4338853c173838174fa4cc3725d92f39a1cfaf7642703a21261bdd349008b0c5fa19e40495f76888927
|
7
|
+
data.tar.gz: f10160f142a95015e93495dfa0e7e72c2888631cc66b2aff9ee5be4a43c3a69ca883d12cd0539fe50d8c892d421b56ed548083a3903074198a382e03e552af0f
|
data/lib/rack/route.rb
ADDED
@@ -0,0 +1,36 @@
|
|
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 ]
|
14
|
+
end
|
15
|
+
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
|
data/lib/rack/router.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Router
|
2
|
+
VALID_HTTP_METHODS = [ :GET, :POST, :PUT, :DELETE ]
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def for env
|
6
|
+
http_method = env[ 'REQUEST_METHOD' ].to_sym
|
7
|
+
|
8
|
+
raise 'Invalid HTTP method' unless VALID_HTTP_METHODS.include?( http_method )
|
9
|
+
|
10
|
+
parts = [ http_method ] + env[ 'PATH_INFO' ].split( '/' ).reject{| p | blank?( p )}
|
11
|
+
|
12
|
+
matched_route = ROUTES.find do |route|
|
13
|
+
route.match?( parts )
|
14
|
+
end
|
15
|
+
|
16
|
+
return { method: :not_found, params:{} } unless matched_route
|
17
|
+
|
18
|
+
params = matched_route.params_for( parts )
|
19
|
+
|
20
|
+
{ method: matched_route.routing_method,
|
21
|
+
params:params }
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_routes
|
25
|
+
lines = File.read( ROUTES_FILE )
|
26
|
+
.split( "\n" )
|
27
|
+
.reject{| l | blank?( l )}
|
28
|
+
|
29
|
+
lines.map do |line|
|
30
|
+
Route.new( line )
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/rack/routing.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- George Ulmer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack-test
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.6.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.6.2
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.5'
|
83
|
+
description: Set up routes quickly and easily for a rack app.
|
84
|
+
email: george@boost-media.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/rack/route.rb
|
90
|
+
- lib/rack/router.rb
|
91
|
+
- lib/rack/routing.rb
|
92
|
+
homepage: http://rubygems.org/gems/rack-routes
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Rack router for your Rack app.
|
116
|
+
test_files: []
|