Sha256: 3eca05d52559bc83d075b440ce75db82f6e2f292ac4e2b5293a6a429971d5802

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require_relative './api_client'
require_relative './exceptions'

module Hubspot
  # Enumerable class for handling paged data from the API
  class PagedBatch < ApiClient
    include Enumerable

    MAX_LIMIT = 100 # HubSpot max items per page

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

    # rubocop:disable Lint/MissingSuper
    def initialize(url:, params: {}, resource_class: nil, object_ids: [])
      @url = url
      @params = params
      @resource_class = resource_class
      @object_ids = object_ids
    end
    # rubocop:enable Lint/MissingSuper

    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

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

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

    private

    def fetch_page(object_ids)
      params_with_ids = @params.dup || {}
      params_with_ids[:inputs] = object_ids.map { |id| { id: id } }

      response = self.class.post(@url, body: params_with_ids.to_json)

      handle_response(response)
    end

    def process_results(response)
      results = response['results'] || []
      return results unless @resource_class

      results.map { |result| @resource_class.new(result) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_hubspot_api-0.3.3 lib/hubspot/paged_batch.rb
ruby_hubspot_api-0.3.2 lib/hubspot/paged_batch.rb
ruby_hubspot_api-0.3.1 lib/hubspot/paged_batch.rb
ruby_hubspot_api-0.3.0 lib/hubspot/paged_batch.rb