Class: Volt::HttpResponseRenderer
- Defined in:
- lib/volt/server/rack/http_response_renderer.rb
Overview
Renders responses for HttpController actions
Class Method Summary collapse
-
.register_renderer(name, content_type, proc) ⇒ Object
Register renderers.
- .renderers ⇒ Object
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 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 |
.renderers ⇒ Object
9 10 11 |
# File 'lib/volt/server/rack/http_response_renderer.rb', line 9 def self.renderers @renderers 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 fromt he 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 |
# 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) return [rendered, content.merge(content_type: renderer[:content_type])] end end # If we couldn't find a renderer - just render an empty string ['', content_type: 'text/plain'] end |