Class: Hubspot::PagedBatch

Inherits:
ApiClient show all
Includes:
Enumerable
Defined in:
lib/hubspot/paged_batch.rb

Overview

Enumerable class for handling paged data from the API

Constant Summary collapse

MAX_LIMIT =

HubSpot max items per page

100

Constants inherited from ApiClient

ApiClient::MAX_RETRIES, ApiClient::RETRY_WAIT_TIME

Instance Method Summary collapse

Methods inherited from ApiClient

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

Constructor Details

#initialize(url:, params: {}, resource_class: nil, object_ids: []) ⇒ PagedBatch

rubocop:disable Lint/MissingSuper



24
25
26
27
28
29
# File 'lib/hubspot/paged_batch.rb', line 24

def initialize(url:, params: {}, resource_class: nil, object_ids: [])
  @url = url
  @params = params
  @resource_class = resource_class
  @object_ids = object_ids
end

Instance Method Details

#allObject



40
41
42
43
44
45
46
# File 'lib/hubspot/paged_batch.rb', line 40

def all
  results = []
  each_page do |page|
    results.concat(page)
  end
  results
end

#each(&block) ⇒ Object



48
49
50
51
52
# File 'lib/hubspot/paged_batch.rb', line 48

def each(&block)
  each_page do |page|
    page.each(&block)
  end
end

#each_pageObject

rubocop:enable Lint/MissingSuper



32
33
34
35
36
37
38
# File 'lib/hubspot/paged_batch.rb', line 32

def each_page
  @object_ids.each_slice(MAX_LIMIT) do |ids|
    response = fetch_page(ids)
    mapped_results = process_results(response)
    yield mapped_results unless mapped_results.empty?
  end
end

#inspectObject

:nocov:



14
15
16
17
18
19
20
# File 'lib/hubspot/paged_batch.rb', line 14

def inspect
  "#<#{self.class.name} " \
  "@url=#{@url.inspect}, " \
  "@params=#{@params.inspect}, " \
  "@resource_class=#{@resource_class.inspect}, " \
  "@object_ids_count=#{@object_ids.size}>"
end