class LabClient::PaginatedResponse

Additional Pagination Support First page is always request prior to this class being instantiated

Attributes

array[RW]
client[RW]
response[RW]

Public Class Methods

new(klass, response, client) click to toggle source
# File lib/labclient/paginated_response.rb, line 9
def initialize(klass, response, client)
  @klass = klass
  @response = response
  @client = client

  @array = response.data.map { |entry| process_entry(entry, response) }

  auto_paginate if client.settings[:paginate]
end

Public Instance Methods

auto_paginate() { |array| ... } click to toggle source
# File lib/labclient/paginated_response.rb, line 50
def auto_paginate(&block)
  yield @array if block_given?

  loop do
    break unless next_page?

    next_page(&block)
  end

  @array
end
each_page() { |array| ... } click to toggle source
# File lib/labclient/paginated_response.rb, line 45
def each_page(&block)
  yield @array # This will eventually be the whole list
  auto_paginate(&block)
end
help() click to toggle source
# File lib/labclient/paginated_response.rb, line 31
    def help
      puts <<~DOC
        Pagination Helper Methods
          auto_paginate
            Automatically collect and return all results for a query. Accepts block
          paginate_with_limit
            Iterate through pages, but end when a certain limit is reached
          each_page
            Similar with auto_paginate, you can call each_page directly
      DOC
    end
inspect() click to toggle source
# File lib/labclient/paginated_response.rb, line 19
def inspect
  @array
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/labclient/paginated_response.rb, line 23
def method_missing(name, *args, &block)
  if @array.respond_to?(name)
    @array.send(name, *args, &block)
  else
    super
  end
end
next_page() { |results| ... } click to toggle source
# File lib/labclient/paginated_response.rb, line 92
def next_page
  return false unless next_page?

  @response = client.http.request(:get, @link)

  raise LabClient::Error.new(@response), @response.friendly_error unless @response.success?

  results = process
  @array.concat results
  yield results if block_given?
end
next_page?() click to toggle source

Check for and store next page

# File lib/labclient/paginated_response.rb, line 77
def next_page?
  text = @response.headers['link']&.split(',')&.find { |x| x.include? 'next' }
  return nil if text.nil?

  @link = link_regex.match(text.strip)[1]

  return false if @link.nil?

  true
end
page() click to toggle source
# File lib/labclient/paginated_response.rb, line 128
def page
  response.headers['x-page'].to_i
end
paginate_with_limit(limit) { |array| ... } click to toggle source

Paginate to a limit

# File lib/labclient/paginated_response.rb, line 63
def paginate_with_limit(limit, &block)
  yield @array if block_given?

  loop do
    break unless next_page?
    break if @array.length >= limit

    next_page(&block)
  end

  @array
end
process() click to toggle source
# File lib/labclient/paginated_response.rb, line 109
def process
  @response.data.map do |entry|
    process_entry(entry, @response)
  end
end
process_entry(entry, entry_response) click to toggle source

Create Class Objects

# File lib/labclient/paginated_response.rb, line 105
def process_entry(entry, entry_response)
  @klass ? @klass.new(entry, entry_response, client) : entry
end
respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/labclient/paginated_response.rb, line 43
def respond_to_missing?(method_name, include_private = false); end
success?() click to toggle source

Forward response success

# File lib/labclient/paginated_response.rb, line 116
def success?
  @response.success?
end
total() click to toggle source
# File lib/labclient/paginated_response.rb, line 124
def total
  response.headers['x-total'].to_i
end
total_pages() click to toggle source
# File lib/labclient/paginated_response.rb, line 120
def total_pages
  response.headers['x-total-pages'].to_i
end