class FacebookAds::AdTargeting

developers.facebook.com/docs/marketing-api/targeting-specs

Constants

ANDROID_DEVICES
ANDROID_OS
APPLE_DEVICES
APPLE_OS
APP_INSTALL_STATES
DEVICES
GENDERS
INSTALLED
MEN
NOT_INSTALLED
OSES
WOMEN

Attributes

age_max[RW]
age_min[RW]
app_install_state[RW]
countries[RW]
custom_locations[RW]
genders[RW]
income[RW]
user_device[RW]
user_os[RW]

Public Class Methods

new() click to toggle source
# File lib/facebook_ads/ad_targeting.rb, line 21
def initialize
  # self.genders = [WOMEN] # If nil, defaults to all genders.
  # self.age_min = 18 # If nil, defaults to 18.
  # self.age_max = 65 # If nil, defaults to 65+.
  # self.user_os = [ANDROID_OS]
  # self.user_device = ANDROID_DEVICES
  # self.app_install_state = NOT_INSTALLED
  self.income = [] # An a rray of objects with 'id' and optional 'name'
end

Public Instance Methods

geo_locations() click to toggle source
# File lib/facebook_ads/ad_targeting.rb, line 31
def geo_locations
  if custom_locations
    { custom_locations: custom_locations }
  elsif countries
    { countries: countries }
  else
    { countries: ['US'] }
  end
end
to_hash() click to toggle source
# File lib/facebook_ads/ad_targeting.rb, line 57
def to_hash
  {
    genders: genders,
    age_min: age_min,
    age_max: age_max,
    geo_locations: geo_locations,
    user_os: user_os,
    user_device: user_device,
    app_install_state: app_install_state,
    income: income
  }.reject { |_k, v| v.nil? }
end
validate!() click to toggle source
# File lib/facebook_ads/ad_targeting.rb, line 41
def validate!
  { gender: genders, countries: countries, user_os: user_os, user_device: user_device, custom_locations: custom_locations }.each_pair do |key, array|
    raise ArgumentError, "#{self.class.name}: #{key} must be an array" if !array.nil? && !array.is_a?(Array)
  end

  { genders: [genders, GENDERS], user_os: [user_os, OSES], user_device: [user_device, DEVICES] }.each_pair do |key, provided_and_acceptable|
    provided, acceptable = provided_and_acceptable

    if !provided.nil? && !(invalid = provided.detect { |value| !acceptable.include?(value) }).nil?
      raise ArgumentError, "#{self.class.name}: #{invalid} is an invalid #{key}"
    end
  end

  true
end