class Jahuty::Service::Snippet

A service for interacting with snippets.

Public Class Methods

new(client:, cache:, expires_in: nil, prefer_latest: false) click to toggle source
Calls superclass method Jahuty::Service::Base::new
# 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

Public Instance Methods

all_renders(tag, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

cache_key(snippet_id:, params: {}, latest: false) click to toggle source
# File lib/jahuty/service/snippet.rb, line 41
def cache_key(snippet_id:, params: {}, latest: false)
  fingerprint = Digest::MD5.new
  fingerprint << "snippets/#{snippet_id}/render/"
  fingerprint << params.to_json
  fingerprint << '/latest' if latest

  "jahuty_#{fingerprint.hexdigest}"
end
cache_renders(renders:, params:, expires_in: @expires_in, latest: false) click to toggle source
# File lib/jahuty/service/snippet.rb, line 50
def cache_renders(renders:, params:, expires_in: @expires_in, latest: false)
  return if renders.nil?

  return unless cacheable?(expires_in)

  global_params = params['*'] || {}

  renders.each do |render|
    local_params = params[render.snippet_id.to_s] || {}
    render_params = ::Jahuty::Util.deep_merge global_params, local_params

    key = cache_key snippet_id: render.snippet_id, params: render_params, latest: latest

    @cache.write key, render, expires_in: expires_in
  end
end
cacheable?(expires_in) click to toggle source
# File lib/jahuty/service/snippet.rb, line 67
def cacheable?(expires_in)
  expires_in.nil? || expires_in.positive?
end
index_renders(tag:, params: {}, prefer_latest: false) click to toggle source
# File lib/jahuty/service/snippet.rb, line 71
def index_renders(tag:, params: {}, prefer_latest: false)
  request_params = { tag: tag }
  request_params[:params] = params.to_json unless params.empty?
  request_params[:latest] = 1 if prefer_latest

  action = ::Jahuty::Action::Index.new(
    resource: 'render',
    params: request_params
  )

  @client.request action
end
show_render(snippet_id:, params: {}, prefer_latest: false, location: nil) click to toggle source
# File lib/jahuty/service/snippet.rb, line 84
def show_render(snippet_id:, params: {}, prefer_latest: false, location: nil)
  request_params = {}
  request_params[:params] = params.to_json unless params.empty?
  request_params[:latest] = 1 if prefer_latest
  request_params[:location] = location unless location.nil?

  action = ::Jahuty::Action::Show.new(
    id: snippet_id,
    resource: 'render',
    params: request_params
  )

  @client.request action
end