Module: Stitches::ApiClientAccessWrapper

Defined in:
lib/stitches/api_client_access_wrapper.rb

Class Method Summary collapse

Class Method Details

.api_key_cacheObject



67
68
69
70
71
72
# File 'lib/stitches/api_client_access_wrapper.rb', line 67

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



74
75
76
# File 'lib/stitches/api_client_access_wrapper.rb', line 74

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

.clear_api_cacheObject



63
64
65
# File 'lib/stitches/api_client_access_wrapper.rb', line 63

def self.clear_api_cache
  api_key_cache.clear if cache_enabled
end

.fetch_for_key(key, configuration) ⇒ Object



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

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

.fetch_for_key_from_cache(key, configuration) ⇒ Object



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

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

.fetch_for_key_from_db(key, configuration) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stitches/api_client_access_wrapper.rb', line 19

def self.fetch_for_key_from_db(key, configuration)
  api_client = ::ApiClient.find_by(key: key)
  return unless api_client

  unless api_client.respond_to?(:enabled?)
    logger.warn('api_keys is missing "enabled" column.  Run "rails g stitches:add_enabled_to_api_clients"')
    return api_client
  end

  unless api_client.respond_to?(:disabled_at)
    logger.warn('api_keys is missing "disabled_at" column.  Run "rails g stitches:add_disabled_at_to_api_clients"')
  end

  return api_client if api_client.enabled?

  disabled_at = api_client.respond_to?(:disabled_at) ? api_client.disabled_at : nil
  if disabled_at && disabled_at > configuration.disabled_key_leniency_in_seconds.seconds.ago
    message = "Allowing disabled ApiClient: #{api_client.name} with key #{redact_key(api_client)} disabled at #{disabled_at}"
    if disabled_at > configuration.disabled_key_leniency_error_log_threshold_in_seconds.seconds.ago
      logger.warn(message)
    else
      logger.error(message)
    end
    return api_client
  else
    logger.error("Rejecting disabled ApiClient: #{api_client.name} with key #{redact_key(api_client)}")
  end
  nil
end

.loggerObject



53
54
55
56
57
58
59
60
61
# File 'lib/stitches/api_client_access_wrapper.rb', line 53

def self.logger
  if defined?(StitchFix::Logger::LogWriter)
    StitchFix::Logger::LogWriter
  elsif defined?(Rails.logger)
    Rails.logger
  else
    ::Logger.new('/dev/null')
  end
end

.redact_key(api_client) ⇒ Object



49
50
51
# File 'lib/stitches/api_client_access_wrapper.rb', line 49

def self.redact_key(api_client)
  "*****#{api_client.key.to_s[-8..-1]}"
end