Class: Airship

Inherits:
Object
  • Object
show all
Defined in:
lib/airship-ruby.rb

Constant Summary collapse

SCHEMA =
{
  "type" => "object",
  "properties" => {
    "type" => {
      "type" => "string",
      "pattern" => "^([A-Z][a-zA-Z]*)+$",
      "maxLength" => 50,
    },
    "is_group" => {
      "type" => "boolean",
    },
    "id" => {
      "type" => ["string", "integer"],
      "pattern" => "^[ -~]+$",
      "maxLength" => 250,
      "minLength" => 1,
    },
    "display_name" => {
      "type" => "string",
      "maxLength" => 250,
      "minLength" => 1,
    },
    "is_anonymous" => {
      "type" => "boolean",
    },
    "attributes" => {
      "type" => "object",
      "patternProperties" => {
        "^[a-zA-Z][a-zA-Z_]{0,48}[a-zA-Z]$" => {
          "oneOf" => [
            {
              "type" => "string",
              "maxLength" => 3000,
            },
            {
              "type" => "boolean"
            },
            {
              "type" => "number"
            },
          ],
        },
      },
      "maxProperties" => 100,
      "additionalProperties" => false,
    },
    "group" => {
      "type" => ["object", "null"],
      "properties" => {
        "type" => {
          "type" => "string",
          "pattern" => "^([A-Z][a-zA-Z]*)+$",
          "maxLength" => 50,
        },
        "is_group" => {
          "type" => "boolean",
          "enum" => [true],
        },
        "id" => {
          "type" => ["string", "integer"],
          "pattern" => "^[ -~]+$",
          "maxLength" => 250,
          "minLength" => 1,
        },
        "display_name" => {
          "type" => "string",
          "maxLength" => 250,
          "minLength" => 1,
        },
        "attributes" => {
          "type" => "object",
          "patternProperties" => {
            "^[a-zA-Z][a-zA-Z_]{0,48}[a-zA-Z]$" => {
              "oneOf" => [
                {
                  "type" => "string",
                  "maxLength" => 3000,
                },
                {
                  "type" => "boolean"
                },
                {
                  "type" => "number"
                },
              ],
            },
          },
          "maxProperties" => 100,
          "additionalProperties" => false,
        },
      },
      "required" => ["id"],
      "additionalProperties" => false,
    },
  },
  "required" => ["id"],
  "additionalProperties" => false,
}
SERVER_URL =
'https://api.airshiphq.com'
IDENTIFY_ENDPOINT =
"#{SERVER_URL}/v1/identify"
GATING_INFO_ENDPOINT =
"#{SERVER_URL}/v1/gating-info"
PLATFORM =
'ruby'
VERSION =
Gem::Specification::load(
  File.join(
    File.dirname(
      File.dirname(
        File.expand_path(__FILE__)
      )
    ),
    'airship-ruby.gemspec'
  )
).version.to_s
SDK_VERSION =
"#{PLATFORM}:#{VERSION}"
CONTROL_TYPE_BOOLEAN =
'boolean'
CONTROL_TYPE_MULTIVARIATE =
'multivariate'
DISTRIBUTION_TYPE_RULE_BASED =
'R'
DISTRIBUTION_TYPE_PERCENTAGE_BASED =
'P'
OBJECT_ATTRIBUTE_TYPE_STRING =
'STRING'
OBJECT_ATTRIBUTE_TYPE_INT =
'INT'
OBJECT_ATTRIBUTE_TYPE_FLOAT =
'FLOAT'
OBJECT_ATTRIBUTE_TYPE_BOOLEAN =
'BOOLEAN'
OBJECT_ATTRIBUTE_TYPE_DATE =
'DATE'
OBJECT_ATTRIBUTE_TYPE_DATETIME =
'DATETIME'
RULE_OPERATOR_TYPE_IS =
'IS'
RULE_OPERATOR_TYPE_IS_NOT =
'IS_NOT'
RULE_OPERATOR_TYPE_IN =
'IN'
RULE_OPERATOR_TYPE_NOT_IN =
'NOT_IN'
RULE_OPERATOR_TYPE_LT =
'LT'
RULE_OPERATOR_TYPE_LTE =
'LTE'
RULE_OPERATOR_TYPE_GT =
'GT'
RULE_OPERATOR_TYPE_GTE =
'GTE'
RULE_OPERATOR_TYPE_FROM =
'FROM'
RULE_OPERATOR_TYPE_UNTIL =
'UNTIL'
RULE_OPERATOR_TYPE_AFTER =
'AFTER'
RULE_OPERATOR_TYPE_BEFORE =
'BEFORE'
@@sdk_id =
[('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten.sample(6).join('')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Airship

Returns a new instance of Airship.



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/airship-ruby.rb', line 162

def initialize(options)
  @api_key = options[:api_key]
  @env_key = options[:env_key]
  @timeout = 10

  @polling_interval = 60
  @ingestion_interval = 60

  @polling_thread = nil
  @ingestion_thread = nil

  @gating_info = nil
  @gating_info_map = nil

  @last_ingestion_timestamp = 0
  @ingestion_tasks = []

  @ingestion_batch = []
  @ingestion_max_batch_size = 500

  @init_lock = Concurrent::Semaphore.new(1)
  @ingestion_batch_lock = Concurrent::Semaphore.new(1)

  @first_ingestion = true
end

Class Method Details

.get_hashed_value(s) ⇒ Object



157
158
159
# File 'lib/airship-ruby.rb', line 157

def get_hashed_value(s)
  Digest::MD5.hexdigest(s).to_i(base=16).fdiv(340282366920938463463374607431768211455)
end

Instance Method Details

#eligible?(control_short_name, object, default_value = false) ⇒ Boolean

Returns:

  • (Boolean)


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/airship-ruby.rb', line 332

def eligible?(control_short_name, object, default_value=false)
  object = self._shallow_copy(object)

  validation_errors = JSON::Validator.fully_validate(SCHEMA, object)
  if validation_errors.size > 0
    puts validation_errors[0]
    return default_value
  end

  object = self._clone_object(object)

  self._maybe_add_fields(object)

  error = self._validate_nesting(object) || self._validate_anonymous(object) || self._maybe_transform_id(object)

  if !error.nil?
    puts error
    return default_value
  end

  if @gating_info_map.nil?
    return default_value
  end

  gate_timestamp = Time.now.utc.iso8601

  start = Time.now

  gate_values = self._get_gate_values(control_short_name, object)
  is_enabled = gate_values['is_enabled']
  variation = gate_values['variation']
  is_eligible = gate_values['is_eligible']
  _should_send_stats = gate_values['_should_send_stats']

  finish = Time.now

  if _should_send_stats
    sdk_gate_timestamp = gate_timestamp
    sdk_gate_latency = "#{(finish - start) * 1000 * 1000}us"
    sdk_version = SDK_VERSION

    stats = {}
    stats['sdk_gate_control_short_name'] = control_short_name
    stats['sdk_gate_timestamp'] = sdk_gate_timestamp
    stats['sdk_gate_latency'] = sdk_gate_latency

    stats['sdk_gate_value'] = is_enabled
    stats['sdk_gate_variation'] = variation
    stats['sdk_gate_eligibility'] = is_eligible
    stats['sdk_gate_type'] = 'eligibility'

    self.(control_short_name, stats)

    stats['sdk_version'] = sdk_version
    stats['sdk_id'] = @@sdk_id

    object['stats'] = stats

    self._ingest_async(object)
  end

  return is_eligible
end

#enabled?(control_short_name, object, default_value = false) ⇒ Boolean

Returns:

  • (Boolean)


204
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
# File 'lib/airship-ruby.rb', line 204

def enabled?(control_short_name, object, default_value=false)
  object = self._shallow_copy(object)

  validation_errors = JSON::Validator.fully_validate(SCHEMA, object)
  if validation_errors.size > 0
    puts validation_errors[0]
    return default_value
  end

  object = self._clone_object(object)

  self._maybe_add_fields(object)

  error = self._validate_nesting(object) || self._validate_anonymous(object) || self._maybe_transform_id(object)

  if !error.nil?
    puts error
    return default_value
  end

  if @gating_info_map.nil?
    return default_value
  end

  gate_timestamp = Time.now.utc.iso8601

  start = Time.now

  gate_values = self._get_gate_values(control_short_name, object)
  is_enabled = gate_values['is_enabled']
  variation = gate_values['variation']
  is_eligible = gate_values['is_eligible']
  _should_send_stats = gate_values['_should_send_stats']

  finish = Time.now

  if _should_send_stats
    sdk_gate_timestamp = gate_timestamp
    sdk_gate_latency = "#{(finish - start) * 1000 * 1000}us"
    sdk_version = SDK_VERSION

    stats = {}
    stats['sdk_gate_control_short_name'] = control_short_name
    stats['sdk_gate_timestamp'] = sdk_gate_timestamp
    stats['sdk_gate_latency'] = sdk_gate_latency

    stats['sdk_gate_value'] = is_enabled
    stats['sdk_gate_variation'] = variation
    stats['sdk_gate_eligibility'] = is_eligible
    stats['sdk_gate_type'] = 'value'

    self.(control_short_name, stats)

    stats['sdk_version'] = sdk_version
    stats['sdk_id'] = @@sdk_id

    object['stats'] = stats

    self._ingest_async(object)
  end

  return is_enabled
end

#initObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/airship-ruby.rb', line 188

def init
  @init_lock.acquire
  if @polling_thread.nil?
    self._poll
    @polling_thread = self._create_poller
    @polling_thread.execute
  end

  if @ingestion_thread.nil?
    @ingestion_thread = self._create_ingestor
    @ingestion_thread.execute
  end
  @init_lock.release
  self
end

#variation(control_short_name, object, default_value = nil) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
# File 'lib/airship-ruby.rb', line 268

def variation(control_short_name, object, default_value=nil)
  object = self._shallow_copy(object)

  validation_errors = JSON::Validator.fully_validate(SCHEMA, object)
  if validation_errors.size > 0
    puts validation_errors[0]
    return default_value
  end

  object = self._clone_object(object)

  self._maybe_add_fields(object)

  error = self._validate_nesting(object) || self._validate_anonymous(object) || self._maybe_transform_id(object)

  if !error.nil?
    puts error
    return default_value
  end

  if @gating_info_map.nil?
    return default_value
  end

  gate_timestamp = Time.now.utc.iso8601

  start = Time.now

  gate_values = self._get_gate_values(control_short_name, object)
  is_enabled = gate_values['is_enabled']
  variation = gate_values['variation']
  is_eligible = gate_values['is_eligible']
  _should_send_stats = gate_values['_should_send_stats']

  finish = Time.now

  if _should_send_stats
    sdk_gate_timestamp = gate_timestamp
    sdk_gate_latency = "#{(finish - start) * 1000 * 1000}us"
    sdk_version = SDK_VERSION

    stats = {}
    stats['sdk_gate_control_short_name'] = control_short_name
    stats['sdk_gate_timestamp'] = sdk_gate_timestamp
    stats['sdk_gate_latency'] = sdk_gate_latency

    stats['sdk_gate_value'] = is_enabled
    stats['sdk_gate_variation'] = variation
    stats['sdk_gate_eligibility'] = is_eligible
    stats['sdk_gate_type'] = 'variation'

    self.(control_short_name, stats)

    stats['sdk_version'] = sdk_version
    stats['sdk_id'] = @@sdk_id

    object['stats'] = stats

    self._ingest_async(object)
  end

  return variation
end