Class: DevCycle::DVCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/devcycle-ruby-server-sdk/api/devcycle_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdkKey, dvc_options = DVCOptions.new, wait_for_init = false) ⇒ DVCClient

Returns a new instance of DVCClient.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 18

def initialize(sdkKey, dvc_options = DVCOptions.new, wait_for_init = false)
  if sdkKey.nil?
    raise ArgumentError('Missing SDK key!')
  elsif !sdkKey.start_with?('server') && !sdkKey.start_with?('dvc_server')
    raise ArgumentError('Invalid SDK key!')
  end

  @sdkKey = sdkKey
  @dvc_options = dvc_options
  @logger = dvc_options.logger

  if @dvc_options.enable_cloud_bucketing
    @api_client = ApiClient.default
    @api_client.config.api_key['bearerAuth'] = @sdkKey
    @api_client.config.enable_edge_db = @dvc_options.enable_edge_db
    @api_client.config.logger = @logger
  else
    @localbucketing = LocalBucketing.new(@sdkKey, dvc_options, wait_for_init)
    @event_queue = EventQueue.new(@sdkKey, dvc_options.event_queue_options, @localbucketing)
  end
end

Instance Method Details

#all_features(user_data, opts = {}) ⇒ Hash<String, Feature>

Get all features by key for user data

Parameters:

  • user_data (UserData)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 77

def all_features(user_data, opts = {})
  if !user_data.is_a?(DevCycle::UserData)
    fail ArgumentError, "user_data param must be an instance of UserData!"
  end

  validate_model(user_data)

  if @dvc_options.enable_cloud_bucketing
    data, _status_code, _headers = all_features_with_http_info(user_data, opts)
    return data
  end

  if local_bucketing_initialized?
    bucketed_config = @localbucketing.generate_bucketed_config(user_data)
    bucketed_config.features
  else
    {}
  end
end

#all_features_with_http_info(user_data, opts = {}) ⇒ Array<(Hash<String, Feature>, Integer, Hash)>

Get all features by key for user data

Parameters:

  • user_data (UserData)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (Array<(Hash<String, Feature>, Integer, Hash)>)

    Hash<String, Feature> data, response status code and response headers



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 101

def all_features_with_http_info(user_data, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: DVCClient.all_features ...'
  end
  # verify the required parameter 'user_data' is set
  if @api_client.config.client_side_validation && user_data.nil?
    fail ArgumentError, "Missing the required parameter 'user_data' when calling DVCClient.all_features"
  end
  # resource path
  local_var_path = '/v1/features'

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'
  content_type = @api_client.select_header_content_type(['application/json'])
  if !content_type.nil?
    header_params['Content-Type'] = content_type
  end

  # form parameters
  form_params = opts[:form_params] || {}

  # http body (model)
  post_body = opts[:debug_body] || user_data.to_json

  # return_type
  return_type = opts[:debug_return_type] || 'Hash<String, Feature>'

  # auth_names
  auth_names = opts[:debug_auth_names] || ['bearerAuth']

  new_options = opts.merge(
    :operation => :"DVCClient.all_features",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :auth_names => auth_names,
    :return_type => return_type
  )

  data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
  if @api_client.config.debugging
    @api_client.config.logger.debug "API called: DVCClient#all_features\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
  end
  return data, status_code, headers
end

#all_variables(user_data, opts = {}) ⇒ Hash<String, Variable>

Get all variables by key for user data

Parameters:

  • user_data (UserData)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 274

def all_variables(user_data, opts = {})
  if !user_data.is_a?(DevCycle::UserData)
    fail ArgumentError, "user_data param must be an instance of UserData!"
  end

  validate_model(user_data)

  if @dvc_options.enable_cloud_bucketing
    data, _status_code, _headers = all_variables_with_http_info(user_data, opts)
    return data
  end

  if local_bucketing_initialized?
    bucketed_config = @localbucketing.generate_bucketed_config(user_data)
    bucketed_config.variables
  else
    {}
  end
end

#all_variables_with_http_info(user_data, opts = {}) ⇒ Array<(Hash<String, Variable>, Integer, Hash)>

Get all variables by key for user data

Parameters:

  • user_data (UserData)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (Array<(Hash<String, Variable>, Integer, Hash)>)

    Hash<String, Variable> data, response status code and response headers



298
299
300
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 298

def all_variables_with_http_info(user_data, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: DVCClient.all_variables ...'
  end
  # verify the required parameter 'user_data' is set
  if @api_client.config.client_side_validation && user_data.nil?
    fail ArgumentError, "Missing the required parameter 'user_data' when calling DVCClient.all_variables"
  end
  # resource path
  local_var_path = '/v1/variables'

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'
  content_type = @api_client.select_header_content_type(['application/json'])
  if !content_type.nil?
    header_params['Content-Type'] = content_type
  end

  # form parameters
  form_params = opts[:form_params] || {}

  # http body (model)
  post_body = opts[:debug_body] || user_data.to_json

  # return_type
  return_type = opts[:debug_return_type] || 'Hash<String, Variable>'

  # auth_names
  auth_names = opts[:debug_auth_names] || ['bearerAuth']

  new_options = opts.merge(
    :operation => :"DVCClient.all_variables",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :auth_names => auth_names,
    :return_type => return_type
  )

  data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
  if @api_client.config.debugging
    @api_client.config.logger.debug "API called: DVCClient#all_variables\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
  end
  return data, status_code, headers
end

#closeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 40

def close
  if @dvc_options.enable_cloud_bucketing
    @logger.info("Cloud Bucketing does not require closing.")
    return
  end
  if @localbucketing != nil
    if !@localbucketing.initialized
      @logger.info("Awaiting client initialization before closing")
      while !@localbucketing.initialized
        sleep(0.5)
      end
    end
    @localbucketing.close
    @localbucketing = nil
    @logger.info("Closed DevCycle Local Bucketing Engine.")
  end

  @event_queue.close
  @logger.info("Closed DevCycle Client.")
end

#flush_eventsObject



448
449
450
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 448

def flush_events
  @event_queue.flush_events
end

#local_bucketing_initialized?Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 452

def local_bucketing_initialized?
  !@localbucketing.nil? && @localbucketing.initialized
end

#set_client_custom_data(customdata) ⇒ Object



61
62
63
64
65
66
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 61

def set_client_custom_data(customdata)
  if @api_client.config.enable_cloud_bucketing
    fail ArgumentError("Client Custom Data is only available in Local bucketing mode.")
  end
  @localbucketing.set_client_custom_data(customdata)
end

#track(user_data, event_data, opts = {}) ⇒ InlineResponse201

Post events to DevCycle for user

Parameters:

  • user_data (UserData)
  • event_data (Event)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 356

def track(user_data, event_data, opts = {})
  if !user_data.is_a?(DevCycle::UserData)
    fail ArgumentError, "user_data param must be an instance of UserData!"
  end

  validate_model(user_data)

  if !event_data.is_a?(DevCycle::Event)
    fail ArgumentError, "event_data param must be an instance of Event!"
  end

  validate_model(event_data)

  if @dvc_options.enable_cloud_bucketing
    track_with_http_info(user_data, event_data, opts)
    return
  end

  if local_bucketing_initialized?
    @event_queue.queue_event(user_data, event_data)
  else
    @logger.warn('track called before DVCClient initialized, event will not be tracked')
  end
end

#track_with_http_info(user_data, event_data, opts = {}) ⇒ Array<(InlineResponse201, Integer, Hash)>

Post events to DevCycle for user

Parameters:

  • user_data (UserData)
  • event_data (Event)
  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (Array<(InlineResponse201, Integer, Hash)>)

    InlineResponse201 data, response status code and response headers



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 386

def track_with_http_info(user_data, event_data, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: DVCClient.post_events ...'
  end
  # verify the required parameter 'user_data_and_events_body' is set
  if @api_client.config.client_side_validation && (user_data.nil? || event_data.nil?)
    fail ArgumentError, "Missing the required parameter 'user_data_and_events_body' when calling DVCClient.post_events"
  end

  user_data_and_events_body = DevCycle::UserDataAndEventsBody.new({
                                                                    user: user_data,
                                                                    events: [event_data]
                                                                  })

  # resource path
  local_var_path = '/v1/track'

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'
  content_type = @api_client.select_header_content_type(['application/json'])
  if !content_type.nil?
    header_params['Content-Type'] = content_type
  end

  # form parameters
  form_params = opts[:form_params] || {}

  # http body (model)
  post_body = opts[:debug_body] || @api_client.object_to_http_body(user_data_and_events_body)

  # if post_body.user.respond_to?(:to_hash)
  #   post_body.user = post_body.user.to_hash()

  # return_type
  return_type = opts[:debug_return_type] || 'InlineResponse201'

  # auth_names
  auth_names = opts[:debug_auth_names] || ['bearerAuth']

  new_options = opts.merge(
    :operation => :"DVCClient.post_events",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :auth_names => auth_names,
    :return_type => return_type
  )

  data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
  if @api_client.config.debugging
    @api_client.config.logger.debug "API called: DVCClient#post_events\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
  end
  return data, status_code, headers
end

#validate_model(model) ⇒ Object



68
69
70
71
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 68

def validate_model(model)
  return if model.valid?
  fail ArgumentError, "Invalid data provided for model #{model.class.name}: #{model.list_invalid_properties()}"
end

#variable(user_data, key, default, opts = {}) ⇒ Variable

Get variable by key for user data

Parameters:

  • user_data (UserData)
  • key (String)

    Variable key

  • default

    Default value for variable if none is retrieved

  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:



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
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 160

def variable(user_data, key, default, opts = {})
  if !user_data.is_a?(DevCycle::UserData)
    fail ArgumentError, "user_data param must be an instance of UserData!"
  end

  validate_model(user_data)

  if @dvc_options.enable_cloud_bucketing
    data, _status_code, _headers = variable_with_http_info(key, user_data, default, opts)
    return data
  end

  if local_bucketing_initialized?
    bucketed_config = @localbucketing.generate_bucketed_config(user_data)
    variable_json = bucketed_config.variables[key]
    if variable_json == nil
      variable_event = Event.new({ type: DevCycle::EventTypes[:agg_variable_defaulted], target: key })
      @event_queue.queue_aggregate_event(variable_event, bucketed_config)

      return Variable.new({ key: key, value: default, isDefaulted: true })
    end

    variable_event = Event.new({ type: DevCycle::EventTypes[:agg_variable_evaluated], target: key })
    @event_queue.queue_aggregate_event(variable_event, bucketed_config)

    Variable.new({
                   key: key,
                   type: variable_json['type'],
                   value: variable_json['value'],
                   isDefaulted: false
                 })
  else
    variable_event = Event.new({ type: DevCycle::EventTypes[:agg_variable_defaulted], target: key })
    @event_queue.queue_aggregate_event(variable_event, bucketed_config)

    Variable.new({ key: key, value: default, isDefaulted: true })
  end
end

#variable_with_http_info(key, user_data, default, opts = {}) ⇒ Array<(Variable, Integer, Hash)>

Get variable by key for user data

Parameters:

  • key (String)

    Variable key

  • user_data (UserData)
  • default

    Default value for variable if none is retrieved

  • opts (Hash) (defaults to: {})

    the optional parameters

Returns:

  • (Array<(Variable, Integer, Hash)>)

    Variable data, response status code and response headers



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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/devcycle-ruby-server-sdk/api/devcycle_api.rb', line 205

def variable_with_http_info(key, user_data, default, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: DVCClient.variable ...'
  end
  # verify the required parameter 'key' is set
  if @api_client.config.client_side_validation && key.nil?
    fail ArgumentError, "Missing the required parameter 'key' when calling DVCClient.variable"
  end
  # verify the required parameter 'user_data' is set
  if @api_client.config.client_side_validation && user_data.nil?
    fail ArgumentError, "Missing the required parameter 'user_data' when calling DVCClient.variable"
  end
  # resource path
  local_var_path = '/v1/variables/{key}'.sub('{' + 'key' + '}', CGI.escape(key.to_s))

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'
  content_type = @api_client.select_header_content_type(['application/json'])
  if !content_type.nil?
    header_params['Content-Type'] = content_type
  end

  # form parameters
  form_params = opts[:form_params] || {}

  # http body (model)
  post_body = opts[:debug_body] || user_data.to_json

  # return_type
  return_type = opts[:debug_return_type] || 'Variable'

  # auth_names
  auth_names = opts[:debug_auth_names] || ['bearerAuth']

  new_options = opts.merge(
    :operation => :"DVCClient.variable",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :auth_names => auth_names,
    :return_type => return_type
  )

  begin
    data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
    if @api_client.config.debugging
      @api_client.config.logger.debug "API called: DVCClient#variable\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
    end
    return data
  rescue ApiError => error
    if error.code != 404
      @api_client.config.logger.error("Failed to retrieve variable value: #{error.message}")
    end

    return Variable.new(key: key, value: default, isDefaulted: true)
  end
end