Class: RooOnRails::Concerns::RequireApiKey::ClientApiKeys

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/roo_on_rails/concerns/require_api_key.rb

Constant Summary collapse

CLIENT_KEY_NAME_SUFFIX_REGEX =
/_CLIENT_KEY\Z/

Instance Method Summary collapse

Constructor Details

#initializeClientApiKeys

Returns a new instance of ClientApiKeys.



77
78
79
80
81
82
# File 'lib/roo_on_rails/concerns/require_api_key.rb', line 77

def initialize
  @cache = ENV.select { |key| key =~ CLIENT_KEY_NAME_SUFFIX_REGEX }
              .map { |k, v| [service_name(k), parse_client_keys(v)] }
              .to_h
              .freeze
end

Instance Method Details

#valid?(service_name, client_key) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/roo_on_rails/concerns/require_api_key.rb', line 84

def valid?(service_name, client_key)
  return false if service_name.to_s.empty? || client_key.to_s.empty?

  client_keys = @cache[normalize(service_name)]
  return false unless client_keys
  client_keys.include?(client_key)
end