Class: XIVAPI::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HTTP
Defined in:
lib/xivapi/paginator.rb

Overview

Paginates XIVAPI results

Constant Summary

Constants included from HTTP

HTTP::API_BASE, HTTP::STAGING_API_BASE

Instance Method Summary collapse

Methods included from HTTP

#request

Constructor Details

#initialize(client, params, endpoint, limit, body = nil, per_page = limit) ⇒ Paginator

Returns a new instance of Paginator

Parameters:

  • client (XIVAPI::Client)

    Client that is sending the request

  • params (Hash)

    Query parameters

  • endpoint (String)

    API endpoint

  • limit (Integer)

    Total number of results to limit to

  • body (Hash) (defaults to: nil)

    Request body (for advanced search)

  • per_page (Integer) (defaults to: limit)

    Number of results per page, defaults to limit



13
14
15
16
17
18
19
# File 'lib/xivapi/paginator.rb', line 13

def initialize(client, params, endpoint, limit, body = nil, per_page = limit)
  @client = client
  @params = params.merge(limit: per_page)
  @endpoint = endpoint
  @limit = limit
  @body = body
end

Instance Method Details

#eachObject

An enumerator for XIVAPI results



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xivapi/paginator.rb', line 22

def each
  total = 0
  next_page = 1

  while next_page && total < @limit
    page = self.next(next_page)
    page.results.take(@limit - total).each { |result| yield result }
    next_page = page.next_page
    total += page.results.size
  end
end

#next(page) ⇒ Object

The next page in the enumeration of results



35
36
37
38
# File 'lib/xivapi/paginator.rb', line 35

def next(page)
  response = request(@client, @endpoint, @params.merge(page: page), @body)
  Page.new(response)
end