Class: Jahuty::Service::Snippet

Inherits:
Base
  • Object
show all
Defined in:
lib/jahuty/service/snippet.rb

Overview

A service for interacting with snippets.

Instance Method Summary collapse

Constructor Details

#initialize(client:, cache:, expires_in: nil, prefer_latest: false) ⇒ Snippet

Returns a new instance of Snippet.



7
8
9
10
11
12
13
# File 'lib/jahuty/service/snippet.rb', line 7

def initialize(client:, cache:, expires_in: nil, prefer_latest: false)
  super(client: client)

  @cache = cache
  @expires_in = expires_in
  @prefer_latest = prefer_latest
end

Instance Method Details

#all_renders(tag, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/jahuty/service/snippet.rb', line 15

def all_renders(tag, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest)
  renders = index_renders tag: tag, params: params, prefer_latest: prefer_latest

  cache_renders renders: renders, params: params, expires_in: expires_in, latest: prefer_latest

  renders
end

#render(snippet_id, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest, location: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jahuty/service/snippet.rb', line 23

def render(snippet_id, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest, location: nil)
  key = cache_key snippet_id: snippet_id, params: params, latest: prefer_latest

  render = @cache.read key

  @cache.delete key unless render.nil? || cacheable?(expires_in)

  if render.nil?
    render = show_render snippet_id: snippet_id, params: params, prefer_latest: prefer_latest, location: location

    @cache.write key, render, expires_in: expires_in if cacheable?(expires_in)
  end

  render
end