Class: TiktokBusinessApi::Resources::BaseResource
- Inherits:
-
Object
- Object
- TiktokBusinessApi::Resources::BaseResource
- Defined in:
- lib/tiktok_business_api/resources/base_resource.rb
Overview
Base class for all API resources
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ TiktokBusinessApi::Client
readonly
Client instance.
Instance Method Summary collapse
-
#_http_get(path, params = {}, headers = {}) ⇒ Hash
Make a GET request to the resource.
-
#_http_post(path, params = {}, headers = {}) ⇒ Hash
Make a POST request to the resource.
-
#api_version ⇒ String
Get the API version.
-
#base_path ⇒ String
Get the base path for this resource.
-
#initialize(client) ⇒ BaseResource
constructor
Initialize a new resource.
-
#paginate(path, params = {}, headers = {}, data_key = 'data') {|item| ... } ⇒ Array
Handle pagination for list endpoints.
-
#resource_name ⇒ String
Get the resource name (used for endpoint paths).
Constructor Details
#initialize(client) ⇒ BaseResource
Initialize a new resource
13 14 15 |
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 13 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ TiktokBusinessApi::Client (readonly)
Returns Client instance.
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
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
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_version ⇒ String
Get the API version
27 28 29 |
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 27 def api_version 'v1.3' end |
#base_path ⇒ String
Get the base path for this resource
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
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_name ⇒ String
Get the resource name (used for endpoint paths)
20 21 22 |
# File 'lib/tiktok_business_api/resources/base_resource.rb', line 20 def resource_name self.class.name.split('::').last.downcase end |