Module: JustimmoClient::V1::JustimmoInterface

Includes:
API, Caching, Logging
Included in:
EmployeeInterface, RealtyInterface
Defined in:
lib/justimmo_client/api/v1/interfaces/justimmo_interface.rb

Instance Method Summary collapse

Methods included from API

#api, #interface, #model, #representer, #request, #versioned_api

Methods included from Logging

default_logger, #logger, rails_logger

Methods included from Caching

#cache, default_cache

Instance Method Details

#cache_key(endpoint, params = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/justimmo_client/api/v1/interfaces/justimmo_interface.rb', line 9

def cache_key(endpoint, params = {})
  key = Digest::SHA256.new
  key << endpoint
  key << params.to_s
  key.hexdigest
end

#with_cache(key, on_hit:, on_miss:, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/justimmo_client/api/v1/interfaces/justimmo_interface.rb', line 16

def with_cache(key, on_hit:, on_miss:, **options)
  log.debug("Looking up cache key #{key}")
  data = nil
  cached = cache.read(key)

  if cached.nil?
    log.debug("Cache miss for #{key}")
    data, new_cache = on_miss.call()
    cache.write(key, new_cache, options)
  else
    log.debug("Cache hit for #{key}")
    data = on_hit.call(cached)
  end

  data
end