Sha256: 140ff81ec94b1739c72a64e3579f652fe052c206915394ea64b56b234ee61851

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 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[: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.jsonapi-compliable',
          records: records,
          options: options
        ]
        ActiveSupport::Notifications.instrument(*opts) do
          yield
        end
      else
        yield
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
graphiti-1.0.alpha.1 lib/graphiti/renderer.rb
graphiti-rb-1.0.alpha.1 lib/graphiti/renderer.rb