Class: TiktokBusinessApi::Resources::CrudResource
- Inherits:
-
BaseResource
- Object
- BaseResource
- TiktokBusinessApi::Resources::CrudResource
- Defined in:
- lib/tiktok_business_api/resources/crud_resource.rb
Overview
Base CRUD class for all API resources
Instance Attribute Summary
Attributes inherited from BaseResource
Instance Method Summary collapse
-
#create(owner_id, params = {}, owner_param_name = 'advertiser_id') ⇒ Hash
Create a new resource.
-
#create_path ⇒ Object
Default path for create operations.
-
#delete(owner_id, resource_id, owner_param_name = 'advertiser_id') ⇒ Hash
Delete a resource.
-
#delete_path ⇒ Object
Default path for delete operations.
-
#get(owner_id, resource_id, owner_param_name = 'advertiser_id') ⇒ Hash
Get a resource by ID.
-
#id_param_name ⇒ Object
Default ID parameter name.
-
#ids_param_name ⇒ Object
Default IDs parameter name for bulk operations.
- #list(filtering: {}, page_size: nil, page: nil, **other_params) ⇒ Object
-
#list_all(owner_id, params = {}, owner_param_name = 'advertiser_id', list_key = 'list') {|resource| ... } ⇒ Array
List all resources with automatic pagination.
-
#list_path ⇒ Object
Default path for read/list operations.
- #resource_name ⇒ Object
-
#status_update_path ⇒ Object
Default path for status updates.
-
#update(owner_id, resource_id, params = {}, owner_param_name = 'advertiser_id') ⇒ Hash
Update a resource.
-
#update_path ⇒ Object
Default path for update operations.
-
#update_status(owner_id, resource_id, status, owner_param_name = 'advertiser_id') ⇒ Hash
Update resource status.
Methods inherited from BaseResource
#_http_get, #_http_post, #api_version, #base_path, #initialize, #paginate
Constructor Details
This class inherits a constructor from TiktokBusinessApi::Resources::BaseResource
Instance Method Details
#create(owner_id, params = {}, owner_param_name = 'advertiser_id') ⇒ Hash
Create a new resource
52 53 54 55 56 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 52 def create(owner_id, params = {}, owner_param_name = 'advertiser_id') params = params.merge(owner_param_name => owner_id) response = _http_post(create_path, params) response['data'] end |
#create_path ⇒ Object
Default path for create operations
8 9 10 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 8 def create_path 'create/' end |
#delete(owner_id, resource_id, owner_param_name = 'advertiser_id') ⇒ Hash
Delete a resource
191 192 193 194 195 196 197 198 199 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 191 def delete(owner_id, resource_id, owner_param_name = 'advertiser_id') params = { owner_param_name => owner_id, ids_param_name => [resource_id] } response = _http_post(delete_path, params) response['data'] end |
#delete_path ⇒ Object
Default path for delete operations
23 24 25 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 23 def delete_path 'delete/' end |
#get(owner_id, resource_id, owner_param_name = 'advertiser_id') ⇒ Hash
Get a resource by ID
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 138 def get(owner_id, resource_id, owner_param_name = 'advertiser_id') params = { owner_param_name => owner_id, ids_param_name => [resource_id] } response = _http_get(list_path, params) items = response.dig('data', 'list') || [] items.first end |
#id_param_name ⇒ Object
Default ID parameter name
37 38 39 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 37 def id_param_name "#{resource_name}_id" end |
#ids_param_name ⇒ Object
Default IDs parameter name for bulk operations
42 43 44 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 42 def ids_param_name "#{resource_name}_ids" end |
#list(filtering: {}, page_size: nil, page: nil, **other_params) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 58 def list(filtering: {}, page_size: nil, page: nil, **other_params) # Build request paramss request_params = other_params.merge(filtering: filtering.to_json) page_size ||= 100 page ||= 1 request_params[:page_size] = [page_size, 100].min # Most APIs limit page size request_params[:page] = page # Make the request response = _http_get(list_path, request_params) # If a block is given, yield each item if block_given? items = response.dig('data', 'list') || [] items.each { |item| yield(item) } # Return the response for method chaining response else # Just return the data otherwise response['data']['list'] end end |
#list_all(owner_id, params = {}, owner_param_name = 'advertiser_id', list_key = 'list') {|resource| ... } ⇒ Array
List all resources with automatic pagination
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 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 92 def list_all(owner_id, params = {}, owner_param_name = 'advertiser_id', list_key = 'list') items = [] page = 1 page_size = params[:page_size] || 10 has_more = true # Ensure owner_id is included in the params request_params = params.merge(owner_param_name => owner_id) while has_more request_params[:page] = page request_params[:page_size] = page_size response = _http_get(list_path, request_params) # Extract data from the response current_items = response.dig('data', list_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') || {} total_number = page_info['total_number'] || 0 total_fetched = page * page_size has_more = page_info['has_more'] == true || (total_number > 0 && total_fetched < total_number) page += 1 # Break if we've reached the end or there's an empty result break if current_items.empty? end block_given? ? nil : items end |
#list_path ⇒ Object
Default path for read/list operations
13 14 15 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 13 def list_path 'get/' end |
#resource_name ⇒ Object
32 33 34 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 32 def resource_name self::class::RESOURCE_NAME end |
#status_update_path ⇒ Object
Default path for status updates
28 29 30 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 28 def status_update_path 'status/update/' end |
#update(owner_id, resource_id, params = {}, owner_param_name = 'advertiser_id') ⇒ Hash
Update a resource
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 156 def update(owner_id, resource_id, params = {}, owner_param_name = 'advertiser_id') # Ensure required parameters are included params = params.merge( owner_param_name => owner_id, id_param_name => resource_id ) response = _http_post(update_path, params) response['data'] end |
#update_path ⇒ Object
Default path for update operations
18 19 20 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 18 def update_path 'update/' end |
#update_status(owner_id, resource_id, status, owner_param_name = 'advertiser_id') ⇒ Hash
Update resource status
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/tiktok_business_api/resources/crud_resource.rb', line 174 def update_status(owner_id, resource_id, status, owner_param_name = 'advertiser_id') params = { owner_param_name => owner_id, ids_param_name => [resource_id], 'operation_status' => status } response = _http_post(status_update_path, params) response['data'] end |