Class: Bandiera::Client

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

Overview

Client class for communicating with a Bandiera server.

Since:

  • 1.0.0

Defined Under Namespace

Classes: Error, ResponseError, TimeoutError

Constant Summary collapse

VERSION =

Since:

  • 1.0.0

'3.1.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Builds a new instance of Bandiera::Client

Parameters:

  • base_uri (String) (defaults to: 'http://localhost')

    The URI of the Bandiera server

  • logger (Logger) (defaults to: Logger.new($stdout))

    A logger object

  • client_name (String) (defaults to: nil)

    A client name to pass through along with the HTTP requests

Since:

  • 1.0.0



28
29
30
31
32
33
34
# File 'lib/bandiera/client.rb', line 28

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
end

Instance Attribute Details

#client_nameString

The client name passed along with HTTP requests

Returns:

  • (String)

    the current value of client_name

Since:

  • 1.0.0



15
16
17
# File 'lib/bandiera/client.rb', line 15

def client_name
  @client_name
end

#loggerLogger (readonly)

The logger object in use

Returns:

  • (Logger)

    the current value of logger

Since:

  • 1.0.0



15
16
17
# File 'lib/bandiera/client.rb', line 15

def logger
  @logger
end

#timeoutFloat

The HTTP timeout value (seconds) for requests

Returns:

  • (Float)

    the current value of timeout

Since:

  • 1.0.0



15
16
17
# File 'lib/bandiera/client.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#cache_strategy=(_) ⇒ Object

Deprecated.

This functionality was deprecated/removed in 3.0.0

Since:

  • 1.0.0



37
38
39
40
# File 'lib/bandiera/client.rb', line 37

def cache_strategy=(_)
  warn 'The caching features in Bandiera::Client have been removed as of v3.0.0, please consider using using ' \
       'the Bandiera::Middleware class shipped as part of the "bandiera-client" gem.'
end

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

Get the active/inactive state for all feature flags known on the Bandiera server

Parameters:

  • params (Hash) (defaults to: {})

    Additional parameters to pass through to the Bandiera request

Options Hash (params):

  • :user_id (String)

    A unique user identifier, or UUID (for use with percentage based feature flags)

  • :user_group (String)

    A group to assign the identify the user with (for use with group based feature flags)

Returns:

  • (Hash)

    A hash of hashes containing the feature flag active/inactive states grouped by ‘group’

Since:

  • 1.0.0



93
94
95
96
97
98
99
100
101
# File 'lib/bandiera/client.rb', line 93

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)
end

#get_feature(group, feature, params = {}, http_opts = {}) ⇒ Boolean Also known as: enabled?

Get the active/inactive state for a single feature flag

Parameters:

  • group (String)

    The group of feature flags we’re interested in

  • feature (String)

    The feature flag we want to retrieve

  • params (Hash) (defaults to: {})

    Additional parameters to pass through to the Bandiera request

Options Hash (params):

  • :user_id (String)

    A unique user identifier, or UUID (for use with percentage based feature flags)

  • :user_group (String)

    A group to assign the identify the user with (for use with group based feature flags)

Returns:

  • (Boolean)

    True/False - depending on if the feature is on or off

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
61
# File 'lib/bandiera/client.rb', line 53

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)
end

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

Get the active/inactive state for all feature flags in a group

Parameters:

  • group (String)

    The group of feature flags we’re interested in

  • params (Hash) (defaults to: {})

    Additional parameters to pass through to the Bandiera request

Options Hash (params):

  • :user_id (String)

    A unique user identifier, or UUID (for use with percentage based feature flags)

  • :user_group (String)

    A group to assign the identify the user with (for use with group based feature flags)

Returns:

  • (Hash)

    A hash of feature flag pairs. Keys are the feature flag names, values are the active/inactive states.

Since:

  • 1.0.0



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

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)
end