Class: Kameleoon::Targeting::CustomDatum Private

Inherits:
Condition
  • Object
show all
Includes:
Exception
Defined in:
lib/kameleoon/targeting/conditions/custom_datum.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

CustomDatum represents an instance of Custom Data condition from back office

Instance Attribute Summary

Attributes inherited from Condition

#include, #type

Instance Method Summary collapse

Constructor Details

#initialize(json_condition) ⇒ CustomDatum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CustomDatum.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 12

def initialize(json_condition)
  super(json_condition)
  @index = json_condition['customDataIndex']

  if json_condition['valueMatchType'].nil?
    raise Exception::NotFoundError.new('valueMatchType'), 'valueMatchType missed'
  end

  @operator = json_condition['valueMatchType']

  @value = json_condition['value']

  @type = ConditionType::CUSTOM_DATUM

  if json_condition['include'].nil? && json_condition['isInclude'].nil?
    raise Exception::NotFoundError.new('include / isInclude missed'), 'include / isInclude missed'
  end

  @include = json_condition['include'] || json_condition['isInclude']
end

Instance Method Details

#check(datas) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
36
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
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 33

def check(datas)
  is_targeted = false
  custom_data = datas.select { |data| data.instance == DataType::CUSTOM && data.id == @index }.last
  if custom_data.nil?
    is_targeted = (@operator == Operator::UNDEFINED.to_s)
  else
    case @operator
    when Operator::MATCH.to_s
      is_targeted = !Regexp.new(@value.to_s).match(custom_data.value.to_s).nil?
    when Operator::CONTAINS.to_s
      is_targeted = custom_data.value.to_s.include? @value
    when Operator::EXACT.to_s
      is_targeted = custom_data.value.to_s == @value.to_s
    when Operator::EQUAL.to_s
      is_targeted = custom_data.value.to_f == @value.to_f
    when Operator::GREATER.to_s
      is_targeted = custom_data.value.to_f > @value.to_f
    when Operator::LOWER.to_s
      is_targeted = custom_data.value.to_f < @value.to_f
    when Operator::IS_TRUE.to_s
      is_targeted = custom_data.value == 'true'
    when Operator::IS_FALSE.to_s
      is_targeted = custom_data.value == 'false'
    when Operator::AMONG_VALUES.to_s
      is_targeted = @value.scan(/"([^"]*)"/).flatten.include?(custom_data.value)
    else
      raise KameleoonError.new("Undefined operator #{@operator}"), "Undefined operator #{@operator}"
    end
  end
  is_targeted
end