Sha256: 07e3535616acc4b61d9aff86edce3b3dc4897059363c087d1c98992f2ea9dbd8
Contents?: true
Size: 1.41 KB
Versions: 1
Compression:
Stored size: 1.41 KB
Contents
module JsonapiCompliable 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(JsonapiCompliable::HashRenderer.new(@proxy.resource)).to_json end def to_xml render(JsonapiCompliable::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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jsonapi_compliable-1.0.alpha.2 | lib/jsonapi_compliable/renderer.rb |