Class: TiktokBusinessApi::Resources::Identity

Inherits:
BaseResource
  • Object
show all
Defined in:
lib/tiktok_business_api/resources/identity.rb

Overview

Identity resource for the TikTok Business API

Constant Summary collapse

RESOURCE_NAME =
'identity'

Instance Attribute Summary

Attributes inherited from BaseResource

#client

Instance Method Summary collapse

Methods inherited from BaseResource

#_http_get, #_http_post, #api_version, #base_path, #initialize, #paginate, #resource_name

Constructor Details

This class inherits a constructor from TiktokBusinessApi::Resources::BaseResource

Instance Method Details

#create(advertiser_id:, display_name:, image_uri: nil) ⇒ Hash

Create a new Custom User identity

Parameters:

  • advertiser_id (String)

    Advertiser ID

  • display_name (String)

    Display name (max 100 characters)

  • image_uri (String) (defaults to: nil)

    The ID of the avatar image (optional)

Returns:

  • (Hash)

    Response containing the new identity ID



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tiktok_business_api/resources/identity.rb', line 68

def create(advertiser_id:, display_name:, image_uri: nil)
  params = {
    advertiser_id: advertiser_id,
    display_name: display_name
  }

  # Add image URI if provided
  params[:image_uri] = image_uri if image_uri

  response = client.request(:post, "#{base_path}create/", params)
  response['data']
end

#get_info(advertiser_id:, identity_id:, identity_type:, identity_authorized_bc_id: nil) ⇒ Hash

Get information about a specific identity

Parameters:

  • advertiser_id (String)

    Advertiser ID

  • identity_id (String)

    Identity ID

  • identity_type (String)

    Identity type. Enum values: CUSTOMIZED_USER, AUTH_CODE, TT_USER, BC_AUTH_TT

  • identity_authorized_bc_id (String) (defaults to: nil)

    ID of the Business Center (required when identity_type is BC_AUTH_TT)

Returns:

  • (Hash)

    Identity information



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tiktok_business_api/resources/identity.rb', line 48

def get_info(advertiser_id:, identity_id:, identity_type:, identity_authorized_bc_id: nil)
  params = {
    advertiser_id: advertiser_id,
    identity_id: identity_id,
    identity_type: identity_type
  }

  # Add Business Center ID if provided (required for BC_AUTH_TT)
  params[:identity_authorized_bc_id] = identity_authorized_bc_id if identity_authorized_bc_id

  response = client.request(:get, "#{base_path}info/", params)
  response['data']['identity_info']
end

#list(advertiser_id:, **options) ⇒ Hash

Get a list of identities

Parameters:

  • advertiser_id (String)

    Advertiser ID

  • options (Hash)

    Additional options for the request

Options Hash (**options):

  • :identity_type (String)

    Identity type. Enum values: CUSTOMIZED_USER, AUTH_CODE, TT_USER, BC_AUTH_TT

  • :identity_authorized_bc_id (String)

    ID of the Business Center (required when identity_type is BC_AUTH_TT)

  • :filtering (Hash)

    Filtering conditions (valid only for CUSTOMIZED_USER or when identity_type not specified)

  • :page (Integer)

    Page number

  • :page_size (Integer)

    Number of results per page

Returns:

  • (Hash)

    Response with list of identities and pagination info



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tiktok_business_api/resources/identity.rb', line 19

def list(advertiser_id:, **options)
  params = {
    advertiser_id: advertiser_id
  }

  # Add optional parameters if provided
  params[:identity_type] = options[:identity_type] if options[:identity_type]
  params[:identity_authorized_bc_id] = options[:identity_authorized_bc_id] if options[:identity_authorized_bc_id]
  params[:filtering] = options[:filtering].to_json if options[:filtering]
  params[:page] = options[:page] if options[:page]
  params[:page_size] = options[:page_size] if options[:page_size]

  response = client.request(:get, "#{base_path}get/", params)

  if block_given? && response['data']['identity_list']
    response['data']['identity_list'].each { |identity| yield(identity) }
    response['data']
  else
    response['data']
  end
end

#list_all(advertiser_id:, **options) {|identity| ... } ⇒ Array?

List all identities with automatic pagination

Parameters:

  • advertiser_id (String)

    Advertiser ID

  • options (Hash)

    Additional options for the request

Yields:

  • (identity)

    Block to process each identity

Yield Parameters:

  • identity (Hash)

    Identity from the response

Returns:

  • (Array, nil)

    All identities if no block is given, nil otherwise



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tiktok_business_api/resources/identity.rb', line 88

def list_all(advertiser_id:, **options, &block)
  page = options[:page] || 1
  page_size = options[:page_size] || 100
  all_identities = []
  has_more = true

  while has_more
    current_options = options.merge(page: page, page_size: page_size)
    response = list(advertiser_id: advertiser_id, **current_options)

    identities = response['identity_list'] || []

    if block_given?
      identities.each { |identity| yield(identity) }
    else
      all_identities.concat(identities)
    end

    # Check pagination info
    page_info = response['page_info'] || {}
    current_page = page_info['page'].to_i
    total_pages = page_info['total_page'].to_i

    has_more = current_page < total_pages
    page += 1

    # Break if empty result or no more pages
    break if identities.empty? || !has_more
  end

  block_given? ? nil : all_identities
end