refraction 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -0
- data/VERSION +1 -1
- data/lib/refraction.rb +20 -7
- data/refraction.gemspec +2 -2
- data/spec/refraction_spec.rb +18 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -40,6 +40,10 @@ Refraction can be installed in a Rails application as a plugin.
|
|
40
40
|
|
41
41
|
$ script/plugin install git://github.com/pivotal/refraction.git
|
42
42
|
|
43
|
+
It can also be used as a gem:
|
44
|
+
|
45
|
+
$ gem install refraction
|
46
|
+
|
43
47
|
In `environments/production.rb`, add Refraction at or near the top of your middleware stack.
|
44
48
|
|
45
49
|
config.middleware.insert_before(::Rack::Lock, ::Refraction, {})
|
@@ -102,6 +106,17 @@ The `found!` method tells Refraction to return a response with a `302 Found` sta
|
|
102
106
|
URL for the Location header. Like `#rewrite!` it can take either a string or hash argument to set
|
103
107
|
the URL or some of its components.
|
104
108
|
|
109
|
+
### `RequestContext#respond!(status, headers, content)`
|
110
|
+
|
111
|
+
Use `respond!` to return an arbitrary response to the request. This is useful for responding with
|
112
|
+
the contents of a static file. For example:
|
113
|
+
|
114
|
+
req.respond!(503, {'Content-Type' => 'text/html'}, File.read(MAINTENANCE_PATH))
|
115
|
+
|
116
|
+
The args are largely the same as the contents of a standard Rack response array with the exceptions
|
117
|
+
that you don't need to wrap the content in an array, and the Content-Length header is generated so it
|
118
|
+
should not be supplied.
|
119
|
+
|
105
120
|
### URL components
|
106
121
|
|
107
122
|
The request object provides the following components of the URL for matching requests: `scheme`,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/refraction.rb
CHANGED
@@ -26,12 +26,9 @@ class Refraction
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def response
|
29
|
-
headers = {
|
30
|
-
|
31
|
-
|
32
|
-
'Content-Length' => message.length.to_s
|
33
|
-
}
|
34
|
-
[status, headers, message]
|
29
|
+
headers = @headers || { 'Location' => location, 'Content-Type' => 'text/plain' }
|
30
|
+
headers['Content-Length'] = message.length.to_s
|
31
|
+
[status, headers, [message]]
|
35
32
|
end
|
36
33
|
|
37
34
|
# URI part accessors
|
@@ -60,6 +57,15 @@ class Refraction
|
|
60
57
|
@env['REQUEST_METHOD']
|
61
58
|
end
|
62
59
|
|
60
|
+
# borrowed from Rack Request class, i.e. time to refactor
|
61
|
+
def ip
|
62
|
+
if addr = @env['HTTP_X_FORWARDED_FOR']
|
63
|
+
addr.split(',').last.strip
|
64
|
+
else
|
65
|
+
@env['REMOTE_ADDR']
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
63
69
|
# actions
|
64
70
|
|
65
71
|
def set(options)
|
@@ -95,6 +101,13 @@ class Refraction
|
|
95
101
|
@message = "moved to #{@uri}"
|
96
102
|
end
|
97
103
|
|
104
|
+
def respond!(status, headers, content)
|
105
|
+
@action = :respond
|
106
|
+
@status = status
|
107
|
+
@headers = headers
|
108
|
+
@message = content
|
109
|
+
end
|
110
|
+
|
98
111
|
def location
|
99
112
|
@uri.to_s
|
100
113
|
end
|
@@ -124,7 +137,7 @@ class Refraction
|
|
124
137
|
self.rules.call(context)
|
125
138
|
|
126
139
|
case context.action
|
127
|
-
when :permanent, :found
|
140
|
+
when :permanent, :found, :respond
|
128
141
|
context.response
|
129
142
|
when :rewrite
|
130
143
|
env["rack.url_scheme"] = context.scheme
|
data/refraction.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{refraction}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Pivotal Labs", "Josh Susser", "Sam Pierson", "Wai Lun Mang"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-12-23}
|
13
13
|
s.description = %q{Reflection is a Rails plugin and standalone Rack middleware library. Give up quirky config syntax and use plain old Ruby for your rewrite and redirection rules.}
|
14
14
|
s.email = %q{gems@pivotallabs.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/refraction_spec.rb
CHANGED
@@ -173,6 +173,24 @@ describe Refraction do
|
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
|
+
describe "generate arbitrary response" do
|
177
|
+
before(:each) do
|
178
|
+
Refraction.configure do |req|
|
179
|
+
req.respond!(503, {'Content-Type' => 'text/plain'}, "Site down for maintenance.")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should respond with status, headers and content" do
|
184
|
+
env = Rack::MockRequest.env_for('http://example.com', :method => 'get')
|
185
|
+
app = mock('app')
|
186
|
+
response = Refraction.new(app).call(env)
|
187
|
+
response[0].should == 503
|
188
|
+
response[1]['Content-Length'].to_i.should == "Site down for maintenance.".length
|
189
|
+
response[1]['Content-Type'].should == "text/plain"
|
190
|
+
response[2].should == ["Site down for maintenance."]
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
176
194
|
describe "environment" do
|
177
195
|
before(:each) do
|
178
196
|
Refraction.configure do |req|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: refraction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pivotal Labs
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2009-
|
15
|
+
date: 2009-12-23 00:00:00 -08:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|