Class: TiktokBusinessApi::Resources::BaseResource

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

Overview

Base class for all API resources

Direct Known Subclasses

Campaign

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BaseResource

Initialize a new resource

Parameters:



13
14
15
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 13

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientTiktokBusinessApi::Client (readonly)

Returns Client instance.

Returns:



8
9
10
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 8

def client
  @client
end

Instance Method Details

#_http_get(path, params = {}, headers = {}) ⇒ Hash

Make a GET request to the resource

Parameters:

  • path (String)

    Path relative to the resource base path

  • params (Hash) (defaults to: {})

    Query parameters

  • headers (Hash) (defaults to: {})

    Custom headers

Returns:

  • (Hash)

    Response data



44
45
46
47
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 44

def _http_get(path, params = {}, headers = {})
  full_path = File.join(base_path, path)
  client.request(:get, full_path, params, headers)
end

#_http_post(path, params = {}, headers = {}) ⇒ Hash

Make a POST request to the resource

Parameters:

  • path (String)

    Path relative to the resource base path

  • params (Hash) (defaults to: {})

    Body parameters

  • headers (Hash) (defaults to: {})

    Custom headers

Returns:

  • (Hash)

    Response data



55
56
57
58
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 55

def _http_post(path, params = {}, headers = {})
  full_path = File.join(base_path, path)
  client.request(:post, full_path, params, headers)
end

#api_versionString

Get the API version

Returns:

  • (String)

    API version



27
28
29
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 27

def api_version
  'v1.3'
end

#base_pathString

Get the base path for this resource

Returns:

  • (String)

    Base path



34
35
36
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 34

def base_path
  "#{api_version}/#{resource_name}/"
end

#paginate(path, params = {}, headers = {}, data_key = 'data') {|item| ... } ⇒ Array

Handle pagination for list endpoints

Parameters:

  • path (String)

    Path relative to the resource base path

  • params (Hash) (defaults to: {})

    Query parameters

  • headers (Hash) (defaults to: {})

    Custom headers

  • data_key (String) (defaults to: 'data')

    Key in the response that contains the data array

Yields:

  • (item)

    Block to process each item

Yield Parameters:

  • item (Hash)

    Item from the response

Returns:

  • (Array)

    All items if no block is given



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
95
96
97
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 69

def paginate(path, params = {}, headers = {}, data_key = 'data')
  items = []
  page = 1
  page_size = params[:page_size] || 10
  has_more = true
  
  while has_more
    params[:page] = page
    params[:page_size] = page_size
    
    response = get(path, params, headers)
    
    # Extract data from the response
    current_items = response.dig('data', data_key) || []
    
    if block_given?
      current_items.each { |item| yield(item) }
    else
      items.concat(current_items)
    end
    
    # Check if there are more pages
    page_info = response.dig('data', 'page_info') || {}
    has_more = page_info['has_more'] == true
    page += 1
  end
  
  block_given? ? nil : items
end

#resource_nameString

Get the resource name (used for endpoint paths)

Returns:

  • (String)

    Resource name



20
21
22
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 20

def resource_name
  self.class.name.split('::').last.downcase
end