Class: Hackle::Client
- Inherits:
-
Object
- Object
- Hackle::Client
- Defined in:
- lib/hackle/client.rb
Overview
A client for Hackle API.
Instance Method Summary collapse
-
#close ⇒ Object
Shutdown the background task and release the resources used for the background task.
-
#initialize(config:, workspace_fetcher:, event_processor:, decider:) ⇒ Client
constructor
Initializes a Hackle client.
-
#track(event:, user:) ⇒ Object
Records the event that occurred by the user.
-
#variation(experiment_key:, user:, default_variation: 'A') ⇒ String
Decide the variation to expose to the user for experiment.
Constructor Details
#initialize(config:, workspace_fetcher:, event_processor:, decider:) ⇒ Client
Initializes a Hackle client.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hackle/client.rb', line 39 def initialize(config:, workspace_fetcher:, event_processor:, decider:) @logger = config.logger # @type [PollingWorkspaceFetcher] @workspace_fetcher = workspace_fetcher # @type [EventProcessor] @event_processor = event_processor # @type [Decider] @decider = decider end |
Instance Method Details
#close ⇒ Object
Shutdown the background task and release the resources used for the background task.
122 123 124 125 |
# File 'lib/hackle/client.rb', line 122 def close @workspace_fetcher.stop! @event_processor.stop! end |
#track(event:, user:) ⇒ Object
Records the event that occurred by the user.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/hackle/client.rb', line 103 def track(event:, user:) return if event.nil? || !event.is_a?(Event) || !event.valid? return if user.nil? || !user.is_a?(User) || !user.valid? workspace = @workspace_fetcher.fetch return if workspace.nil? event_type = workspace.get_event_type(event_type_key: event.key) track_event = UserEvent::Track.new(user: user, event_type: event_type, event: event) @event_processor.process(event: track_event) rescue => e @logger.error { "Unexpected error while tracking event: #{e.inspect}" } end |
#variation(experiment_key:, user:, 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
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/hackle/client.rb', line 67 def variation(experiment_key:, user:, default_variation: 'A') return default_variation if experiment_key.nil? || !experiment_key.is_a?(Integer) return default_variation if user.nil? || !user.is_a?(User) || !user.valid? 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: user) case decision when Decision::NotAllocated default_variation when Decision::ForcedAllocated decision.variation_key when Decision::NaturalAllocated exposure_event = UserEvent::Exposure.new(user: user, experiment: experiment, variation: decision.variation) @event_processor.process(event: exposure_event) decision.variation.key else default_variation end rescue => e @logger.error { "Unexpected error while deciding variation for experiment[#{experiment_key}]. Returning default variation[#{default_variation}]: #{e.inspect}" } default_variation end |