soar_sc-rack-router 2.0.0 → 2.0.1
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,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f58cbab68abf5cb5fae462ee5cbd3f684ec90825
|
4
|
+
data.tar.gz: 9db67f90583e5d57fc1eed5e20b2e9a2b015af64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33186c63a75e6e5670113e83fa269d7b452b6060c4cb7fe2ba7b2b353a07057ad1541ba8a7bbfeab3c5e2c7693829263424c140ecb62f938669d94a008109ace
|
7
|
+
data.tar.gz: ef4aed9e7bb629ac050dd84843a3fdb139668498d41676b990add2e283ff89a6012685a87f5e56ca46708a7b82c2d8138e4afe165de49bcd275e1871a8fb442e
|
@@ -8,7 +8,15 @@ module SoarSc
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
class
|
11
|
+
class InvalidRouteError < RuntimeError; end
|
12
|
+
|
13
|
+
class InvalidPathError < InvalidRouteError; end
|
14
|
+
|
15
|
+
class InvalidParameterError < InvalidPathError; end
|
16
|
+
|
17
|
+
class InvalidMethodError < InvalidRouteError; end
|
18
|
+
|
19
|
+
class InvalidActionError < InvalidRouteError; end
|
12
20
|
|
13
21
|
end
|
14
22
|
end
|
@@ -4,14 +4,20 @@ module SoarSc
|
|
4
4
|
module HttpMethod
|
5
5
|
|
6
6
|
ANY = "*"
|
7
|
-
|
8
|
-
POST = "POST"
|
9
|
-
PUT = "PUT"
|
7
|
+
CONNECT = "CONNECT"
|
10
8
|
DELETE = "DELETE"
|
9
|
+
GET = "GET"
|
11
10
|
HEAD = "HEAD"
|
12
11
|
OPTIONS = "OPTIONS"
|
12
|
+
POST = "POST"
|
13
|
+
PUT = "PUT"
|
13
14
|
TRACE = "TRACE"
|
14
|
-
|
15
|
+
|
16
|
+
ALL_METHODS = [ ANY, CONNECT, DELETE, GET, HEAD, OPTIONS, POST, PUT, TRACE ]
|
17
|
+
|
18
|
+
def self.valid?(method)
|
19
|
+
ALL_METHODS.include?(method)
|
20
|
+
end
|
15
21
|
|
16
22
|
end
|
17
23
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'soar_sc/rack/router/http_method'
|
2
|
+
require 'soar_sc/rack/router/route_validation'
|
2
3
|
|
3
4
|
# TODO Normalize paths (compress runs of slashes)
|
4
5
|
module SoarSc
|
@@ -6,6 +7,9 @@ module SoarSc
|
|
6
7
|
class Router
|
7
8
|
|
8
9
|
class Route
|
10
|
+
|
11
|
+
include RouteValidation
|
12
|
+
|
9
13
|
PARAMETER_PREFIX = ":"
|
10
14
|
PARAMETER_VALID_REGEXP = /:[a-z_][a-z0-9_]*/
|
11
15
|
REQUEST_METHOD = "REQUEST_METHOD"
|
@@ -16,8 +20,8 @@ module SoarSc
|
|
16
20
|
attr_reader :method, :path, :action
|
17
21
|
|
18
22
|
def initialize(method, path, action)
|
19
|
-
validate_path(path)
|
20
23
|
@method, @path, @action = method, path, action
|
24
|
+
validate
|
21
25
|
@static = components(@path).none? { |c| c.start_with?(PARAMETER_PREFIX) }
|
22
26
|
end
|
23
27
|
|
@@ -36,6 +40,10 @@ module SoarSc
|
|
36
40
|
parameters
|
37
41
|
end
|
38
42
|
|
43
|
+
def to_s
|
44
|
+
"<#{@method.inspect} #{@path.inspect} #{@action.inspect}>"
|
45
|
+
end
|
46
|
+
|
39
47
|
private
|
40
48
|
|
41
49
|
def method_matches?(method)
|
@@ -75,14 +83,6 @@ module SoarSc
|
|
75
83
|
path.split('/')
|
76
84
|
end
|
77
85
|
|
78
|
-
def validate_path(path)
|
79
|
-
components(path).each do |c|
|
80
|
-
if c.start_with?(PARAMETER_PREFIX) and c !~ PARAMETER_VALID_REGEXP
|
81
|
-
raise InvalidParameterError, "Invalid parameter #{c} in path #{path}"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
86
|
end
|
87
87
|
|
88
88
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module SoarSc
|
2
|
+
module Rack
|
3
|
+
class Router
|
4
|
+
|
5
|
+
module RouteValidation
|
6
|
+
|
7
|
+
def validate
|
8
|
+
validate_method
|
9
|
+
validate_path
|
10
|
+
validate_action
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_method
|
14
|
+
unless HttpMethod.valid?(@method)
|
15
|
+
raise InvalidMethodError, "Invalid method in route #{to_s}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_path
|
20
|
+
unless @path.is_a?(String)
|
21
|
+
raise InvalidPathError, "Invalid path in route #{to_s}"
|
22
|
+
end
|
23
|
+
components(@path).each do |c|
|
24
|
+
if c.start_with?(Route::PARAMETER_PREFIX) and c !~ Route::PARAMETER_VALID_REGEXP
|
25
|
+
raise InvalidParameterError, "Invalid path parameter in route #{to_s}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_action
|
31
|
+
unless (@action.respond_to?(:arity) and @action.arity == 1) or (@action.respond_to?(:call) and @action.method(:call).arity == 1)
|
32
|
+
raise InvalidActionError, "Invalid action in route #{to_s}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soar_sc-rack-router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sheldon Hearn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/soar_sc/rack/router/errors.rb
|
89
89
|
- lib/soar_sc/rack/router/http_method.rb
|
90
90
|
- lib/soar_sc/rack/router/route.rb
|
91
|
+
- lib/soar_sc/rack/router/route_validation.rb
|
91
92
|
- lib/soar_sc/rack/router/version.rb
|
92
93
|
- soar_sc-rack-router.gemspec
|
93
94
|
homepage: https://github.com/hetznerZA/soar_sc-rack-router
|