module Adparlor module Facebook module GraphApi class CustomAudienceUser < GraphObject include Fields::CustomAudienceUser include Traits::Methods field_attrs FIELDS allow_method :destroy, :update FIELD_KEYS = { emails: 'EMAIL_SHA256', phone_numbers: 'PHONE_SHA256', facebook_uids: 'UID', mobile_adversiter_ids: 'MOBILE_ADVERTISER_ID', genders: ['GEN'], first_names: ['FN'], last_name: ['LN'], cities: ['CT'], states: ['ST'], zip_codes: ['ZIP'], mobile_device_ids: ['MADID'] } def destroy(path, options = {}) if @custom_audience_id destroy_path = "/#{custom_audience_id}/users" else raise FbError.new('required parameter account_id missing', 500) unless @account_id account_id = remove_instance_variable(:@account_id) destroy_path = "act_#{account_id}/usersofanyaudience" end ready_payload super(destroy_path, options) end def path raise FbError.new('required parameter custom_audience_id missing', 500) unless custom_audience_id "/#{custom_audience_id}/users" end def update(path, options = {}) ready_payload super(path, options) end private def ready_payload schema, data = [], [] instance_variables.each do |iv| schema_key = FIELD_KEYS[iv.to_s.delete('@').to_sym] schema << schema_key data << instance_variable_get(iv).map { |obj| normalize_object(obj, schema_key) } remove_instance_variable iv end schema = schema.first if schema.length == 1 data = data.first if data.length == 1 @payload = { schema: schema, data: data } end def normalize_object(obj, type) audience_type = type.is_a?(Array) ? type.first : type case audience_type when 'EMAIL_SHA256' Digest::SHA256.hexdigest(obj.downcase.strip) when 'PHONE_SHA256' Digest::SHA256.hexdigest(obj.gsub(/\D/, '')) when 'GEN' if obj.downcase.include?('fe') [Digest::SHA256.hexdigest('f')] else [Digest::SHA256.hexdigest('m')] end when 'LN', 'FN', 'CT' [Digest::SHA256.hexdigest(obj.gsub(/(\W+|[0-9 ]+)/, ''))] when 'ST', 'ZIP', 'MADID' [Digest::SHA256.hexdigest(obj.downcase)] else Digest::SHA256.hexdigest(obj) end end end end end end