Class: Hubspot::PagedCollection

Inherits:
ApiClient show all
Includes:
Enumerable, ResourceFilter::FilterGroupMethods
Defined in:
lib/hubspot/paged_collection.rb

Overview

Enumerable class for handling paged data from the API

Constant Summary collapse

MAX_LIMIT =

HubSpot max items per page

200

Constants included from ResourceFilter::FilterGroupMethods

ResourceFilter::FilterGroupMethods::OPERATOR_MAP

Constants inherited from ApiClient

ApiClient::MAX_RETRIES, ApiClient::RETRY_WAIT_TIME

Instance Method Summary collapse

Methods included from ResourceFilter::FilterGroupMethods

#build_filter_groups, #extract_property_and_operator

Methods inherited from ApiClient

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

Constructor Details

#initialize(url:, params: {}, resource_class: nil, method: :get, results_param: 'results') ⇒ PagedCollection

rubocop:disable Lint/MissingSuper



17
18
19
20
21
22
23
# File 'lib/hubspot/paged_collection.rb', line 17

def initialize(url:, params: {}, resource_class: nil, method: :get, results_param: 'results')
  @url = url
  @params = params
  @resource_class = resource_class
  @method = method.to_sym
  @results_param = results_param
end

Instance Method Details

#allObject



43
44
45
46
47
48
49
# File 'lib/hubspot/paged_collection.rb', line 43

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

#each(&block) ⇒ Object

rubocop:enable Metrics/MethodLength



73
74
75
76
77
# File 'lib/hubspot/paged_collection.rb', line 73

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

#each_pageObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hubspot/paged_collection.rb', line 30

def each_page
  offset = nil
  loop do
    response = fetch_page(offset)
    @total = response['total'] if response['total']
    mapped_results = process_results(response)
    yield mapped_results unless mapped_results.empty?
    sleep wait_between_pages
    offset = response.dig('paging', 'next', 'after')
    break unless offset
  end
end

#first(limit = 1) ⇒ Object

Override Enumerable’s first method so as not to have to call each (via all) rubocop:disable Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hubspot/paged_collection.rb', line 53

def first(limit = 1)
  resources = []
  remaining = limit

  original_limit = @params.delete(:limit)
  # Modify @params directly to set the limit
  @params[:limit] = [remaining, MAX_LIMIT].min

  # loop through pages in case limit is more than the max limit
  each_page do |page|
    resources.concat(page)
    remaining -= page.size
    break if remaining <= 0
  end

  @params[:limit] = original_limit
  limit == 1 ? resources.first : resources.first(limit)
end

#select(*properties) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/hubspot/paged_collection.rb', line 97

def select(*properties)
  unless properties.blank?
    @params ||= {}
    @params[:properties] ||= []
    @params[:properties].concat(properties.flatten)
  end

  self
end

#totalObject

rubocop:enable Lint/MissingSuper



26
27
28
# File 'lib/hubspot/paged_collection.rb', line 26

def total
  @total ||= determine_total
end

#where(filters = {}) ⇒ Object



79
80
81
# File 'lib/hubspot/paged_collection.rb', line 79

def where(filters = {})
  dup.where!(filters)
end

#where!(filters = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hubspot/paged_collection.rb', line 83

def where!(filters = {})
  # Build new filter groups from the provided filters
  new_filter_groups = build_filter_groups(filters)

  # Merge new filters into the existing params structure
  if @params[:filterGroups]&.any?
    @params[:filterGroups][0][:filters].concat(new_filter_groups[0][:filters])
  else
    @params[:filterGroups] = new_filter_groups
  end

  self # Returning self for method chaining if needed
end