Class: Hackle::Client

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

Overview

A client for Hackle API.

Instance Method Summary collapse

Constructor Details

#initialize(config:, workspace_fetcher:, event_processor:, decider:) ⇒ Client

Initializes a Hackle client.

Parameters:



37
38
39
40
41
42
# File 'lib/hackle/client.rb', line 37

def initialize(config:, workspace_fetcher:, event_processor:, decider:)
  @logger = config.logger
  @workspace_fetcher = workspace_fetcher
  @event_processor = event_processor
  @decider = decider
end

Instance Method Details

#closeObject

Shutdown the background task and release the resources used for the background task.



109
110
111
112
# File 'lib/hackle/client.rb', line 109

def close
  @workspace_fetcher.stop!
  @event_processor.stop!
end

#track(event_key:, user_id:, value: nil) ⇒ Object

Records the events performed by the user.

Parameters:

  • event_key (String)

    The unique key of the events.

  • user_id (String)

    The identifier of user that performed the vent.

  • value (Float) (defaults to: nil)

    Additional numeric value of the events (e.g. purchase_amount, api_latency, etc.)



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hackle/client.rb', line 92

def track(event_key:, user_id:, value: nil)

  return if event_key.nil?
  return if user_id.nil?

  workspace = @workspace_fetcher.fetch
  return if workspace.nil?

  event_type = workspace.get_event_type(event_type_key: event_key)

  track_event = Event::Track.new(user_id: user_id, event_type: event_type, value: value)
  @event_processor.process(event: track_event)
end

#variation(experiment_key:, user_id:, default_variation: 'A') ⇒ String

Decide the variation to expose to the user for experiment.

This method return the control variation ‘A’ if:

  • The experiment key is invalid

  • The experiment has not started yet

  • The user is not allocated to the experiment

  • The decided variation has been dropped

Parameters:

  • experiment_key (Integer)

    The unique key of the experiment.

  • user_id (String)

    The identifier of your customer. (e.g. user_email, account_id, decide_id, etc.)

  • default_variation (String) (defaults to: 'A')

    The default variation of the experiment.

Returns:

  • (String)

    The decided variation for the user, or default variation



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
# File 'lib/hackle/client.rb', line 59

def variation(experiment_key:, user_id:, default_variation: 'A')

  return default_variation if experiment_key.nil?
  return default_variation if user_id.nil?

  workspace = @workspace_fetcher.fetch
  return default_variation if workspace.nil?

  experiment = workspace.get_experiment(experiment_key: experiment_key)
  return default_variation if experiment.nil?

  decision = @decider.decide(experiment: experiment, user_id: user_id)
  case decision
  when Decision::NotAllocated
    default_variation
  when Decision::ForcedAllocated
    decision.variation_key
  when Decision::NaturalAllocated
    exposure_event = Event::Exposure.new(user_id: user_id, experiment: experiment, variation: decision.variation)
    @event_processor.process(event: exposure_event)
    decision.variation.key
  else
    default_variation
  end
end