class CleverTap::Uploader

unit uploading profile data to CleverTap

Constants

ENTITY_DATA_NAMES
HTTP_PATH
TYPE_EVENT
TYPE_PROFILE

Attributes

date_field[R]
dry_run[R]
event_name[R]
identity_field[R]
records[R]
type[R]

Public Class Methods

new(records, identity_field:, date_field: nil, event_name: nil, dry_run: false) click to toggle source

TODO: make defaults configurable date_field should be a date object responding to `to_i` which should returns epoch time profile respond to .to_h

# File lib/clever_tap/uploader.rb, line 20
def initialize(records, identity_field:, date_field: nil, event_name: nil, dry_run: false)
  @type = event_name ? TYPE_EVENT : TYPE_PROFILE
  @records = records

  @identity_field = identity_field
  @date_field = date_field
  @event_name = event_name
  @dry_run = dry_run
end

Public Instance Methods

call(client) click to toggle source
# File lib/clever_tap/uploader.rb, line 30
def call(client)
  response = client.post(HTTP_PATH, build_request_body) do |request|
    request.params.merge!(dryRun: 1) if dry_run
  end

  parse_response(response)
end

Private Instance Methods

build_request_body() click to toggle source
# File lib/clever_tap/uploader.rb, line 40
def build_request_body
  records.each_with_object('d' => []) do |record, request_body|
    request_body['d'] << normalize_record(record)
  end.to_json
end
normalize_record(record) click to toggle source
# File lib/clever_tap/uploader.rb, line 46
def normalize_record(record)
  ts = date_field ? record[date_field] : Time.now

  {
    'identity' => pluck_identity(record).to_s,
    'ts' => ts.to_i,
    'type' => type,
    ENTITY_DATA_NAMES[type] => record.to_h
  }.tap do |hash|
    hash.merge!('evtName' => event_name) if type == TYPE_EVENT
  end
end
parse_response(http_response) click to toggle source
# File lib/clever_tap/uploader.rb, line 59
def parse_response(http_response)
  http_response
end
pluck_identity(record) click to toggle source
# File lib/clever_tap/uploader.rb, line 63
def pluck_identity(record)
  # TODO: symbolize record keys
  identity = record[identity_field.to_s] || record[identity_field.to_sym]

  raise "Missing identity field with name: '#{identity_field}' for #{record}" unless identity

  identity
end