Class: Kameleoon::Configuration::Experiment

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/configuration/experiment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, variations_by_exposition) ⇒ Experiment

Returns a new instance of Experiment.



11
12
13
14
# File 'lib/kameleoon/configuration/experiment.rb', line 11

def initialize(id, variations_by_exposition)
  @id = id
  @variations_by_exposition = variations_by_exposition
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/kameleoon/configuration/experiment.rb', line 9

def id
  @id
end

#variations_by_expositionObject (readonly)

Returns the value of attribute variations_by_exposition.



9
10
11
# File 'lib/kameleoon/configuration/experiment.rb', line 9

def variations_by_exposition
  @variations_by_exposition
end

Class Method Details

.from_json(hash) ⇒ Object



16
17
18
19
20
21
# File 'lib/kameleoon/configuration/experiment.rb', line 16

def self.from_json(hash)
  id = hash['experimentId'] || 0
  variations_by_exposition = VariationByExposition.create_from_array(hash['variationByExposition'])
  variations_by_exposition.freeze
  Experiment.new(id, variations_by_exposition)
end

Instance Method Details

#get_variation(hash_double) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/kameleoon/configuration/experiment.rb', line 27

def get_variation(hash_double)
  total = 0.0
  @variations_by_exposition.each do |var_by_exp|
    total += var_by_exp.exposition
    return var_by_exp if total >= hash_double
  end
  nil
end

#get_variation_by_key(variation_key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/kameleoon/configuration/experiment.rb', line 36

def get_variation_by_key(variation_key)
  var_by_exp = @variations_by_exposition.find { |v| v.variation_key == variation_key }
  unless var_by_exp
    raise Exception::FeatureVariationNotFound.new(variation_key),
          "#{self} does not contain variation '#{variation_key}'"
  end
  var_by_exp
end

#to_sObject



23
24
25
# File 'lib/kameleoon/configuration/experiment.rb', line 23

def to_s
  "Experiment{id:#{@id}}"
end