class Volt::HttpResponseRenderer
Renders responses for HttpController
actions
Attributes
renderers[R]
Public Class Methods
register_renderer(name, content_type, proc)
click to toggle source
Register renderers.
# File lib/volt/server/rack/http_response_renderer.rb, line 14 def self.register_renderer(name, content_type, proc) @renderers[name.to_sym] = { proc: proc, content_type: content_type } end
Public Instance Methods
render(content)
click to toggle source
Iterate through @renderes to find a matching renderer for the given content and call the given proc. Other params from the content are returned as additional headers Returns an empty string if no renderer could be found
# File lib/volt/server/rack/http_response_renderer.rb, line 26 def render(content) content = content.symbolize_keys self.class.renderers.keys.each do |renderer_name| if content.key?(renderer_name) renderer = self.class.renderers[renderer_name] to_render = content.delete(renderer_name) rendered = renderer[:proc].call(to_render) # Unwrap a promise if we got one back if rendered.is_a?(Promise) rendered = rendered.sync end return [rendered, content.merge(content_type: renderer[:content_type])] end end # If we couldn't find a renderer - just render an empty string ["Error: render only supports #{self.class.renderers.keys.join(', ')}", content_type: 'text/plain'] end