Sha256: 2043ae1d65efe13ce92f79f56ec035f9ff329d656ce6b9d9679e8904dd907ae3

Contents?: true

Size: 1.53 KB

Versions: 28

Compression:

Stored size: 1.53 KB

Contents

require 'json'
require 'volt'

module Volt
  # Renders responses for HttpController actions
  class HttpResponseRenderer
    @renderers = {}

    class << self
      attr_reader :renderers
    end

    # Register renderers.
    def self.register_renderer(name, content_type, proc)
      @renderers[name.to_sym] = { proc: proc, content_type: content_type }
    end

    # Default renderers for json and plain text
    register_renderer(:json, 'application/json', proc { |data| data.to_json })
    register_renderer(:text, 'text/plain', proc { |data| data.to_s })

    # 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
    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
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
volt-0.9.7.pre8 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.7.pre7 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.7.pre6 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.7.pre5 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.7.pre3 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.7.pre2 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.6 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.6.pre3 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.6.pre2 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.6.pre1 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre12 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre11 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre9 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre8 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre7 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre6 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre5 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre4 lib/volt/server/rack/http_response_renderer.rb
volt-0.9.5.pre3 lib/volt/server/rack/http_response_renderer.rb