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 =
3600

Constants included from Version

Version::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient



76
77
78
# File 'lib/feature_flag_client/client.rb', line 76

def initialize
  validate_config
end

Class Method Details

.api_sessionObject



70
71
72
73
74
# File 'lib/feature_flag_client/client.rb', line 70

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



62
63
64
# File 'lib/feature_flag_client/client.rb', line 62

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

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

Yields:



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

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

Instance Method Details

#api_sessionObject



101
102
103
# File 'lib/feature_flag_client/client.rb', line 101

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

#cacheObject



97
98
99
# File 'lib/feature_flag_client/client.rb', line 97

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

#features(product_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/feature_flag_client/client.rb', line 80

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}", response.parsed, expires_in: CACHE_TTL)
  end

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

#versionObject



93
94
95
# File 'lib/feature_flag_client/client.rb', line 93

def version
  VERSION
end