Class: Hackle::Client

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

Overview

A client for Hackle API.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • sdk_key (String)

    The SDK key of your Hackle environment

  • config (Config) (defaults to: Config.new)

    An optional client configuration

Returns:

  • (Client)

    The Hackle client instance.



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

#closeObject

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.

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.)



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

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



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