Class: LaunchDarkly::LDClient

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • api_key (String)

    the API key for your LaunchDarkly account

  • config (Config) (defaults to: Config.default)

    an optional client configuration object



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

#flushObject



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.options.timeout = @config.read_timeout          
        req.options.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

Returns:

  • (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

Parameters:

  • The (Hash)

    user to register



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

Returns:

  • (Boolean)


170
171
172
# File 'lib/ldclient-rb/ldclient.rb', line 170

def is_offline?()
  return @offline
end

#set_offlineObject



162
163
164
# File 'lib/ldclient-rb/ldclient.rb', line 162

def set_offline()
  @offline = true
end

#set_onlineObject



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.

Examples:

Basic user hash

{:key => "[email protected]"}

More complete user hash

{:key => "[email protected]", :ip => "127.0.0.1", :country => "US"}

A user hash with custom attributes

{:key => "[email protected]", :custom => {:customer_rank => 1000, :groups => ["google", "microsoft"]}} 

Parameters:

  • key (String)

    the unique feature key for the feature flag, as shown on the LaunchDarkly dashboard

  • user (Hash)

    a hash containing parameters for the end user requesting the flag

  • default=false (Boolean)

    the default value of the flag

Returns:

  • (Boolean)

    whether or not the flag should be enabled, or the default value if the flag is disabled on the LaunchDarkly control panel



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

Parameters:

  • event_name (String)

    The name of the event

  • user (Hash)

    The user that performed the event. This should be the same user hash used in calls to #get_flag?

  • data (Hash)

    A hash containing any additional data associated with the 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