Module: Gitlab::Experiment::BaseInterface

Extended by:
ActiveSupport::Concern
Included in:
Gitlab::Experiment
Defined in:
lib/gitlab/experiment/base_interface.rb

Instance Method Summary collapse

Instance Method Details

#idObject Also known as: to_param



75
76
77
# File 'lib/gitlab/experiment/base_interface.rb', line 75

def id
  "#{name}:#{context.key}"
end

#initialize(name = nil, variant_name = nil, **context) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/experiment/base_interface.rb', line 48

def initialize(name = nil, variant_name = nil, **context)
  raise ArgumentError, 'name is required' if name.blank? && self.class.base?

  @_name = self.class.experiment_name(name, suffix: false)
  @_context = Context.new(self, **context)
  @_assigned_variant_name = cache_variant(variant_name) { nil } if variant_name.present?

  yield self if block_given?
end

#inspectObject



58
59
60
# File 'lib/gitlab/experiment/base_interface.rb', line 58

def inspect
  "#<#{self.class.name || 'AnonymousClass'}:#{format('0x%016X', __id__)} name=#{name} context=#{context.value}>"
end

#key_for(source, seed = name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/experiment/base_interface.rb', line 88

def key_for(source, seed = name)
  return source if source.is_a?(String)

  source = source.keys + source.values if source.is_a?(Hash)

  ingredients = Array(source).map { |v| identify(v) }
  ingredients.unshift(seed).unshift(Configuration.context_key_secret)

  Digest::SHA2.new(Configuration.context_key_bit_length).hexdigest(ingredients.join('|')) # rubocop:disable Fips/OpenSSL
end

#process_redirect_url(url) ⇒ Object



81
82
83
84
85
86
# File 'lib/gitlab/experiment/base_interface.rb', line 81

def process_redirect_url(url)
  return unless Configuration.redirect_url_validator&.call(url)

  track('visited', url: url)
  url # return the url, which allows for mutation
end

#run(variant_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/experiment/base_interface.rb', line 62

def run(variant_name)
  behaviors.freeze
  context.freeze

  block = behaviors[variant_name]
  raise BehaviorMissingError, "the `#{variant_name}` variant hasn't been registered" if block.nil?

  result = block.call
  publish(result) if enabled?

  result
end

#variant_namesObject

Deprecated.


100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/experiment/base_interface.rb', line 100

def variant_names
  Configuration.deprecated(
    :variant_names,
    'instead use `behavior.names`, which includes :control',
    version: '0.8.0'
  )

  behaviors.keys - [:control]
end