Class: Molasses::Client
- Inherits:
-
Object
- Object
- Molasses::Client
- Defined in:
- lib/molasses.rb
Instance Method Summary collapse
- #experiment_started(key, user = nil, additional_details = {}) ⇒ Object
- #experiment_success(key, user = nil, additional_details = {}) ⇒ Object
-
#initialize(api_key, opts = {}) ⇒ Client
constructor
A new instance of Client.
- #is_active(key, user = {}) ⇒ Object
- #stop ⇒ Object
- #track(key, user = nil, additional_details = {}) ⇒ Object
Constructor Details
#initialize(api_key, opts = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/molasses.rb', line 10 def initialize(api_key, opts = {}) @api_key = api_key @send_events = opts[:auto_send_events] || false @base_url = opts[:base_url] || "https://sdk.molasses.app/v1" @logger = opts[:logger] || get_default_logger @conn = Faraday.new( url: @base_url, headers: { "Content-Type" => "application/json", "Authorization" => "Bearer #{@api_key}", }, ) @feature_cache = {} @initialized = {} fetch_features @timer = Workers::PeriodicTimer.new(15) do fetch_features end end |
Instance Method Details
#experiment_started(key, user = nil, additional_details = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/molasses.rb', line 60 def experiment_started(key, user = nil, additional_details = {}) if !@initialized || user == nil || !user.include?("id") return false end unless @feature_cache.include?(key) @logger.info "Warning - feature flag #{key} not set in environment" return false end feature = @feature_cache[key] result = is_active(feature, user) send_event({ "event" => "experiment_started", "tags" => user.include?("params") ? user["params"].merge(additional_details) : additional_details, "userId" => user["id"], "featureId" => feature["id"], "featureName" => key, "testType" => result ? "experiment" : "control", }) end |
#experiment_success(key, user = nil, additional_details = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/molasses.rb', line 80 def experiment_success(key, user = nil, additional_details = {}) if !@initialized || user == nil || !user.include?("id") return false end unless @feature_cache.include?(key) @logger.info "Warning - feature flag #{key} not set in environment" return false end feature = @feature_cache[key] result = is_active(feature, user) send_event({ "event" => "experiment_success", "tags" => user.include?("params") ? user["params"].merge(additional_details) : additional_details, "userId" => user["id"], "featureId" => feature["id"], "featureName" => key, "testType" => result ? "experiment" : "control", }) end |
#is_active(key, user = {}) ⇒ Object
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/molasses.rb', line 31 def is_active(key, user = {}) unless @initialized return false end if @feature_cache.include?(key) feature = @feature_cache[key] result = user_active(feature, user) if @send_events && user && user.include?("id") send_event({ "event" => "experiment_started", "tags" => user["params"], "userId" => user["id"], "featureId" => feature["id"], "featureName" => key, "testType" => result ? "experiment" : "control", }) end return result else @logger.info "Warning - feature flag #{key} not set in environment" return false end end |
#stop ⇒ Object
56 57 58 |
# File 'lib/molasses.rb', line 56 def stop @timer.cancel end |
#track(key, user = nil, additional_details = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/molasses.rb', line 100 def track(key, user = nil, additional_details = {}) if user == nil || !user.include?("id") return false end send_event({ "event" => key, "tags" => user.include?("params") ? user["params"].merge(additional_details) : additional_details, "userId" => user["id"], }) end |