Skip to content

Instantly share code, notes, and snippets.

@dashdotat
Created February 21, 2009 09:14
Show Gist options
  • Save dashdotat/67966 to your computer and use it in GitHub Desktop.
Save dashdotat/67966 to your computer and use it in GitHub Desktop.
Sinatra accepts a number of different types in the results processing code, and processes/displays them in the following order:
nil
===
If nil is passed to the results, then just the standard Sinatra headers are returned to the client, in the following example
-- test.rb
get "/test/" do
nil
end
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Server: thin 1.0.0 codename That's What She Said
String
======
If a string (responds to to_str) is passed as the results, the standard Sinatra headers are returned to the client with the string (without any further processing) is returned as the body, as shown in the following example
-- test.rb
get "/test/" do
"This is just a string"
end
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 21
Connection: keep-alive
Server: thin 1.0.0 codename That's What She Said
This is just a string
Array
=====
Processing of arrays are handled in the following way
1. [Status, (Headers), Body]
The array consists of the numeric HTTP Status code, an optional hash of additional headers to add to the response and the body to return to the client, as shown in the following examples
-- test.rb
get "/test/" do
[500, "Internal Server Error"]
end
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 500 Internal Server Error
Content-Type: text/html
Content-Length: 21
Connection: keep-alive
Server: thin 1.0.0 codename That's What She Said
Internal Server Error
-- test.rb
get "/test/" do
[500, {"X-Sung-By" => "Sinatra #{Sinatra::VERSION}"}, "Internal Server Error"]
end
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 500 Internal Server Error
X-Sung-By: Sinatra 0.9.0.5
Content-Type: text/html
Content-Length: 21
Connection: keep-alive
Server: thin 1.0.0 codename That's What She Said
Internal Server Error
Collection
==========
If the result responds to each but doesn't match any of the prior conditions, to_s is called on each member of the collection with the results sent as the body to the client, as shown in the following example using File
-- test.rb
get "/test/" do
File.open("data.txt","r")
end
-- data.txt
123
456
789
123
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Server: thin 1.0.0 codename That's What She Said
123
456
789
123
Range
=====
If the result is a number in the range 100 to 599 then the HTTP Status code is set to the result, and no additional headers or body is sent to the client, as shown in the following example
-- test.rb
get "/test/" do
404
end
-- curl -i http://127.0.0.1:4567/test/
HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Server: thin 1.0.0 codename That's What She Said
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment