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

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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kameleoon/data/custom_data.rb', line 24

def initialize(arg0, *args)
  super(DataType::CUSTOM)
  if arg0.is_a?(Hash)
    hash = arg0
    id = hash['id']
    raise Kameleoon::Exception::NotFound.new('id'), '"id" is mandatory' if id.nil?

    @id = id
    values = hash['values']
    raise Kameleoon::Exception::NotFound.new('values'), '"values" is mandatory' if values.nil?

    @values = values.is_a?(Array) ? values.dup : [values]
  else
    @id = arg0
    @values = args
  end

  if @values.empty?
    Logging::KameleoonLogger.error('Created a custom data %s with no values. It will not be tracked.', @id)
  end

  return if @id.is_a?(Integer)

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
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

#obtain_full_post_text_lineObject

rubocop:enable Metrics/MethodLength



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kameleoon/data/custom_data.rb', line 52

def obtain_full_post_text_line
  return '' if @values.empty?

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

#to_sObject



13
14
15
# File 'lib/kameleoon/data/custom_data.rb', line 13

def to_s
  "CustomData{id:#{@id},values:#{@values}}"
end