Class: LaunchDarkly::LDClient
- Inherits:
-
Object
- Object
- LaunchDarkly::LDClient
- Defined in:
- lib/ldclient-rb/ldclient.rb
Overview
A client for the LaunchDarkly API. Client instances are thread-safe. Users should create a single client instance for the lifetime of the application.
Instance Method Summary collapse
- #flush ⇒ Object
- #get_flag?(key, user, default = false) ⇒ Boolean
-
#identify(user) ⇒ Object
Registers the user.
-
#initialize(api_key, config = Config.default) ⇒ LDClient
constructor
Creates a new client instance that connects to LaunchDarkly.
- #is_offline? ⇒ Boolean
- #set_offline ⇒ Object
- #set_online ⇒ Object
-
#toggle?(key, user, default = false) ⇒ Boolean
Calculates the value of a feature flag for a given user.
-
#track(event_name, user, data) ⇒ void
Tracks that a user performed an event.
Constructor Details
#initialize(api_key, config = Config.default) ⇒ LDClient
Creates a new client instance that connects to LaunchDarkly. A custom configuration parameter can also supplied to specify advanced options, but for most use cases, the default configuration is appropriate.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ldclient-rb/ldclient.rb', line 30 def initialize(api_key, config = Config.default) @queue = Queue.new @api_key = api_key @config = config @client = Faraday.new do |builder| builder.use :http_cache, store: @config.store builder.adapter :net_http_persistent end @offline = false @worker = create_worker() end |
Instance Method Details
#flush ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ldclient-rb/ldclient.rb', line 44 def flush() events = [] num_events = @queue.length() begin num_events.times do events << @queue.pop(true) end rescue end if !events.empty?() res = log_timings("Flush events") { next @client.post (@config.base_uri + "/api/events/bulk") do |req| req.headers['Authorization'] = 'api_key ' + @api_key req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION req.headers['Content-Type'] = 'application/json' req.body = events.to_json req..timeout = @config.read_timeout req..open_timeout = @config.connect_timeout end } if res.status != 200 @config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}") end end end |
#get_flag?(key, user, default = false) ⇒ Boolean
87 88 89 |
# File 'lib/ldclient-rb/ldclient.rb', line 87 def get_flag?(key, user, default=false) toggle?(key, user, default) end |
#identify(user) ⇒ Object
Registers the user
158 159 160 |
# File 'lib/ldclient-rb/ldclient.rb', line 158 def identify(user) add_event({:kind => 'identify', :key => user[:key], :user => user}) end |
#is_offline? ⇒ Boolean
170 171 172 |
# File 'lib/ldclient-rb/ldclient.rb', line 170 def is_offline?() return @offline end |
#set_offline ⇒ Object
162 163 164 |
# File 'lib/ldclient-rb/ldclient.rb', line 162 def set_offline() @offline = true end |
#set_online ⇒ Object
166 167 168 |
# File 'lib/ldclient-rb/ldclient.rb', line 166 def set_online() @offline = false end |
#toggle?(key, user, default = false) ⇒ Boolean
Calculates the value of a feature flag for a given user. At a minimum, the user hash should contain a :key .
For authenticated users, the :key should be the unique identifier for your user. For anonymous users, the :key should be a session identifier or cookie. In either case, the only requirement is that the key is unique to a user.
You can also pass IP addresses and country codes in the user hash.
Countries should be sent as ISO 3166-1 alpha-2 codes.
The user hash can contain arbitrary custom attributes stored in a :custom sub-hash:
Attribute values in the custom hash can be integers, booleans, strings, or lists of integers, booleans, or strings.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ldclient-rb/ldclient.rb', line 121 def toggle?(key, user, default=false) begin if @offline return default end value = get_flag_int(key, user, default) add_event({:kind => 'feature', :key => key, :user => user, :value => value}) LDNewRelic.annotate_transaction(key, value) return value rescue StandardError => error @config.logger.error("[LDClient] Unhandled exception in get_flag: (#{error.class.name}) #{error.to_s}\n\t#{error.backtrace.join("\n\t")}") default end end |
#track(event_name, user, data) ⇒ void
This method returns an undefined value.
Tracks that a user performed an event
182 183 184 |
# File 'lib/ldclient-rb/ldclient.rb', line 182 def track(event_name, user, data) add_event({:kind => 'custom', :key => event_name, :user => user, :data => data }) end |