class Volt::HttpController
Allow you to create controllers that act as http endpoints
Attributes
request[R]
response_body[RW]
response_headers[R]
Public Class Methods
new(volt_app, params, request)
click to toggle source
Initialzed with the params parsed from the route and the HttpRequest
# File lib/volt/controllers/http_controller.rb, line 17 def initialize(volt_app, params, request) @volt_app = volt_app @response_headers = HttpResponseHeader.new @response_body = [] @request = request @initial_params = params end
Public Instance Methods
params()
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 25 def params @params ||= begin params = request.params.symbolize_keys.merge(@initial_params) Volt::Model.new(params, persistor: Volt::Persistors::Params) end end
perform(action='index')
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 36 def perform(action='index') filtered = run_callbacks(:before_action, action) send(action.to_sym) unless filtered run_callbacks(:after_action, action) unless filtered respond end
Private Instance Methods
head(status, additional_headers = {})
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 52 def head(status, additional_headers = {}) @response_status = status response_headers.merge!(additional_headers) end
redirect_to(target, status = :found)
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 57 def redirect_to(target, status = :found) head(status, location: target) end
render(content)
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 61 def render(content) status = content.delete(:status) || :ok body, additional_headers = HttpResponseRenderer.new.render(content) head(status, additional_headers) response_body << body end
respond()
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 68 def respond unless @response_status # render was not called, show an error @response_body = ['Error: render was not called in controller action'] @response_status = 500 end resp = Rack::Response.new(response_body, response_status, response_headers) # Update any changed cookies new_cookies = cookies.persistor.changed_cookies new_cookies.each_pair do |key, value| if value.is_a?(String) value = {value: value} end value[:path] = '/' resp.set_cookie(key.to_s, value) end resp end
response_status()
click to toggle source
Returns the http status code as integer
# File lib/volt/controllers/http_controller.rb, line 93 def response_status if @response_status.is_a?(Symbol) Rack::Utils::SYMBOL_TO_STATUS_CODE[@response_status] else @response_status.try(:to_i) || 200 end end
store()
click to toggle source
# File lib/volt/controllers/http_controller.rb, line 48 def store @volt_app.store end