Module: Stitches::ApiClientAccessWrapper

Defined in:
lib/stitches/api_client_access_wrapper.rb

Class Method Summary collapse

Class Method Details

.api_key_cacheObject



32
33
34
35
36
37
# File 'lib/stitches/api_client_access_wrapper.rb', line 32

def self.api_key_cache
  @api_key_cache ||= LruRedux::TTL::ThreadSafeCache.new(
    Stitches.configuration.max_cache_size,
    Stitches.configuration.max_cache_ttl,
  )
end

.cache_enabledObject



39
40
41
# File 'lib/stitches/api_client_access_wrapper.rb', line 39

def self.cache_enabled
  Stitches.configuration.max_cache_ttl.positive?
end

.clear_api_cacheObject



28
29
30
# File 'lib/stitches/api_client_access_wrapper.rb', line 28

def self.clear_api_cache
  api_key_cache.clear if cache_enabled
end

.fetch_for_key(key) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/stitches/api_client_access_wrapper.rb', line 5

def self.fetch_for_key(key)
  if cache_enabled
    fetch_for_key_from_cache(key)
  else
    fetch_for_key_from_db(key)
  end
end

.fetch_for_key_from_cache(key) ⇒ Object



13
14
15
16
17
# File 'lib/stitches/api_client_access_wrapper.rb', line 13

def self.fetch_for_key_from_cache(key)
  api_key_cache.getset(key) do
    fetch_for_key_from_db(key)
  end
end

.fetch_for_key_from_db(key) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/stitches/api_client_access_wrapper.rb', line 19

def self.fetch_for_key_from_db(key)
  if ::ApiClient.column_names.include?("enabled")
    ::ApiClient.find_by(key: key, enabled: true)
  else
    ActiveSupport::Deprecation.warn('api_keys is missing "enabled" column.  Run "rails g stitches:add_enabled_to_api_clients"')
    ::ApiClient.find_by(key: key)
  end
end