Class: Hubspot::Batch

Inherits:
ApiClient show all
Defined in:
lib/hubspot/batch.rb

Overview

Class to handle batch updates of resources rubocop:disable Metrics/ClassLength

Constant Summary collapse

CONTACT_LIMIT =
10
DEFAULT_LIMIT =
100

Constants inherited from ApiClient

ApiClient::MAX_RETRIES, ApiClient::RETRY_WAIT_TIME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApiClient

delete, get, #handle_response, handle_response, log_request, patch, post

Constructor Details

#initialize(resources = [], id_property: 'id', resource_matcher: nil) ⇒ Batch

rubocop:disable Lint/MissingSuper



45
46
47
48
49
50
51
52
# File 'lib/hubspot/batch.rb', line 45

def initialize(resources = [], id_property: 'id', resource_matcher: nil)
  validate_resource_matcher(resource_matcher)

  @resources = []
  @id_property = id_property # Set id_property for the batch (default: 'id')
  @responses = []            # Store multiple BatchResponse objects here
  resources.each { |resource| add_resource(resource) }
end

Instance Attribute Details

#id_propertyObject

Returns the value of attribute id_property.



29
30
31
# File 'lib/hubspot/batch.rb', line 29

def id_property
  @id_property
end

#resourcesObject

Returns the value of attribute resources.



29
30
31
# File 'lib/hubspot/batch.rb', line 29

def resources
  @resources
end

#responsesObject

Returns the value of attribute responses.



29
30
31
# File 'lib/hubspot/batch.rb', line 29

def responses
  @responses
end

Class Method Details

.read(object_class, object_ids = [], properties: [], id_property: 'id') ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/hubspot/batch.rb', line 305

def read(object_class, object_ids = [], properties: [], id_property: 'id')
  unless object_class < Hubspot::Resource
    raise ArgumentError, 'Must be a valid Hubspot resource class'
  end

  # fetch all the matching resources with paging handled
  resources = object_class.batch_read(object_ids,
                                      properties: properties,
                                      id_property: id_property).all

  # return instance of Hubspot::Batch with the resources set
  new(resources, id_property: id_property)
end

Instance Method Details

#add_resource(resource) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hubspot/batch.rb', line 103

def add_resource(resource)
  if @resources.any?
    if @resources.first.resource_name != resource.resource_name
      raise ArgumentError, 'All resources in a batch must be of the same type'
    end
  else
    add_resource_method(resource.resource_name)
  end

  @resources << resource
end

#all_successful?Boolean

Check if all responses were successful

Returns:

  • (Boolean)


89
90
91
# File 'lib/hubspot/batch.rb', line 89

def all_successful?
  @responses.all?(&:all_successful?)
end

#any_changes?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/hubspot/batch.rb', line 115

def any_changes?
  @resources.any?(&:changes?)
end

#any_failed?Boolean

Check if any responses failed

Returns:

  • (Boolean)


99
100
101
# File 'lib/hubspot/batch.rb', line 99

def any_failed?
  @responses.any? { |response| !response.all_successful? && !response.partial_success? }
end

#archiveObject

Archive method



84
85
86
# File 'lib/hubspot/batch.rb', line 84

def archive
  save(action: 'archive')
end

#createObject

batch create from the resources



56
57
58
# File 'lib/hubspot/batch.rb', line 56

def create
  save(action: 'create')
end

#inspectObject

:nocov:



35
36
37
38
39
40
41
# File 'lib/hubspot/batch.rb', line 35

def inspect
  "#<#{self.class.name} " \
  "@resource_count=#{@resources.size}, " \
  "@id_property=#{@id_property.inspect}, " \
  "@resource_type=#{@resources.first&.resource_name}, " \
  "@responses_count=#{@responses.size}>"
end

#partial_success?Boolean

Check if some responses were successful and others failed

Returns:

  • (Boolean)


94
95
96
# File 'lib/hubspot/batch.rb', line 94

def partial_success?
  @responses.any?(&:partial_success?) && @responses.none?(&:all_successful?)
end

#updateObject



60
61
62
63
# File 'lib/hubspot/batch.rb', line 60

def update
  # validate_update_conditions
  save(action: 'update')
end

#upsert(resource_matcher: nil) ⇒ Object

Upsert method that calls save with upsert action



66
67
68
69
70
71
# File 'lib/hubspot/batch.rb', line 66

def upsert(resource_matcher: nil)
  validate_resource_matcher(resource_matcher)

  validate_upsert_conditions
  save(action: 'upsert')
end

#validate_resource_matcher(resource_matcher) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/hubspot/batch.rb', line 73

def validate_resource_matcher(resource_matcher)
  return if resource_matcher.blank?

  unless resource_matcher.is_a?(Proc) && resource_matcher.arity == 2
    raise ArgumentError, 'resource_matcher must be a proc that accepts exactly 2 arguments'
  end

  @resource_matcher = resource_matcher
end