Class: Attio::Resources::Bulk Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Bulk operations for efficient batch processing
Constant Summary collapse
- MAX_BATCH_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Maximum number of records per bulk operation
100
Instance Method Summary collapse
-
#add_list_entries(list_id:, entries:, options: {}) ⇒ Hash
private
Bulk add entries to a list.
-
#create_records(object:, records:, options: {}) ⇒ Hash
private
Bulk create multiple records.
-
#delete_records(object:, ids:, options: {}) ⇒ Hash
private
Bulk delete multiple records.
- #initialize_merged_result(batch_count) ⇒ Object private private
- #merge_batch_results(results) ⇒ Object private private
- #merge_single_result!(merged, result) ⇒ Object private private
- #merge_statistics!(target, source) ⇒ Object private private
-
#remove_list_entries(list_id:, entry_ids:, options: {}) ⇒ Hash
private
Bulk remove entries from a list.
-
#update_records(object:, updates:, options: {}) ⇒ Hash
private
Bulk update multiple records.
-
#upsert_records(object:, records:, match_attribute:, options: {}) ⇒ Hash
private
Bulk upsert records (create or update based on matching criteria).
- #validate_array!(array, name, operation) ⇒ Object private private
- #validate_bulk_ids!(ids) ⇒ Object private private
- #validate_bulk_records!(records, operation) ⇒ Object private private
- #validate_bulk_updates!(updates) ⇒ Object private private
- #validate_id_item!(id, index) ⇒ Object private private
- #validate_max_size!(array, name) ⇒ Object private private
- #validate_update_item!(update, index) ⇒ Object private private
Constructor Details
This class inherits a constructor from Attio::Resources::Base
Instance Method Details
#add_list_entries(list_id:, entries:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk add entries to a list
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/attio/resources/bulk.rb', line 149 def add_list_entries(list_id:, entries:, options: {}) validate_id!(list_id, "List") validate_bulk_records!(entries, "add to list") batches = entries.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { entries: batch, partial_success: .fetch(:partial_success, false), } result = request(:post, "lists/#{list_id}/entries/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#create_records(object:, records:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk create multiple records
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/attio/resources/bulk.rb', line 36 def create_records(object:, records:, options: {}) validate_required_string!(object, "Object type") validate_bulk_records!(records, "create") batches = records.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { records: batch.map { |record| { data: record } }, partial_success: .fetch(:partial_success, false), return_records: .fetch(:return_records, true), } result = request(:post, "objects/#{object}/records/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#delete_records(object:, ids:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk delete multiple records
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/attio/resources/bulk.rb', line 93 def delete_records(object:, ids:, options: {}) validate_required_string!(object, "Object type") validate_bulk_ids!(ids) batches = ids.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { ids: batch, partial_success: .fetch(:partial_success, false), } result = request(:delete, "objects/#{object}/records/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#initialize_merged_result(batch_count) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/attio/resources/bulk.rb', line 259 private def initialize_merged_result(batch_count) { "success" => true, "total_batches" => batch_count, "records" => [], "errors" => [], "statistics" => { "created" => 0, "updated" => 0, "deleted" => 0, "failed" => 0, }, } end |
#merge_batch_results(results) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
249 250 251 252 253 254 255 256 257 |
# File 'lib/attio/resources/bulk.rb', line 249 private def merge_batch_results(results) merged = initialize_merged_result(results.size) results.each do |result| merge_single_result!(merged, result) end merged end |
#merge_single_result!(merged, result) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
274 275 276 277 278 279 |
# File 'lib/attio/resources/bulk.rb', line 274 private def merge_single_result!(merged, result) merged["records"].concat(result["records"] || []) merged["errors"].concat(result["errors"] || []) merge_statistics!(merged["statistics"], result["statistics"]) merged["success"] &&= result["success"] != false end |
#merge_statistics!(target, source) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
281 282 283 284 285 286 287 |
# File 'lib/attio/resources/bulk.rb', line 281 private def merge_statistics!(target, source) return unless source %w[created updated deleted failed].each do |key| target[key] += source[key] || 0 end end |
#remove_list_entries(list_id:, entry_ids:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk remove entries from a list
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/attio/resources/bulk.rb', line 175 def remove_list_entries(list_id:, entry_ids:, options: {}) validate_id!(list_id, "List") validate_bulk_ids!(entry_ids) batches = entry_ids.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { entry_ids: batch, partial_success: .fetch(:partial_success, false), } result = request(:delete, "lists/#{list_id}/entries/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#update_records(object:, updates:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk update multiple records
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/attio/resources/bulk.rb', line 65 def update_records(object:, updates:, options: {}) validate_required_string!(object, "Object type") validate_bulk_updates!(updates) batches = updates.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { updates: batch, partial_success: .fetch(:partial_success, false), return_records: .fetch(:return_records, true), } result = request(:patch, "objects/#{object}/records/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#upsert_records(object:, records:, match_attribute:, options: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Bulk upsert records (create or update based on matching criteria)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/attio/resources/bulk.rb', line 120 def upsert_records(object:, records:, match_attribute:, options: {}) validate_required_string!(object, "Object type") validate_required_string!(match_attribute, "Match attribute") validate_bulk_records!(records, "upsert") batches = records.each_slice(MAX_BATCH_SIZE).to_a results = [] batches.each_with_index do |batch, index| body = { records: batch.map { |record| { data: record } }, match_attribute: match_attribute, partial_success: .fetch(:partial_success, false), return_records: .fetch(:return_records, true), } result = request(:put, "objects/#{object}/records/bulk", body) results << result.merge("batch" => index + 1) end merge_batch_results(results) end |
#validate_array!(array, name, operation) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
236 237 238 239 240 |
# File 'lib/attio/resources/bulk.rb', line 236 private def validate_array!(array, name, operation) raise ArgumentError, "#{name} array is required for #{operation}" if array.nil? raise ArgumentError, "#{name} must be an array for #{operation}" unless array.is_a?(Array) raise ArgumentError, "#{name} array cannot be empty for #{operation}" if array.empty? end |
#validate_bulk_ids!(ids) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
221 222 223 224 225 226 227 228 |
# File 'lib/attio/resources/bulk.rb', line 221 private def validate_bulk_ids!(ids) validate_array!(ids, "IDs", "bulk operation") validate_max_size!(ids, "IDs") ids.each_with_index do |id, index| validate_id_item!(id, index) end end |
#validate_bulk_records!(records, operation) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/attio/resources/bulk.rb', line 195 private def validate_bulk_records!(records, operation) raise ArgumentError, "Records array is required for bulk #{operation}" if records.nil? raise ArgumentError, "Records must be an array for bulk #{operation}" unless records.is_a?(Array) raise ArgumentError, "Records array cannot be empty for bulk #{operation}" if records.empty? raise ArgumentError, "Too many records (max 1000)" if records.size > MAX_BATCH_SIZE * 10 records.each_with_index do |record, index| raise ArgumentError, "Record at index #{index} must be a hash" unless record.is_a?(Hash) end end |
#validate_bulk_updates!(updates) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
206 207 208 209 210 211 212 213 |
# File 'lib/attio/resources/bulk.rb', line 206 private def validate_bulk_updates!(updates) validate_array!(updates, "Updates", "bulk update") validate_max_size!(updates, "updates") updates.each_with_index do |update, index| validate_update_item!(update, index) end end |
#validate_id_item!(id, index) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
230 231 232 233 234 |
# File 'lib/attio/resources/bulk.rb', line 230 private def validate_id_item!(id, index) return unless id.nil? || id.to_s.strip.empty? raise ArgumentError, "ID at index #{index} cannot be nil or empty" end |
#validate_max_size!(array, name) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
242 243 244 245 246 247 |
# File 'lib/attio/resources/bulk.rb', line 242 private def validate_max_size!(array, name) max = MAX_BATCH_SIZE * 10 return unless array.size > max raise ArgumentError, "Too many #{name} (max #{max})" end |
#validate_update_item!(update, index) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
215 216 217 218 219 |
# File 'lib/attio/resources/bulk.rb', line 215 private def validate_update_item!(update, index) raise ArgumentError, "Update at index #{index} must be a hash" unless update.is_a?(Hash) raise ArgumentError, "Update at index #{index} must have an :id" unless update[:id] raise ArgumentError, "Update at index #{index} must have :data" unless update[:data] end |