Class: Renderful::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/renderful/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contentful:, renderers:, cache: nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/renderful/client.rb', line 7

def initialize(contentful:, renderers:, cache: nil)
  @contentful = contentful
  @renderers = renderers
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5
6
7
# File 'lib/renderful/client.rb', line 5

def cache
  @cache
end

#contentfulObject (readonly)

Returns the value of attribute contentful.



5
6
7
# File 'lib/renderful/client.rb', line 5

def contentful
  @contentful
end

#renderersObject (readonly)

Returns the value of attribute renderers.



5
6
7
# File 'lib/renderful/client.rb', line 5

def renderers
  @renderers
end

Instance Method Details

#cache_key_for(entry) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/renderful/client.rb', line 24

def cache_key_for(entry)
  if entry.respond_to?(:content_type)
    cache_key_for(
      content_type_id: entry.content_type.id,
      entry_id: entry.id,
    )
  else
    "contentful/#{entry.fetch(:content_type_id)}/#{entry.fetch(:entry_id)}"
  end
end

#render(entry) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/renderful/client.rb', line 13

def render(entry)
  renderer = renderers[entry.content_type.id]
  fail(NoRendererError, entry) unless renderer

  return cache.read(cache_key_for(entry)) if cache&.exist?(cache_key_for(entry))

  renderer.new(entry, client: self).render.tap do |output|
    cache&.write(cache_key_for(entry), output)
  end
end