Sha256: 8207e20cea867da091b3399a2cfa59425315995e1769344506ff968658402ba6

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module Graphiti
  class Renderer
    CONTENT_TYPE = 'application/vnd.api+json'

    attr_reader :proxy, :options

    def initialize(proxy, options)
      @proxy = proxy
      @options = options
    end

    def records
      @records ||= @proxy.data
    end

    def to_jsonapi
      render(JSONAPI::Renderer.new).to_json
    end

    def to_json
      render(Graphiti::HashRenderer.new(@proxy.resource)).to_json
    end

    def to_xml
      render(Graphiti::HashRenderer.new(@proxy.resource)).to_xml(root: :data)
    end

    private

    def render(implementation)
      notify do
        instance = JSONAPI::Serializable::Renderer.new(implementation)
        options[:fields] = proxy.fields
        options[:expose] ||= {}
        options[:expose][:extra_fields] = proxy.extra_fields
        options[:expose][:proxy] = proxy
        options[:include] = proxy.include_hash
        options[:meta] ||= {}
        options[:meta].merge!(stats: proxy.stats) unless proxy.stats.empty?
        instance.render(records, options)
      end
    end

    # TODO: more generic notification pattern
    # Likely comes out of debugger work
    def notify
      if defined?(ActiveSupport::Notifications)
        opts = [
          'render.graphiti',
          records: records,
          options: options
        ]
        ActiveSupport::Notifications.instrument(*opts) do
          yield
        end
      else
        yield
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
graphiti-1.0.alpha.7 lib/graphiti/renderer.rb
graphiti-1.0.alpha.6 lib/graphiti/renderer.rb
graphiti-1.0.alpha.5 lib/graphiti/renderer.rb
graphiti-1.0.alpha.4 lib/graphiti/renderer.rb