Class: Bandiera::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bandiera/client.rb,
lib/bandiera/client/version.rb

Constant Summary collapse

CACHE_STRATEGIES =
[:single_feature, :group, :all]
VERSION =
'2.2.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = 'http://localhost', logger = Logger.new($stdout), client_name = nil) ⇒ Client

Returns a new instance of Client.


15
16
17
18
19
20
21
22
23
24
# File 'lib/bandiera/client.rb', line 15

def initialize(base_uri = 'http://localhost', logger = Logger.new($stdout), client_name = nil)
  @base_uri       = base_uri
  @base_uri       << '/api' unless @base_uri.match(/\/api$/)
  @logger         = logger
  @timeout        = 0.2 # 0.4s (0.2 + 0.2) default timeout
  @client_name    = client_name
  @cache          = Moneta.new(:LRUHash, expires: true)
  @cache_ttl      = 5 # 5 seconds
  @cache_strategy = :group
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache


13
14
15
# File 'lib/bandiera/client.rb', line 13

def cache
  @cache
end

#cache_strategyObject

Returns the value of attribute cache_strategy


13
14
15
# File 'lib/bandiera/client.rb', line 13

def cache_strategy
  @cache_strategy
end

#cache_ttlObject

Returns the value of attribute cache_ttl


12
13
14
# File 'lib/bandiera/client.rb', line 12

def cache_ttl
  @cache_ttl
end

#client_nameObject

Returns the value of attribute client_name


12
13
14
# File 'lib/bandiera/client.rb', line 12

def client_name
  @client_name
end

#loggerObject (readonly)

Returns the value of attribute logger


13
14
15
# File 'lib/bandiera/client.rb', line 13

def logger
  @logger
end

#timeoutObject

Returns the value of attribute timeout


12
13
14
# File 'lib/bandiera/client.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#enabled?(group, feature, params = {}, http_opts = {}) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bandiera/client.rb', line 33

def enabled?(group, feature, params = {}, http_opts = {})
  cache_key = build_cache_key(group, feature, params)

  unless cache.key?(cache_key)
    case cache_strategy
    when :single_feature then get_feature(group, feature, params, http_opts)
    when :group          then get_features_for_group(group, params, http_opts)
    when :all            then get_all
    end
  end

  cache.fetch(cache_key)
end

#get_all(params = {}, http_opts = {}) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bandiera/client.rb', line 71

def get_all(params = {}, http_opts = {})
  path             = '/v2/all'
  default_response = {}
  error_msg_prefix = "[Bandiera::Client#get_all] '#{params}'"

  logger.debug "[Bandiera::Client#get_all] calling #{path} with params: #{params}"

  get_and_handle_exceptions(path, params, http_opts, default_response, error_msg_prefix) do |group_hash|
    group_hash.each do |group, feature_hash|
      store_feature_hash_in_cache(group, params, feature_hash)
    end
  end
end

#get_feature(group, feature, params = {}, http_opts = {}) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bandiera/client.rb', line 47

def get_feature(group, feature, params = {}, http_opts = {})
  path             = "/v2/groups/#{group}/features/#{feature}"
  default_response = false
  error_msg_prefix = "[Bandiera::Client#get_feature] '#{group} / #{feature} / #{params}'"

  logger.debug "[Bandiera::Client#get_feature] calling #{path} with params: #{params}"

  get_and_handle_exceptions(path, params, http_opts, default_response, error_msg_prefix) do |value|
    store_value_in_cache(group, feature, params, value)
  end
end

#get_features_for_group(group, params = {}, http_opts = {}) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bandiera/client.rb', line 59

def get_features_for_group(group, params = {}, http_opts = {})
  path             = "/v2/groups/#{group}/features"
  default_response = {}
  error_msg_prefix = "[Bandiera::Client#get_features_for_group] '#{group} / #{params}'"

  logger.debug "[Bandiera::Client#get_features_for_group] calling #{path} with params: #{params}"

  get_and_handle_exceptions(path, params, http_opts, default_response, error_msg_prefix) do |feature_hash|
    store_feature_hash_in_cache(group, params, feature_hash)
  end
end