Class: Hackle::Client
- Inherits:
-
Object
- Object
- Hackle::Client
- Defined in:
- lib/hackle-ruby-sdk/client.rb
Overview
A client for Hackle API.
Class Method Summary collapse
-
.create(sdk_key, config = Config.new) ⇒ Client
Instantiates a Hackle client.
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
A new instance of Client.
-
#track(event_key, user_id, value = nil) ⇒ Object
Records the events performed by the user.
-
#variation(experiment_key, user_id, default_variation = 'A') ⇒ String
Decide the variation to expose to the user for experiment.
Constructor Details
#initialize(config, workspace_fetcher, event_processor, decider) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 |
# File 'lib/hackle-ruby-sdk/client.rb', line 9 def initialize(config, workspace_fetcher, event_processor, decider) @logger = config.logger @workspace_fetcher = workspace_fetcher @event_processor = event_processor @decider = decider end |
Class Method Details
.create(sdk_key, config = Config.new) ⇒ Client
Instantiates a Hackle client.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hackle-ruby-sdk/client.rb', line 93 def self.create(sdk_key, config = Config.new) sdk_info = SdkInfo.new(sdk_key) http_workspace_fetcher = HttpWorkspaceFetcher.new(config, sdk_info) polling_workspace_fetcher = PollingWorkspaceFetcher.new(config, http_workspace_fetcher) event_dispatcher = EventDispatcher.new(config, sdk_info) event_processor = EventProcessor.new(config, event_dispatcher) polling_workspace_fetcher.start! event_processor.start! Client.new(config, polling_workspace_fetcher, event_processor, Decider.new) end |
Instance Method Details
#close ⇒ Object
Shutdown the background task and release the resources used for the background task.
80 81 82 83 |
# File 'lib/hackle-ruby-sdk/client.rb', line 80 def close @workspace_fetcher.stop! @event_processor.stop! end |
#track(event_key, user_id, value = nil) ⇒ Object
Records the events performed by the user.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hackle-ruby-sdk/client.rb', line 63 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_key) return if event_type.nil? @event_processor.process(Event::Track.new(user_id, event_type, value)) 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hackle-ruby-sdk/client.rb', line 31 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) return default_variation if experiment.nil? decision = @decider.decide(experiment, user_id) case decision when Decision::NotAllocated default_variation when Decision::ForcedAllocated decision.variation_key when Decision::NaturalAllocated @event_processor.process(Event::Exposure.new(user_id, experiment, decision.variation)) decision.variation.key else default_variation end end |