Class: SplitIoClient::SplitConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/split_config.rb

Overview

This class manages configuration options for the split client library. If not custom configuration is required the default configuration values will be used

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ type

Constructor for creating custom split client config

Parameters:

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

    optional hash with configuration options

Options Hash (opts):

  • :base_uri (String) — default: "https://sdk.split.io/api/"

    The base URL for split API end points

  • :read_timeout (Int) — default: 10

    The read timeout for network connections in seconds.

  • :connection_timeout (Int) — default: 2

    The connect timeout for network connections in seconds.

  • :local_store (Object)

    A cache store for the Faraday HTTP caching library. Defaults to the Rails cache in a Rails environment, or a thread-safe in-memory store otherwise.

  • :features_refresh_rate (Int)

    The SDK polls Split servers for changes to feature roll-out plans. This parameter controls this polling period in seconds.

  • :segments_refresh_rate (Int)
  • :metrics_refresh_rate (Int)
  • :impressions_refresh_rate (Int)
  • :logger (Object)

    a logger to user for messages from the client. Defaults to stdout

  • :debug_enabled (Boolean) — default: false

    The value for the debug flag



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/splitclient-rb/split_config.rb', line 26

def initialize(opts = {})
  @base_uri = (opts[:base_uri] || SplitConfig.default_base_uri).chomp('/')
  @local_store = opts[:local_store] || SplitConfig.default_local_store
  @connection_timeout = opts[:connection_timeout] || SplitConfig.default_connection_timeout
  @read_timeout = opts[:read_timeout] || SplitConfig.default_read_timeout
  @features_refresh_rate = opts[:features_refresh_rate] || SplitConfig.default_features_refresh_rate
  @segments_refresh_rate = opts[:segments_refresh_rate] || SplitConfig.default_segments_refresh_rate
  @metrics_refresh_rate = opts[:metrics_refresh_rate] || SplitConfig.default_metrics_refresh_rate
  @impressions_refresh_rate = opts[:impressions_refresh_rate] || SplitConfig.default_impressions_refresh_rate
  @logger = opts[:logger] || SplitConfig.default_logger
  @debug_enabled = opts[:debug_enabled] || SplitConfig.default_debug
  @machine_name = SplitConfig.get_hostname
  @machine_ip = SplitConfig.get_ip
end

Instance Attribute Details

#base_uriString (readonly)

The base URL for split API end points

Returns:

  • (String)

    The configured base URL for the split API end points



45
46
47
# File 'lib/splitclient-rb/split_config.rb', line 45

def base_uri
  @base_uri
end

#connection_timeoutInt (readonly)

The connection timeout for network connections in seconds.

Returns:

  • (Int)

    The connect timeout in seconds.



64
65
66
# File 'lib/splitclient-rb/split_config.rb', line 64

def connection_timeout
  @connection_timeout
end

#debug_enabledBoolean (readonly)

The boolean that represents the state of the debug log level

Returns:

  • (Boolean)

    The value for the debug flag



77
78
79
# File 'lib/splitclient-rb/split_config.rb', line 77

def debug_enabled
  @debug_enabled
end

#features_refresh_rateObject (readonly)

Returns the value of attribute features_refresh_rate.



82
83
84
# File 'lib/splitclient-rb/split_config.rb', line 82

def features_refresh_rate
  @features_refresh_rate
end

#impressions_refresh_rateObject (readonly)

Returns the value of attribute impressions_refresh_rate.



85
86
87
# File 'lib/splitclient-rb/split_config.rb', line 85

def impressions_refresh_rate
  @impressions_refresh_rate
end

#local_storeObject (readonly)

The store for the Faraday HTTP caching library. Stores should respond to ‘read’, ‘write’ and ‘delete’ requests.

Returns:

  • (Object)

    The configured store for the Faraday HTTP caching library.



52
53
54
# File 'lib/splitclient-rb/split_config.rb', line 52

def local_store
  @local_store
end

#loggerLogger (readonly)

The configured logger. The client library uses the log to print warning and error messages.

Returns:

  • (Logger)

    The configured logger



71
72
73
# File 'lib/splitclient-rb/split_config.rb', line 71

def logger
  @logger
end

#machine_ipObject (readonly)

Returns the value of attribute machine_ip.



79
80
81
# File 'lib/splitclient-rb/split_config.rb', line 79

def machine_ip
  @machine_ip
end

#machine_nameObject (readonly)

Returns the value of attribute machine_name.



80
81
82
# File 'lib/splitclient-rb/split_config.rb', line 80

def machine_name
  @machine_name
end

#metrics_refresh_rateObject (readonly)

Returns the value of attribute metrics_refresh_rate.



84
85
86
# File 'lib/splitclient-rb/split_config.rb', line 84

def metrics_refresh_rate
  @metrics_refresh_rate
end

#read_timeoutInt (readonly)

The read timeout for network connections in seconds.

Returns:

  • (Int)

    The timeout in seconds.



58
59
60
# File 'lib/splitclient-rb/split_config.rb', line 58

def read_timeout
  @read_timeout
end

#segments_refresh_rateObject (readonly)

Returns the value of attribute segments_refresh_rate.



83
84
85
# File 'lib/splitclient-rb/split_config.rb', line 83

def segments_refresh_rate
  @segments_refresh_rate
end

Class Method Details

.defaultConfig

The default split client configuration

Returns:

  • (Config)

    The default split client configuration.



91
92
93
# File 'lib/splitclient-rb/split_config.rb', line 91

def self.default
  SplitConfig.new
end

.default_base_uristring

The default base uri for api calls

Returns:

  • (string)

    The default base uri



99
100
101
# File 'lib/splitclient-rb/split_config.rb', line 99

def self.default_base_uri
  'https://sdk.split.io/api/'
end

.default_connection_timeoutint

The default connection timeout value

Returns:

  • (int)


120
121
122
# File 'lib/splitclient-rb/split_config.rb', line 120

def self.default_connection_timeout
  5
end

.default_debugboolean

The default debug value

Returns:

  • (boolean)


152
153
154
# File 'lib/splitclient-rb/split_config.rb', line 152

def self.default_debug
  false
end

.default_features_refresh_rateObject



124
125
126
# File 'lib/splitclient-rb/split_config.rb', line 124

def self.default_features_refresh_rate
  30
end

.default_impressions_refresh_rateObject



136
137
138
# File 'lib/splitclient-rb/split_config.rb', line 136

def self.default_impressions_refresh_rate
  60
end

.default_local_storeLocalStore

Returns configuration value for local cache store.

Returns:

  • (LocalStore)

    configuration value for local cache store



104
105
106
# File 'lib/splitclient-rb/split_config.rb', line 104

def self.default_local_store
  defined?(Rails) && Rails.respond_to?(:cache) ? Rails.cache : LocalStore.new
end

.default_loggerobject

The default logger object

Returns:

  • (object)


144
145
146
# File 'lib/splitclient-rb/split_config.rb', line 144

def self.default_logger
  Logger.new($stdout)
end

.default_metrics_refresh_rateObject



132
133
134
# File 'lib/splitclient-rb/split_config.rb', line 132

def self.default_metrics_refresh_rate
  60
end

.default_read_timeoutint

The default read timeout value

Returns:

  • (int)


112
113
114
# File 'lib/splitclient-rb/split_config.rb', line 112

def self.default_read_timeout
  5
end

.default_segments_refresh_rateObject



128
129
130
# File 'lib/splitclient-rb/split_config.rb', line 128

def self.default_segments_refresh_rate
  60
end

.get_hostnamestring

gets the hostname where the sdk gem is running

Returns:

  • (string)


170
171
172
173
174
175
176
177
# File 'lib/splitclient-rb/split_config.rb', line 170

def self.get_hostname
  begin
    Socket.gethostname
  rescue
    #unable to get hostname
    'localhost'
  end
end

.get_ipstring

gets the ip where the sdk gem is running

Returns:

  • (string)


183
184
185
186
187
188
189
190
# File 'lib/splitclient-rb/split_config.rb', line 183

def self.get_ip
  begin
    Socket::getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET)[0][3]
  rescue
    #unable to get local ip
    '127.0.0.0'
  end
end

Instance Method Details

#log_found_exception(caller, exn) ⇒ void

This method returns an undefined value.

custom logger of exceptions



160
161
162
163
164
# File 'lib/splitclient-rb/split_config.rb', line 160

def log_found_exception(caller, exn)
  error_traceback = "#{exn.inspect} #{exn}\n\t#{exn.backtrace.join("\n\t")}"
  error = "[splitclient-rb] Unexpected exception in #{caller}: #{error_traceback}"
  @logger.error(error)
end