tgauge 0.1.3 → 0.1.4
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/.gitignore +2 -0
- data/lib/db/router.rb +59 -57
- data/lib/db/server.rb +4 -3
- data/lib/db/show_exceptions.rb +0 -1
- data/lib/version.rb +1 -1
- 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: 253f30fa85171dd6864075d5031debeff3ef3c81
|
4
|
+
data.tar.gz: 9e29cffe94fb981e244fb1ce339428d8e2e22616
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc22ef833fdfe91701380846af6a39b47fd4584b4709e58a1b7e474fff9449a6ac0177c7c97ec549e386fb0f68ef35e2cb02008ebdfe9bd6330e632833434f2a
|
7
|
+
data.tar.gz: e28dd468782f3e5a8712f3e7668878fa15a2cd0a94325c5d6bad67e9f3b61d48e8eaca224773a2a67265bf4facca63da3a4879381bc8297e5f0f0b0af80df56b
|
data/.gitignore
CHANGED
data/lib/db/router.rb
CHANGED
@@ -1,74 +1,76 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module TGauge
|
2
|
+
class Route
|
3
|
+
attr_reader :pattern, :http_method, :controller_class, :action_name
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# checks if pattern matches path and method matches request method
|
12
|
-
def matches?(req)
|
13
|
-
(@pattern =~ req.path) == 0 && req.request_method.downcase == @http_method.to_s
|
14
|
-
end
|
5
|
+
def initialize(pattern, http_method, controller_class, action_name)
|
6
|
+
@pattern = pattern
|
7
|
+
@http_method = http_method
|
8
|
+
@controller_class = controller_class
|
9
|
+
@action_name = action_name
|
10
|
+
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
match_data = @pattern.match(req.path)
|
20
|
-
route_params = {}
|
21
|
-
match_data.names.each do |key|
|
22
|
-
route_params[key] = match_data[key]
|
12
|
+
# checks if pattern matches path and method matches request method
|
13
|
+
def matches?(req)
|
14
|
+
(@pattern =~ req.path) == 0 && req.request_method.downcase == @http_method.to_s
|
23
15
|
end
|
24
16
|
|
25
|
-
|
26
|
-
controller
|
17
|
+
# use pattern to pull out route params (save for later?)
|
18
|
+
# instantiate controller and call controller action
|
19
|
+
def run(req, res)
|
20
|
+
match_data = @pattern.match(req.path)
|
21
|
+
route_params = {}
|
22
|
+
match_data.names.each do |key|
|
23
|
+
route_params[key] = match_data[key]
|
24
|
+
end
|
25
|
+
|
26
|
+
controller = controller_class.new(req, res, route_params)
|
27
|
+
controller.invoke_action(@action_name)
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
class Router
|
31
|
-
|
31
|
+
class Router
|
32
|
+
attr_reader :routes
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def initialize
|
35
|
+
@routes = []
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
# simply adds a new route to the list of routes
|
39
|
+
def add_route(pattern, method, controller_class, action_name)
|
40
|
+
@routes << Route.new(pattern, method, controller_class, action_name)
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
# evaluate the proc in the context of the instance
|
44
|
+
# for syntactic sugar :)
|
45
|
+
def draw(&proc)
|
46
|
+
self.instance_eval(&proc)
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
# make each of these methods that
|
50
|
+
# when called add route
|
51
|
+
[:get, :post, :put, :delete].each do |http_method|
|
52
|
+
define_method(http_method) do |path, controller, action_name|
|
53
|
+
add_route(path, http_method, controller, action_name)
|
54
|
+
end
|
53
55
|
end
|
54
|
-
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
# should return the route that matches this request
|
58
|
+
def match(req)
|
59
|
+
@routes.each do |route|
|
60
|
+
return route if route.matches?(req)
|
61
|
+
end
|
62
|
+
nil
|
60
63
|
end
|
61
|
-
nil
|
62
|
-
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
65
|
+
# either throw 404 or call run on a matched route
|
66
|
+
def run(req, res)
|
67
|
+
route = match(req)
|
68
|
+
if route
|
69
|
+
route.run(req, res)
|
70
|
+
else
|
71
|
+
res.write("Route #{req.path} could not be found.")
|
72
|
+
res.status = 404
|
73
|
+
end
|
72
74
|
end
|
73
75
|
end
|
74
76
|
end
|
data/lib/db/server.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rack'
|
2
|
-
|
3
|
-
require_relative '
|
4
|
-
require_relative '
|
2
|
+
require './config/routes'
|
3
|
+
require_relative 'static_viewer'
|
4
|
+
require_relative 'show_exceptions'
|
5
5
|
|
6
6
|
module TGauge
|
7
7
|
class Server
|
@@ -23,5 +23,6 @@ module TGauge
|
|
23
23
|
app: full_app,
|
24
24
|
Port: 3000
|
25
25
|
})
|
26
|
+
end
|
26
27
|
end
|
27
28
|
end
|
data/lib/db/show_exceptions.rb
CHANGED
data/lib/version.rb
CHANGED