Class: SplitIoClient::Api::Splits

Inherits:
Client
  • Object
show all
Defined in:
lib/splitclient-rb/engine/api/splits.rb

Overview

Retrieves split definitions from the Split Backend

Constant Summary collapse

METRICS_PREFIX =
'splitChangeFetcher'

Constants inherited from Client

Client::RUBY_ENCODING

Instance Method Summary collapse

Methods inherited from Client

#get_api, #post_api

Constructor Details

#initialize(api_key, metrics, config) ⇒ Splits



9
10
11
12
13
# File 'lib/splitclient-rb/engine/api/splits.rb', line 9

def initialize(api_key, metrics, config)
  super(config)
  @api_key = api_key
  @metrics = metrics
end

Instance Method Details

#since(since) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/splitclient-rb/engine/api/splits.rb', line 15

def since(since)
  start = Time.now

  response = get_api("#{@config.base_uri}/splitChanges", @api_key, since: since)
  if response.success?
    result = splits_with_segment_names(response.body)

    @metrics.count(METRICS_PREFIX + '.status.' + response.status.to_s, 1)
    unless result[:splits].empty?
      @config.split_logger.log_if_debug("#{result[:splits].length} splits retrieved. since=#{since}")
    end
    @config.split_logger.log_if_transport("Split changes response: #{result.to_s}")

    latency = (Time.now - start) * 1000.0
    @metrics.time(METRICS_PREFIX + '.time', latency)

    result
  else
    @metrics.count(METRICS_PREFIX + '.status.' + response.status.to_s, 1)
    @config.logger.error("Unexpected status code while fetching splits: #{response.status}. " \
    'Check your API key and base URI')
    raise 'Split SDK failed to connect to backend to fetch split definitions'
  end
end