-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from thunderboltlabs/master
Fix #to_rack to handle non-array response bodies.
- Loading branch information
Showing
3 changed files
with
29 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,18 +1,31 @@ | |||
require 'rack' | require 'rack' | ||
|
|
||
class MyRackApp | class MyRackApp | ||
class NonArrayResponse | |||
# The rack response body need not implement #join, | |||
# but it must implement #each. It need not be an Array. | |||
# ActionDispatch::Response, for example, exercises that fact. | |||
# See: http://rack.rubyforge.org/doc/SPEC.html | |||
|
|||
def each(*args, &blk) | |||
["This is not in an array!"].each(*args, &blk) | |||
end | |||
end | |||
|
|||
def self.call(env) | def self.call(env) | ||
case env.values_at('REQUEST_METHOD', 'PATH_INFO') | case env.values_at('REQUEST_METHOD', 'PATH_INFO') | ||
when ['GET', '/'] | when ['GET', '/'] | ||
[200, {}, ["This is my root!"]] | [200, {}, ["This is my root!"]] | ||
when ['GET', '/greet'] | when ['GET', '/greet'] | ||
name = env['QUERY_STRING'][/name=([^&]*)/, 1] || "World" | name = env['QUERY_STRING'][/name=([^&]*)/, 1] || "World" | ||
[200, {}, ["Hello, #{name}"]] | [200, {}, ["Hello, #{name}"]] | ||
when ['GET', '/non_array_response'] | |||
[200, {}, NonArrayResponse.new] | |||
when ['POST', '/greet'] | when ['POST', '/greet'] | ||
name = env["rack.input"].read[/name=([^&]*)/, 1] || "World" | name = env["rack.input"].read[/name=([^&]*)/, 1] || "World" | ||
[200, {}, ["Good to meet you, #{name}!"]] | [200, {}, ["Good to meet you, #{name}!"]] | ||
else | else | ||
[404, {}, ['']] | [404, {}, ['']] | ||
end | end | ||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters