Class: Kilden::Decide Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kilden/client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

One-attempt /decide lookups (spec §8.2: a flag answer that arrives after a retry budget is useless — return the default instead).

Instance Method Summary collapse

Constructor Details

#initialize(write_key:, host:, timeout:, transport:, logger:) ⇒ Decide

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Decide.



347
348
349
350
351
352
# File 'lib/kilden/client.rb', line 347

def initialize(write_key:, host:, timeout:, transport:, logger:)
  @write_key = write_key
  @url = "#{host.chomp('/')}/decide"
  @transport = transport || Transport::NetHttp.new(timeout: timeout)
  @logger = logger
end

Instance Method Details

#flags_for(distinct_id, person_properties) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/kilden/client.rb', line 354

def flags_for(distinct_id, person_properties)
  request = { "write_key" => @write_key, "distinct_id" => distinct_id }
  request["person_properties"] = person_properties unless person_properties.nil?

  response = @transport.post(@url, JSON.generate(request),
                             "Content-Type" => "application/json",
                             "User-Agent" => "kilden-ruby/#{VERSION}")
  unless response.status == 200
    reason = response.network_error? ? response.error&.class : "HTTP #{response.status}"
    @logger.warn("kilden: /decide failed (#{reason}); using defaults")
    return nil
  end

  flags = JSON.parse(response.body)["flags"]
  flags.is_a?(Hash) ? flags : nil
rescue JSON::ParserError
  @logger.warn("kilden: /decide returned a malformed body; using defaults")
  nil
end