track 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +71 -1
- data/lib/track.rb +4 -6
- data/lib/track/{application.rb → controller.rb} +23 -16
- data/lib/track/filter_map.rb +13 -6
- metadata +13 -14
- data/lib/track/config.rb +0 -7
data/README.md
CHANGED
@@ -1,3 +1,73 @@
|
|
1
1
|
# Track
|
2
2
|
|
3
|
-
A nano framework for server applications based on rack and ruby 1.9
|
3
|
+
A nano framework for server applications based on rack and ruby 1.9 named capture groups.
|
4
|
+
|
5
|
+
## What Track does
|
6
|
+
|
7
|
+
- modularize your rack app through controllers
|
8
|
+
- routes paths to methods inside of your controllers via the `route` method
|
9
|
+
- define before filters via the `pre` method
|
10
|
+
|
11
|
+
## What Track does *not*
|
12
|
+
|
13
|
+
- support any template engines, your actions have to return low level rack responses
|
14
|
+
- support any ORM framework
|
15
|
+
|
16
|
+
## Install
|
17
|
+
|
18
|
+
add the following to your Gemfile
|
19
|
+
|
20
|
+
`gem 'track'`
|
21
|
+
|
22
|
+
or install it via rubygems
|
23
|
+
|
24
|
+
`gem install track`
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Subclass the `Track::Controller` class to define controllers:
|
29
|
+
|
30
|
+
### Example
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class UsersController < Track::Controller
|
34
|
+
|
35
|
+
route '/', :index, :get
|
36
|
+
route '/show/:id', :show, :get
|
37
|
+
route '/update/(?<id> [^\/]+)', :show, [:post, :put]
|
38
|
+
|
39
|
+
pre :find_user, :show
|
40
|
+
|
41
|
+
def index
|
42
|
+
[200, { "Content-Type" => 'text/plain' }, StringIO.new('hello from index')]
|
43
|
+
end
|
44
|
+
|
45
|
+
def show
|
46
|
+
[200, { "Content-Type" => 'text/plain' }, StringIO.new(@user.name)]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def find_user
|
52
|
+
@user = User.find(params['id'])
|
53
|
+
fail [404, { "Content-Type" => 'text/plain' }, StringIO.new('user not found')] unless @user
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
The `route` method maps a route to an action in your controller. You can define named parameters by appending a : or by using a named capture group.
|
59
|
+
|
60
|
+
You can build arbitrary path match patterns by using regaular expressions.
|
61
|
+
|
62
|
+
The `pre` method calls a method prior to the action. If `fail` is called in before filter method the action will not get called and the given response will be returned.
|
63
|
+
|
64
|
+
## Author
|
65
|
+
|
66
|
+
[Lars Kuhnt](http://www.github.com/larskuhnt)
|
67
|
+
Copyright (c) 2011
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
Published under the MIT License.
|
72
|
+
|
73
|
+
See [LICENSE](LICENSE) for details.
|
data/lib/track.rb
CHANGED
@@ -3,18 +3,17 @@ require_relative 'route_map'
|
|
3
3
|
require_relative 'filter_map'
|
4
4
|
|
5
5
|
module Track
|
6
|
-
class
|
7
|
-
|
6
|
+
class Controller
|
7
|
+
|
8
8
|
@@route_map = RouteMap.new
|
9
9
|
@@filter_map = FilterMap.new
|
10
|
-
|
10
|
+
|
11
11
|
attr_accessor :params
|
12
|
-
|
13
|
-
def initialize
|
14
|
-
@routing_error_response = routing_error_response
|
12
|
+
|
13
|
+
def initialize
|
15
14
|
@params = {}
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
def call(env)
|
19
18
|
req = Rack::Request.new(env)
|
20
19
|
@params.merge!(req.params)
|
@@ -22,28 +21,36 @@ module Track
|
|
22
21
|
@params.merge!(route[:matches])
|
23
22
|
response_for(route)
|
24
23
|
else
|
25
|
-
|
24
|
+
routing_error
|
26
25
|
end
|
27
26
|
end
|
28
|
-
|
27
|
+
|
29
28
|
protected
|
30
|
-
|
29
|
+
|
30
|
+
def routing_error
|
31
|
+
[404, { 'Content-Type' => 'text/plain' }, ['route not found']]
|
32
|
+
end
|
33
|
+
|
31
34
|
def response_for(route)
|
32
35
|
if filters = @@filter_map.scan(self.class.name, route[:action])
|
33
36
|
filters.each do |m|
|
34
|
-
|
35
|
-
return
|
37
|
+
send(m)
|
38
|
+
return @_response if @_response
|
36
39
|
end
|
37
40
|
end
|
38
41
|
send(route[:action])
|
39
42
|
end
|
40
|
-
|
43
|
+
|
44
|
+
def fail(response = [404, { 'Content-Type' => 'text/plain' }, ['']])
|
45
|
+
@_response = response
|
46
|
+
end
|
47
|
+
|
41
48
|
def self.route(pattern, action, methods = nil)
|
42
49
|
@@route_map.add self.name, pattern, action, methods
|
43
50
|
end
|
44
|
-
|
45
|
-
def self.
|
46
|
-
@@filter_map.add self.name,
|
51
|
+
|
52
|
+
def self.pre(method, actions)
|
53
|
+
@@filter_map.add self.name, method, actions
|
47
54
|
end
|
48
55
|
|
49
56
|
end
|
data/lib/track/filter_map.rb
CHANGED
@@ -3,14 +3,21 @@
|
|
3
3
|
module Track
|
4
4
|
class FilterMap < Hash
|
5
5
|
|
6
|
-
def add(
|
7
|
-
self[
|
8
|
-
|
9
|
-
|
6
|
+
def add(key, method, actions)
|
7
|
+
self[key] ||= {}
|
8
|
+
actions = actions.is_a?(Array) ? actions.map(&:to_sym) : [actions.to_sym]
|
9
|
+
actions.each do |action|
|
10
|
+
self[key][action.to_sym] ||= []
|
11
|
+
self[key][action.to_sym] << method.to_sym
|
12
|
+
end
|
10
13
|
end
|
11
14
|
|
12
|
-
def scan(
|
13
|
-
|
15
|
+
def scan(key, action)
|
16
|
+
if self[key]
|
17
|
+
self[key][action.to_sym] ? self[key][action.to_sym] : []
|
18
|
+
else
|
19
|
+
[]
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: track
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-08 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &70191469044140 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70191469044140
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70191469043600 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70191469043600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: guard-rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70191469043180 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70191469043180
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rack-test
|
49
|
-
requirement: &
|
49
|
+
requirement: &70191469042760 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70191469042760
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70191469042300 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70191469042300
|
69
69
|
description: Nano framework to build small server applications based on rack and ruby
|
70
70
|
1.9
|
71
71
|
email:
|
@@ -74,8 +74,7 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
-
- lib/track/
|
78
|
-
- lib/track/config.rb
|
77
|
+
- lib/track/controller.rb
|
79
78
|
- lib/track/filter_map.rb
|
80
79
|
- lib/track/route_map.rb
|
81
80
|
- lib/track.rb
|