Class: Graphiti::Renderer

Inherits:
Object show all
Defined in:
lib/graphiti/renderer.rb

Constant Summary collapse

CONTENT_TYPE =
"application/vnd.api+json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, options) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
# File 'lib/graphiti/renderer.rb', line 7

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/graphiti/renderer.rb', line 5

def options
  @options
end

#proxyObject (readonly)

Returns the value of attribute proxy.



5
6
7
# File 'lib/graphiti/renderer.rb', line 5

def proxy
  @proxy
end

Class Method Details

.graphql_renderer(proxy) ⇒ Object



50
51
52
53
# File 'lib/graphiti/renderer.rb', line 50

def self.graphql_renderer(proxy)
  implementation = Graphiti::HashRenderer.new(proxy.resource, graphql: true)
  JSONAPI::Serializable::Renderer.new(implementation)
end

.hash_renderer(proxy) ⇒ Object



45
46
47
48
# File 'lib/graphiti/renderer.rb', line 45

def self.hash_renderer(proxy)
  implementation = Graphiti::HashRenderer.new(proxy.resource)
  JSONAPI::Serializable::Renderer.new(implementation)
end

.jsonapi_rendererObject



40
41
42
43
# File 'lib/graphiti/renderer.rb', line 40

def self.jsonapi_renderer
  @jsonapi_renderer ||= JSONAPI::Serializable::Renderer
    .new(JSONAPI::Renderer.new)
end

Instance Method Details

#as_graphqlObject



20
21
22
# File 'lib/graphiti/renderer.rb', line 20

def as_graphql
  render(self.class.graphql_renderer(@proxy))
end

#as_jsonObject



32
33
34
# File 'lib/graphiti/renderer.rb', line 32

def as_json
  render(self.class.hash_renderer(@proxy))
end

#recordsObject



12
13
14
# File 'lib/graphiti/renderer.rb', line 12

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

#to_graphqlObject



24
25
26
# File 'lib/graphiti/renderer.rb', line 24

def to_graphql
  as_graphql.to_json
end

#to_jsonObject



28
29
30
# File 'lib/graphiti/renderer.rb', line 28

def to_json
  as_json.to_json
end

#to_jsonapiObject



16
17
18
# File 'lib/graphiti/renderer.rb', line 16

def to_jsonapi
  render(self.class.jsonapi_renderer).to_json
end

#to_xmlObject



36
37
38
# File 'lib/graphiti/renderer.rb', line 36

def to_xml
  render(self.class.hash_renderer(@proxy)).to_xml(root: :data)
end