Class: Kameleoon::CustomData

Inherits:
Data
  • Object
show all
Defined in:
lib/kameleoon/data/custom_data.rb

Overview

Represents any custom data for targeting conditions

Instance Attribute Summary collapse

Attributes inherited from Data

#instance, #sent

Instance Method Summary collapse

Constructor Details

#initialize(arg0, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kameleoon/data/custom_data.rb', line 19

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.to_s
    value = hash['value']
    values = hash['values']
    if values.nil? && value.nil?
      raise Kameleoon::Exception::NotFound.new('value or values'), '"value" or "values" is mandatory'
    end

    if values.nil?
      @values = [value]
    else
      @values = values.is_a?(Array) ? values.dup : [values]
      @values.append(value) unless value.nil?
    end
  else
    @id = arg0.to_s
    @values = args
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#obtain_full_post_text_lineObject

rubocop:enable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kameleoon/data/custom_data.rb', line 46

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