Class: Printavo::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/printavo/resources/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(graphql) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/printavo/resources/base.rb', line 7

def initialize(graphql)
  @graphql = graphql
end

Instance Method Details

#all_pages(first: 25, **kwargs) ⇒ Array

Returns all records across all pages as a flat Array. Extra keyword arguments are forwarded to fetch_page (e.g. order_id: for Jobs).

Examples:

all_customers = client.customers.all_pages
all_jobs      = client.jobs.all_pages(order_id: "99")

Parameters:

  • first (Integer) (defaults to: 25)

    page size per request (default 25)

Returns:

  • (Array)


41
42
43
# File 'lib/printavo/resources/base.rb', line 41

def all_pages(first: 25, **kwargs)
  [].tap { |all| each_page(first: first, **kwargs) { |records| all.concat(records) } }
end

#each_page(first: 25, **kwargs) {|records| ... } ⇒ Object

Yields each page of records as an Array, following cursors automatically. Extra keyword arguments are forwarded to fetch_page (e.g. order_id: for Jobs).

Examples:

client.customers.each_page(first: 50) do |records|
  records.each { |c| puts c.full_name }
end

Parameters:

  • first (Integer) (defaults to: 25)

    page size (default 25)

Yield Parameters:

  • records (Array)

    one page of domain model objects



21
22
23
24
25
26
27
28
29
30
# File 'lib/printavo/resources/base.rb', line 21

def each_page(first: 25, **kwargs)
  after = nil
  loop do
    page = fetch_page(first: first, after: after, **kwargs)
    yield(page.records)
    break unless page.has_next_page

    after = page.end_cursor
  end
end