Class: Kameleoon::CustomData

Inherits:
DuplicationUnsafeData show all
Defined in:
lib/kameleoon/data/custom_data.rb

Overview

Represents any custom data for targeting conditions

Direct Known Subclasses

MappingIdentifier

Constant Summary collapse

UNDEFINED_INDEX =
-1

Instance Attribute Summary collapse

Attributes inherited from Data

#instance

Instance Method Summary collapse

Methods inherited from Data

#mark_as_sent, #mark_as_transmitting, #mark_as_unsent, #sent, #transmitting, #unsent

Constructor Details

#initialize(arg0, *args, overwrite: true) ⇒ Object



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
64
65
66
67
68
# File 'lib/kameleoon/data/custom_data.rb', line 33

def initialize(arg0, *args, overwrite: true)
  super(DataType::CUSTOM)
  if arg0.is_a?(Hash)
    hash = arg0
    index = hash['index'] || hash['id']
    name = hash['name']
    if index.nil? && name.nil?
      raise Exception::NotFound.new('index/id/name'), '"index/id/name" is mandatory'
    end

    @index = index || CustomData::UNDEFINED_INDEX
    @name = name
    values = hash['values']
    raise Exception::NotFound.new('values'), '"values" is mandatory' if values.nil?

    @values = values.is_a?(Array) ? values.dup : [values]
    @overwrite = hash.fetch('overwrite', true)
  else
    case arg0
    when Integer
      @index = arg0
    when String
      @index = CustomData::UNDEFINED_INDEX
      @name = arg0
    else
      raise Exception.new('Unexpected arg0 type'), 'Unexpected arg0 type'
    end
    @values = args
    @overwrite = overwrite
  end

  return if @index.is_a?(Integer)

  Logging::KameleoonLogger.warning("CustomData field 'index' must be of 'Integer' type")
  @index = @index.is_a?(String) ? @index.to_i : CustomData::UNDEFINED_INDEX
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



11
12
13
# File 'lib/kameleoon/data/custom_data.rb', line 11

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/kameleoon/data/custom_data.rb', line 11

def name
  @name
end

#overwriteObject (readonly)

Returns the value of attribute overwrite.



11
12
13
# File 'lib/kameleoon/data/custom_data.rb', line 11

def overwrite
  @overwrite
end

#valuesObject (readonly)

Returns the value of attribute values.



11
12
13
# File 'lib/kameleoon/data/custom_data.rb', line 11

def values
  @values
end

Instance Method Details

#idObject

DEPRECATED. Please use ‘index` instead.



84
85
86
# File 'lib/kameleoon/data/custom_data.rb', line 84

def id
  @index
end

#named_to_indexed(index) ⇒ Object

rubocop:enable Metrics/MethodLength



71
72
73
74
75
76
77
78
79
80
# File 'lib/kameleoon/data/custom_data.rb', line 71

def named_to_indexed(index)
  CustomData.new(
    {
      'index' => index,
      'name' => @name,
      'values' => @values,
      'overwrite' => @overwrite
    }
  )
end

#obtain_full_post_text_lineObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kameleoon/data/custom_data.rb', line 88

def obtain_full_post_text_line
  str_values = JSON.generate(Hash[@values.collect { |k| [k, 1] }])
  params = {
    eventType: 'customData',
    index: @index,
    valuesCountMap: str_values,
    overwrite: @overwrite,
    nonce: nonce
  }
  Network::UriHelper.encode_query(params)
end

#to_sObject



15
16
17
# File 'lib/kameleoon/data/custom_data.rb', line 15

def to_s
  "CustomData{index:#{@index},name:'#{@name}',values:#{@values},overwrite:#{@overwrite}}"
end