Class: SplitIoClient::SplitClient

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config = {}, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository) ⇒ SplitIoClient

Creates a new split client instance that connects to split.io API.

Parameters:

  • api_key (String)

    the API key for your split account



9
10
11
12
13
14
15
16
17
18
# File 'lib/splitclient-rb/clients/split_client.rb', line 9

def initialize(api_key, config = {}, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository)
  @config = config

  @splits_repository = splits_repository
  @segments_repository = segments_repository
  @impressions_repository = impressions_repository
  @metrics_repository = metrics_repository

  @adapter = adapter
end

Class Method Details

.sdk_versionstring

method that returns the sdk gem version

Returns:

  • (string)

    version value for this sdk



110
111
112
# File 'lib/splitclient-rb/clients/split_client.rb', line 110

def self.sdk_version
  'ruby-'+SplitIoClient::VERSION
end

Instance Method Details

#get_treatment(key, split_name, attributes = nil, split_data = nil, store_impressions = true) ⇒ String/Hash

obtains the treatment for a given feature

Parameters:

  • key (String/Hash)

    user id or hash with matching_key/bucketing_key

  • split_name (String/Array)

    name of the feature that is being validated or array of them

Returns:

  • (String/Hash)

    Treatment as String or Hash of treatments in case of array of features



43
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/splitclient-rb/clients/split_client.rb', line 43

def get_treatment(key, split_name, attributes = nil, split_data = nil, store_impressions = true)
  bucketing_key, matching_key = keys_from_key(key)
  bucketing_key = matching_key if bucketing_key.nil?

  if matching_key.nil?
    @config.logger.warn('matching_key was null for split_name: ' + split_name.to_s)
    return Treatments::CONTROL
  end

  if split_name.nil?
    @config.logger.warn('split_name was null for key: ' + key)
    return Treatments::CONTROL
  end

  start = Time.now
  result = nil

  begin
    split = split_data ? split_data : @splits_repository.get_split(split_name)

    result = if split.nil?
      Treatments::CONTROL
    else
      SplitIoClient::Engine::Parser::SplitTreatment.new(@segments_repository).call(
        { bucketing_key: bucketing_key, matching_key: matching_key }, split, attributes
      )
    end
  rescue StandardError => error
    @config.log_found_exception(__method__.to_s, error)
  end

  result = result.nil? ? Treatments::CONTROL : result

  begin
    latency = (Time.now - start) * 1000.0
    if @config.impressions_queue_size > 0 && store_impressions
      # Disable impressions if @config.impressions_queue_size == -1
      @impressions_repository.add(split_name,
        'key_name' => matching_key,
        'bucketing_key' => bucketing_key,
        'treatment' => result,
        'time' => (Time.now.to_f * 1000.0).to_i
      )
    end

    # Measure
    @adapter.metrics.time("sdk.get_treatment", latency)
  rescue StandardError => error
    @config.log_found_exception(__method__.to_s, error)
  end

  result
end

#get_treatments(key, split_names, attributes = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/splitclient-rb/clients/split_client.rb', line 20

def get_treatments(key, split_names, attributes = nil)
  bucketing_key, matching_key = keys_from_key(key)
  bucketing_key = matching_key if bucketing_key.nil?

  treatments =
    @splits_repository.get_splits(split_names).each_with_object({}) do |(name, data), memo|
      memo.merge!(name => get_treatment(key, name, attributes, data, false))
    end

  if @config.impressions_queue_size > 0
    @impressions_repository.add_bulk(matching_key, bucketing_key, treatments, (Time.now.to_f * 1000.0).to_i)
  end

  treatments
end

#keys_from_key(key) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/splitclient-rb/clients/split_client.rb', line 97

def keys_from_key(key)
  case key.class.to_s
  when 'Hash'
    key.values_at(:bucketing_key, :matching_key)
  when 'String'
    [key, key]
  end
end