Class: Keystok::Client

Inherits:
Object
  • Object
show all
Includes:
AESCrypto, Cache
Defined in:
lib/keystok/client.rb

Overview

Keystok client class

Constant Summary collapse

API_HOST =
'https://api.keystok.com'
AUTH_HOST =
'https://keystok.com'
DEFAULT_CONFIG =
{ eager_fetching: true }
REQUEST_TRY_COUNT =
3

Constants included from AESCrypto

AESCrypto::PREFIX

Instance Method Summary collapse

Methods included from Cache

#cache_file_exist?, #cache_file_path, #cache_stream, #load_cache, #write_cache

Methods included from AESCrypto

#cipher, #decipher, #decrypt_key

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



24
25
26
27
# File 'lib/keystok/client.rb', line 24

def initialize(config = {})
  @config = DEFAULT_CONFIG.merge(decode_config(config))
  @keys_store = {}
end

Instance Method Details

#access_tokenObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/keystok/client.rb', line 29

def access_token
  @access_token ||= begin
    fail Error::ConfigError, 'SDK not configured' if @config[:rt].nil?
    refresh_token = OAuth2::AccessToken.new(oauth_client, nil,
                                            refresh_token:
                                              @config[:rt])
    access_token = nil
    REQUEST_TRY_COUNT.times do |try_count|
      begin
        access_token = refresh_token.refresh!
        break
      rescue Faraday::Error::ClientError => exception
        Keystok.logger.warn(
          "Exception during token refresh: #{exception.message}")
        raise if try_count == (REQUEST_TRY_COUNT - 1)
      end
    end
    access_token
  end
end

#configured?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/keystok/client.rb', line 50

def configured?
  check_config
  true
rescue Error::ConfigError
  false
end

#get(key_name_or_symbol, force_reload = false) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/keystok/client.rb', line 57

def get(key_name_or_symbol, force_reload = false)
  key_name = key_name_or_symbol.to_s
  if force_reload || @config[:volatile] || @keys_store[key_name].nil?
    load_keys(key_name_or_symbol)
  end
  @keys_store[key_name]
end

#keys(force_reload = false) ⇒ Object



65
66
67
68
# File 'lib/keystok/client.rb', line 65

def keys(force_reload = false)
  load_keys if force_reload || @config[:volatile] || @keys_store.empty?
  @keys_store
end