rack-action 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +113 -13
- data/lib/rack/action.rb +2 -97
- data/rack-action.gemspec +1 -1
- metadata +3 -3
- data/lib/rack/version.rb +0 -5
data/README.md
CHANGED
@@ -1,18 +1,6 @@
|
|
1
1
|
# Rack::Action
|
2
2
|
|
3
|
-
Rack
|
4
|
-
|
5
|
-
require 'rack/action'
|
6
|
-
|
7
|
-
class MyAction < Rack::Action
|
8
|
-
def respond
|
9
|
-
"Hello, World!"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
run MyAction
|
14
|
-
|
15
|
-
See the docs for Rack::Action for more details
|
3
|
+
Rack::Action is a small, simple framework for generating [Rack][rack] responses.
|
16
4
|
|
17
5
|
## Installation
|
18
6
|
|
@@ -28,6 +16,113 @@ Or install it yourself as:
|
|
28
16
|
|
29
17
|
$ gem install rack-action
|
30
18
|
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Rack::Action provides functionality to generate a Rack response. To use Rack::Action, you should subclass Rack::Action and provide your own implementation of `respond`. The simplest Rack action is
|
22
|
+
one that just returns a string from respond:
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
require 'rack/action'
|
26
|
+
|
27
|
+
class MyAction < Rack::Action
|
28
|
+
def respond
|
29
|
+
"Hello, World!"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
run MyAction
|
34
|
+
```
|
35
|
+
|
36
|
+
The class itself is a rack app, so the previous code example is a valid
|
37
|
+
rackup file. Rack::Action is meant to be used with one action per
|
38
|
+
page/endpoint in your application, so it is typically used in conjuction
|
39
|
+
with something like [Rack::Router][rack-router], which would look something like this:
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
require 'rack/action'
|
43
|
+
require 'rack/router'
|
44
|
+
|
45
|
+
class FooAction < Rack::Action
|
46
|
+
def respond
|
47
|
+
"foo"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class BarAction < Rack::Action
|
52
|
+
def respond
|
53
|
+
"bar"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
router = Rack::Router.new do
|
58
|
+
get "/foo" => FooAction
|
59
|
+
get "/bar" => BarAction
|
60
|
+
end
|
61
|
+
|
62
|
+
run router
|
63
|
+
```
|
64
|
+
|
65
|
+
Rack::Action makes an instance of [Rack::Request][rack-req] and [Rack::Response][rack-res] available
|
66
|
+
which can be used to set headers, cookies, etc.
|
67
|
+
|
68
|
+
``` ruby
|
69
|
+
class ArticleAction < Rack::Action
|
70
|
+
def respond
|
71
|
+
article = Article.find(params["id"])
|
72
|
+
response['Content-Type'] = "text/xml"
|
73
|
+
article.to_xml
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
You can use before filters to do things before respond is called:
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
class AccountAction < Rack::Action
|
82
|
+
before_filter :load_current_user
|
83
|
+
|
84
|
+
def load_current_user
|
85
|
+
@current_user = User.find(params["id"])
|
86
|
+
end
|
87
|
+
|
88
|
+
def respond
|
89
|
+
"Welcome Back, #{@current_user.name}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
and you can of course share functionality across actions with inheritance:
|
95
|
+
|
96
|
+
``` ruby
|
97
|
+
class ApplicationAction < Rack::Action
|
98
|
+
before_filter :login_required
|
99
|
+
|
100
|
+
def login_required
|
101
|
+
redirect_to "/login" unless logged_in?
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class PublicAction < ApplicationAction
|
106
|
+
skip_before_filter :login_required
|
107
|
+
|
108
|
+
def respond
|
109
|
+
"Hello"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class PrivateAction < ApplicationAction
|
114
|
+
def respond
|
115
|
+
"It's A Secret To Everybody."
|
116
|
+
end
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
Before filters will execute in the order they are defined. If a before
|
121
|
+
filter writes to the response, subsequent filters will not be executed
|
122
|
+
and the respond method will not be executed. As long as no before filters
|
123
|
+
write to the response, subsequent filters and the respond
|
124
|
+
method will be called.
|
125
|
+
|
31
126
|
## Contributing
|
32
127
|
|
33
128
|
1. Fork it
|
@@ -35,3 +130,8 @@ Or install it yourself as:
|
|
35
130
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
131
|
4. Push to the branch (`git push origin my-new-feature`)
|
37
132
|
5. Create new Pull Request
|
133
|
+
|
134
|
+
[rack]: http://rack.github.com/
|
135
|
+
[rack-req]: http://rubydoc.info/gems/rack/Rack/Request
|
136
|
+
[rack-res]: http://rubydoc.info/gems/rack/Rack/Response
|
137
|
+
[rack-router]: https://github.com/pjb3/rack-router
|
data/lib/rack/action.rb
CHANGED
@@ -2,106 +2,11 @@ require 'time'
|
|
2
2
|
require 'json'
|
3
3
|
require 'rack'
|
4
4
|
require 'rack/filters'
|
5
|
-
require 'rack/version'
|
6
5
|
|
7
6
|
module Rack
|
8
|
-
# Rack::Action provides functionality to generate a Rack response.
|
9
|
-
# To use Rack::Action, you should subclass Rack::Action and provide
|
10
|
-
# your own implementation of {#respond}. The simplest Rack action is
|
11
|
-
# one that just returns a string from respond:
|
12
|
-
#
|
13
|
-
# require 'rack/action'
|
14
|
-
#
|
15
|
-
# class MyAction < Rack::Action
|
16
|
-
# def respond
|
17
|
-
# "Hello, World!"
|
18
|
-
# end
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# run MyAction
|
22
|
-
#
|
23
|
-
# The class itself is a rack app, so the previous code example is a valid
|
24
|
-
# rackup file. Rack::Action is meant to be used with one action per
|
25
|
-
# page/endpoint in your application, so it is typically used in conjuction
|
26
|
-
# with something like Rack::Router, which would look something like this:
|
27
|
-
#
|
28
|
-
# require 'rack/action'
|
29
|
-
# require 'rack/router'
|
30
|
-
#
|
31
|
-
# class FooAction < Rack::Action
|
32
|
-
# def respond
|
33
|
-
# "foo"
|
34
|
-
# end
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# class BarAction < Rack::Action
|
38
|
-
# def respond
|
39
|
-
# "bar"
|
40
|
-
# end
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# router = Rack::Router.new do
|
44
|
-
# get "/foo" => FooAction
|
45
|
-
# get "/bar" => BarAction
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# run router
|
49
|
-
#
|
50
|
-
# Rack::Action makes an instance of Rack::Request and Rack::Response available
|
51
|
-
# which can be used to set headers, cookies, etc.
|
52
|
-
#
|
53
|
-
# class ArticleAction < Rack::Action
|
54
|
-
# def respond
|
55
|
-
# article = Article.find(params["id"])
|
56
|
-
# response['Content-Type'] = "text/xml"
|
57
|
-
# article.to_xml
|
58
|
-
# end
|
59
|
-
# end
|
60
|
-
#
|
61
|
-
# You can use before filters to do things before respond is called:
|
62
|
-
#
|
63
|
-
# class AccountAction < Rack::Action
|
64
|
-
# before_filter :load_current_user
|
65
|
-
#
|
66
|
-
# def load_current_user
|
67
|
-
# @current_user = User.find(params["id"])
|
68
|
-
# end
|
69
|
-
#
|
70
|
-
# def respond
|
71
|
-
# "Welcome Back, #{@current_user.name}"
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# and you can of course share functionality across actions with inheritance:
|
76
|
-
#
|
77
|
-
# class ApplicationAction < Rack::Action
|
78
|
-
# before_filter :login_required
|
79
|
-
#
|
80
|
-
# def login_required
|
81
|
-
# redirect_to "/login" unless logged_in?
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
#
|
85
|
-
# class PublicAction < ApplicationAction
|
86
|
-
# skip_before_filter :login_required
|
87
|
-
#
|
88
|
-
# def respond
|
89
|
-
# "Hello"
|
90
|
-
# end
|
91
|
-
# end
|
92
|
-
#
|
93
|
-
# class PrivateAction < ApplicationAction
|
94
|
-
# def respond
|
95
|
-
# "It's A Secret To Everybody."
|
96
|
-
# end
|
97
|
-
# end
|
98
|
-
#
|
99
|
-
# Before filters will execute in the order they are defined. If a before
|
100
|
-
# filter writes to the response, subsequent filters will not be executed
|
101
|
-
# and the respond method will not be executed. As long as no before filters
|
102
|
-
# write to the response, execution of subsequent filters and the respond
|
103
|
-
# method will be called.
|
104
7
|
class Action
|
8
|
+
VERSION = '0.2.1'
|
9
|
+
|
105
10
|
extend Filters
|
106
11
|
|
107
12
|
# @private
|
data/rack-action.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-action
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -45,7 +45,6 @@ files:
|
|
45
45
|
- lib/rack-action.rb
|
46
46
|
- lib/rack/action.rb
|
47
47
|
- lib/rack/filters.rb
|
48
|
-
- lib/rack/version.rb
|
49
48
|
- lib/rack_action.rb
|
50
49
|
- rack-action.gemspec
|
51
50
|
- test/rack/action_test.rb
|
@@ -77,3 +76,4 @@ summary: a small, simple framework for generating Rack responses
|
|
77
76
|
test_files:
|
78
77
|
- test/rack/action_test.rb
|
79
78
|
- test/rack_test.rb
|
79
|
+
has_rdoc:
|