Class: FeatureFlagClient::Client

Inherits:
Object
  • Object
show all
Includes:
Version
Defined in:
lib/feature_flag_client/client.rb

Overview

Api Client class to fetch the features

Constant Summary collapse

CACHE_KEY =
'features'
CACHE_TTL =
60

Constants included from Version

Version::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



95
96
97
# File 'lib/feature_flag_client/client.rb', line 95

def initialize
  validate_config
end

Class Method Details

.api_sessionObject



89
90
91
92
93
# File 'lib/feature_flag_client/client.rb', line 89

def self.api_session
  @api_session ||= Session.new(client_id: config.client_id, client_secret: config.client_secret) do |s|
    s.auth_service_url = config.auth_url
  end
end

.configObject



81
82
83
# File 'lib/feature_flag_client/client.rb', line 81

def self.config
  @config ||= FeatureFlagClient::Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



85
86
87
# File 'lib/feature_flag_client/client.rb', line 85

def self.configure
  yield(config) if block_given?
end

Instance Method Details

#api_sessionObject



120
121
122
# File 'lib/feature_flag_client/client.rb', line 120

def api_session
  config.api_session || self.class.api_session
end

#cacheObject



116
117
118
# File 'lib/feature_flag_client/client.rb', line 116

def cache
  @cache ||= MiniCache::Store.new
end

#features(product_name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/feature_flag_client/client.rb', line 99

def features(product_name)
  if cache.get("#{CACHE_KEY}_#{product_name}").nil?
    url = "#{config.api_base_url}#{features_pathname}"
    response = Response.from_raw(perform_request_with_query(url, productName: product_name))

    raise ClientError, response.error unless response.ok?

    cache.set("#{CACHE_KEY}_#{product_name}", FeatureCollection.new(response.parsed), expires_in: CACHE_TTL)
  end

  cache.get("#{CACHE_KEY}_#{product_name}")
end

#versionObject



112
113
114
# File 'lib/feature_flag_client/client.rb', line 112

def version
  VERSION
end