Sha256: c96133f265118bd8392496d6196cc492397ecbd54440485e196fd29e33d3f3ab

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'intercom/utils'
require 'intercom/base_collection_proxy'

module Intercom
  class ClientCollectionProxy < BaseCollectionProxy
    def each(&block)
      next_page = nil
      current_page = nil
      loop do
        response_hash = fetch(next_page)
        raise Intercom::HttpError, 'Http Error - No response entity returned' unless response_hash

        current_page = extract_current_page(response_hash)
        deserialize_response_hash(response_hash, block)
        next_page = extract_next_link(response_hash)
        break if next_page.nil? || (@params[:page] && (current_page >= @params[:page]))
      end
      self
    end

    def fetch(next_page)
      if next_page
        @client.get(next_page, {})
      else
        @client.get(@url, @params)
      end
    end

    private

    def paging_info_present?(response_hash)
      !!(response_hash['pages'] && response_hash['pages']['type'])
    end

    def extract_next_link(response_hash)
      return nil unless paging_info_present?(response_hash)

      paging_info = response_hash.delete('pages')
      paging_info['next']
    end

    def extract_current_page(response_hash)
      return nil unless paging_info_present?(response_hash)

      response_hash['pages']['page']
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
intercom-4.2.1 lib/intercom/client_collection_proxy.rb
intercom-4.2.0 lib/intercom/client_collection_proxy.rb
intercom-4.1.3 lib/intercom/client_collection_proxy.rb
intercom-4.1.2 lib/intercom/client_collection_proxy.rb
intercom-4.1.1 lib/intercom/client_collection_proxy.rb
intercom-4.1.0 lib/intercom/client_collection_proxy.rb
intercom-4.0.1 lib/intercom/client_collection_proxy.rb
intercom-4.0.0 lib/intercom/client_collection_proxy.rb