Sha256: 0d0f4a987e6556b830aca3c531cb03fcf97836aa171606b2730cec95e33343e5

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require_relative 'base_endpoint'

module Panoptes
  module Endpoints
    class JsonApiEndpoint < BaseEndpoint
      # Get a path and perform automatic depagination
      def paginate(path, query, resource: nil)
        resource = path.split('/').last if resource.nil?
        data = last_response = get(path, query)

        # ensure we yield the first page of data
        yield data, last_response if block_given?

        # while we have more result set pages
        while (next_path = last_response['meta'][resource]['next_href'])
          # fetch next page of data
          last_response = get(next_path, query)
          if block_given?
            # if we pass a block then yield the first page and current page responses
            yield data, last_response
          else
            # add to the data representation of all result set pages
            data[resource].concat(last_response[resource]) if data[resource].is_a?(Array)
            data['meta'][resource].merge!(last_response['meta'][resource])
            data['links'].merge!(last_response['links'])
          end
        end

        # return the data results for when we don't use a block to process response pages
        data
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panoptes-client-1.2.1 lib/panoptes/endpoints/json_api_endpoint.rb
panoptes-client-1.2.0 lib/panoptes/endpoints/json_api_endpoint.rb
panoptes-client-1.1.1 lib/panoptes/endpoints/json_api_endpoint.rb