Class: Volt::HttpResponseRenderer
- Defined in:
- lib/volt/server/rack/http_response_renderer.rb
Overview
Renders responses for HttpController actions
Class Attribute Summary collapse
-
.renderers ⇒ Object
readonly
Returns the value of attribute renderers.
Class Method Summary collapse
-
.register_renderer(name, content_type, proc) ⇒ Object
Register renderers.
Instance Method Summary collapse
-
#render(content) ⇒ Object
Iterate through @renderes to find a matching renderer for the given content and call the given proc.
Class Attribute Details
.renderers ⇒ Object (readonly)
Returns the value of attribute renderers.
10 11 12 |
# File 'lib/volt/server/rack/http_response_renderer.rb', line 10 def renderers @renderers end |
Class Method Details
.register_renderer(name, content_type, proc) ⇒ Object
Register renderers.
14 15 16 |
# 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 |
Instance Method Details
#render(content) ⇒ Object
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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |