V1.3: Request & Response
Request
In order to access the metadata coming from a HTTP request, an action has a private object request
that derives from Rack::Request
.
Here an example of some information that we can introspect.
# apps/web/controllers/dashboard/index.rb
module Web
module Controllers
module Dashboard
class Index
include Web::Action
def call(params)
puts request.path_info # => "/dashboard"
puts request.request_method # => "GET"
puts request.get? # => true
puts request.post? # => false
puts request.xhr? # => false
puts request.referer # => "http://example.com/"
puts request.user_agent # => "Mozilla/5.0 Macintosh; ..."
puts request.ip # => "127.0.0.1"
puts request.env['HTTP_AUTHORIZATION'] # => "Basic abc123"
end
end
end
end
end
Instantiating a request
for each incoming HTTP request can lead to minor performance degradation.
As an alternative, please consider getting the same information from private action methods like accept?
or from the raw Rack environment params.env
.
Response
The implicit return value of #call
is a serialized Rack::Response
(see #finish):
# apps/web/controllers/dashboard/index.rb
module Web
module Controllers
module Dashboard
class Index
include Web::Action
def call(params)
end
end
end
end
end
# It will return [200, {}, [""]]
It has private accessors to explicitly set status, headers and body:
# apps/web/controllers/dashboard/index.rb
module Web
module Controllers
module Dashboard
class Index
include Web::Action
def call(params)
self.status = 201
self.body = 'Your resource has been created'
self.headers.merge!({ 'X-Custom' => 'OK' })
end
end
end
end
end
# It will return [201, { "X-Custom" => "OK" }, ["Your resource has been created"]]
As shortcut we can use #status
.
# apps/web/controllers/dashboard/index.rb
module Web
module Controllers
module Dashboard
class Index
include Web::Action
def call(params)
status 201, "Your resource has been created"
end
end
end
end
end
# It will return [201, {}, ["Your resource has been created"]]