Class: SnowplowTracker::Payload
- Inherits:
-
Object
- Object
- SnowplowTracker::Payload
- Defined in:
- lib/snowplow-tracker/payload.rb
Overview
Every`track_x_event` method creates a new Payload object. The Tracker then uses the Payload instance methods to add properties to the Payload ‘@data` hash. These properties form the raw event, after the completed hash is given to the Emitter.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#add(name, value) ⇒ Object
Add a single name-value pair to @data.
-
#add_hash(hash) ⇒ Object
Add each name-value pair in a hash to @data.
-
#add_json(json, encode_base64, type_when_encoded, type_when_not_encoded) ⇒ Object
Stringify a JSON and add it to @data.
-
#initialize ⇒ Payload
constructor
A new instance of Payload.
Constructor Details
#initialize ⇒ Payload
Returns a new instance of Payload.
30 31 32 |
# File 'lib/snowplow-tracker/payload.rb', line 30 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/snowplow-tracker/payload.rb', line 28 def data @data end |
Instance Method Details
#add(name, value) ⇒ Object
Add a single name-value pair to @data.
35 36 37 |
# File 'lib/snowplow-tracker/payload.rb', line 35 def add(name, value) @data[name] = value if (value != '') && !value.nil? end |
#add_hash(hash) ⇒ Object
Add each name-value pair in a hash to @data.
40 41 42 |
# File 'lib/snowplow-tracker/payload.rb', line 40 def add_hash(hash) hash.each { |key, value| add(key, value) } end |
#add_json(json, encode_base64, type_when_encoded, type_when_not_encoded) ⇒ Object
Stringify a JSON and add it to @data.
In practice, the JSON provided will be a SelfDescribingJson. This method is used to add context to events, or for ‘track_self_describing_event`.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/snowplow-tracker/payload.rb', line 50 def add_json(json, encode_base64, type_when_encoded, type_when_not_encoded) return if json.nil? stringified = JSON.generate(json) if encode_base64 add(type_when_encoded, Base64.strict_encode64(stringified)) else add(type_when_not_encoded, stringified) end end |