Module: FacebookAds::CustomAudienceHelpers

Included in:
CustomAudience
Defined in:
lib/facebook_ads/ad_objects/helpers/custom_audience_helpers.rb

Constant Summary collapse

SCHEMAS =
%w{
  UID
  EMAIL_SHA256
  PHONE_SHA256
  MOBILE_ADVERTISER_ID
}
MULTIKEY_SCHEMAS =
%w{
  EXTERN_ID
  EMAIL
  PHONE
  GEN
  DOBY
  DOBM
  DOBD
  LN
  FN
  FI
  CT
  ST
  ZIP
  MADID
  COUNTRY
}
SCHEMAS_WITHOUT_HASH =
%w{
  EXTERN_ID
  MOBILE_ADVERTISER_ID
}

Instance Method Summary collapse

Instance Method Details

#add_user(data, schema, options = {}) ⇒ Object



41
42
43
# File 'lib/facebook_ads/ad_objects/helpers/custom_audience_helpers.rb', line 41

def add_user(data, schema, options = {})
  self.users.create(prepare_params(data, schema, **options))
end

#prepare_params(data, schema, app_ids: nil, page_ids: nil, pre_hashed: false, namespace: nil, session: nil, data_source: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/facebook_ads/ad_objects/helpers/custom_audience_helpers.rb', line 49

def prepare_params(data,
                   schema,
                   app_ids:     nil,
                   page_ids:    nil,
                   pre_hashed:  false,
                   namespace:   nil,
                   session:     nil,
                   data_source: nil)

  unless schema.is_a?(Array) || schema.split(',').length > 1
    # single key
    schema = schema.upcase
    unless is_valid_single_key?(schema) || is_combinational_key?(schema)
      raise InvalidParameterError, "#{schema} is not a valid schema"
    end

    data = pre_hashed ? data : data.map { |entry| generate_hash(entry, schema)}
    is_raw = !is_combinational_key?(schema)
  else
    # multi key
    schema = schema.split(',') unless schema.is_a?(Array)

    data = data.map do |row|
      raise 'Schema size and data size mismatch' if row.length != schema.length

      row.each_with_index.map do |val, idx|
        pre_hashed ? val : generate_hash(val, schema[idx])
      end
    end

    is_raw = !(schema.map { |s| is_combinational_key?(s) }.any?)
  end

  {
    payload: {
      schema: schema,
      data: data,
      is_raw: is_raw,
      app_ids: app_ids,
      page_ids: page_ids,
      data_source: data_source,
    }.compact,
    namespace: namespace,
    session: session,
  }.compact
end

#remove_user(data, schema, options = {}) ⇒ Object



45
46
47
# File 'lib/facebook_ads/ad_objects/helpers/custom_audience_helpers.rb', line 45

def remove_user(data, schema, options = {})
  self.users.destroy(prepare_params(data, schema, **options))
end