Class: Whop_sdk::Experiments::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/whop_sdk/experiments/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#activate(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Starts (or resumes) an experiment or feature flag so evaluation begins serving it. Activating a draft stamps started_at; resuming a paused experiment keeps the original start. Only drafts and paused experiments can be activated. Requires the corresponding experiment permission on the owning account, or Whop internal access for internal experiments.

Examples:

client.experiments.activate(id: "id")

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :account_id (String, nil)

Returns:



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/whop_sdk/experiments/client.rb', line 301

def activate(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request_data = Whop_sdk::Experiments::Types::ActivateExperimentsRequest.new(params).to_h
  non_body_param_names = %w[id account_id]
  body = request_data.except(*non_body_param_names)

  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "experiments/#{URI.encode_uri_component(params[:id].to_s)}/activate",
    query: query_params,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#create(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Creates a draft experiment for the specified account. Use internal for a Whop platform experiment.

Examples:

client.experiments.create(
  account_id: "internal",
  flag_key: "checkout_redesign_v2"
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/whop_sdk/experiments/client.rb', line 96

def create(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "experiments",
    body: Whop_sdk::Experiments::Types::CreateExperimentsRequest.new(params).to_h,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#end_(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Concludes the experiment and records required findings. Pass winning_arm to serve the winning treatment to everyone; omit it when control won. Ended experiments cannot restart, but may be ended again to correct the winner. Requires experiment:manage on the account, or internal access for platform experiments.

Examples:

client.experiments.end_(
  id: "id",
  findings: "Treatment lifted signups 12%, shipping it to everyone."
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :account_id (String, nil)

Returns:



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/whop_sdk/experiments/client.rb', line 353

def end_(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request_data = Whop_sdk::Experiments::Types::EndExperimentsRequest.new(params).to_h
  non_body_param_names = %w[id account_id]
  body = request_data.except(*non_body_param_names)

  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "experiments/#{URI.encode_uri_component(params[:id].to_s)}/end",
    query: query_params,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#exposures(request_options: {}, **params) ⇒ Whop_sdk::Experiments::Types::ExposuresExperimentsResponse

Evaluates and records an exposure without requiring authentication. When credentials resolve, their authentication method, API key ID, and signed-in user ID are recorded on the exposure event. Pass subject for bucketing identity and account_id for experiment ownership.

Pass flag_key to check a single flag, or omit it to fetch active flags in the account and related resource scope. Internal anonymous callers may use the x-whop-anonymous-id header or ajs_anonymous_id cookie; explicit subject[anonymous_id] takes precedence.

Assignments use exactly the configured bucket_by: subject[user_id], subject[account_id], or subject[anonymous_id]. Internal user experiments derive identity from the signed-in session. Missing the required identity fails single evaluation and omits the experiment from batch evaluation. Subjects outside all treatment ranges receive control.

Pass subject[account_id] to enable account-level targeting rules. Pass properties as a JSON object to supply the values that property targeting conditions match against.

Pass log_exposure=false to read an assignment without recording an exposure, for a client that caches assignments up front and records the exposure when the arm is actually rendered. Omitted records the exposure, so pinned callers are unchanged.

Examples:

client.experiments.exposures

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/whop_sdk/experiments/client.rb', line 157

def exposures(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["subject"] = params[:subject] if params.key?(:subject)
  query_params["related_resource"] = params[:related_resource] if params.key?(:related_resource)
  query_params["flag_key"] = params[:flag_key] if params.key?(:flag_key)
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)
  query_params["properties"] = params[:properties] if params.key?(:properties)
  query_params["log_exposure"] = params[:log_exposure] if params.key?(:log_exposure)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "experiments/exposures",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Experiments::Types::ExposuresExperimentsResponse.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list(request_options: {}, **params) ⇒ Whop_sdk::Experiments::Types::ListExperimentsResponse

Lists experiments for one account with experiment:read permission. Omit account_id or pass internal to list internal experiments, which requires Whop internal access.

Examples:

client.experiments.list

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/whop_sdk/experiments/client.rb', line 37

def list(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)
  query_params["related_resource"] = params[:related_resource] if params.key?(:related_resource)
  query_params["status"] = params[:status] if params.key?(:status)
  query_params["first"] = params[:first] if params.key?(:first)
  query_params["after"] = params[:after] if params.key?(:after)
  query_params["last"] = params[:last] if params.key?(:last)
  query_params["before"] = params[:before] if params.key?(:before)
  query_params["order"] = params[:order] if params.key?(:order)
  query_params["direction"] = params[:direction] if params.key?(:direction)

  Whop_sdk::Internal::CursorItemIterator.new(
    cursor_field: :end_cursor,
    item_field: :data,
    initial_cursor: query_params["after"]
  ) do |next_cursor|
    query_params["after"] = next_cursor
    request = Whop_sdk::Internal::JSON::Request.new(
      base_url: request_options[:base_url],
      method: "GET",
      path: "experiments",
      query: query_params,
      request_options: request_options
    )
    begin
      response = @client.send(request)
    rescue Net::HTTPRequestTimeout
      raise Whop_sdk::Errors::TimeoutError
    end
    code = response.code.to_i
    if code.between?(200, 299)
      parsed_response = Whop_sdk::Experiments::Types::ListExperimentsResponse.load(response.body)
      [parsed_response, response]
    else
      error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(response.body, code: code)
    end
  end
end

#pause(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Pauses an active experiment or feature flag: evaluation stops serving it and exposures stop flowing. Assignments are keyed on stable identity, so users return to their original arm when the experiment resumes. Requires the corresponding experiment permission on the owning account, or Whop internal access for internal experiments.

Examples:

client.experiments.pause(id: "id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :account_id (String, nil)

Returns:



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/whop_sdk/experiments/client.rb', line 402

def pause(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "experiments/#{URI.encode_uri_component(params[:id].to_s)}/pause",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#retrieve(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Retrieves a single experiment or feature flag by its expt_ id or flag_key handle. Requires the corresponding experiment permission on the owning account, or Whop internal access for internal experiments.

Examples:

client.experiments.retrieve(id: "id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :account_id (String, nil)

Returns:



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/whop_sdk/experiments/client.rb', line 205

def retrieve(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "experiments/#{URI.encode_uri_component(params[:id].to_s)}",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#update(request_options: {}, **params) ⇒ Whop_sdk::Types::Experiment

Updates the targeting rules, treatment allocation, metrics, or hypothesis of an existing experiment or feature flag. Weights and metrics can only grow, so enrolled users never change arms and an existing metric is never dropped. Lifecycle moves through the transition endpoints (activate, pause, end), never through this update. Requires the corresponding experiment permission on the owning account, or Whop internal access for internal experiments.

Examples:

client.experiments.update(id: "id")

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :account_id (String, nil)

Returns:



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/whop_sdk/experiments/client.rb', line 251

def update(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request_data = Whop_sdk::Experiments::Types::UpdateExperimentsRequest.new(params).to_h
  non_body_param_names = %w[id account_id]
  body = request_data.except(*non_body_param_names)

  query_params = {}
  query_params["account_id"] = params[:account_id] if params.key?(:account_id)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PATCH",
    path: "experiments/#{URI.encode_uri_component(params[:id].to_s)}",
    query: query_params,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Experiment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end