Class: EInvoiceAPI::Internal::DocumentsNumberPage

Inherits:
Object
  • Object
show all
Includes:
Type::BasePage
Defined in:
lib/e_invoice_api/internal/documents_number_page.rb

Overview

Examples:

if documents_number_page.has_next?
  documents_number_page = documents_number_page.next_page
end
documents_number_page.auto_paging_each do |inbox|
  puts(inbox)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

#initialize(client:, req:, headers:, page_data:) ⇒ DocumentsNumberPage

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DocumentsNumberPage.

Parameters:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 71

def initialize(client:, req:, headers:, page_data:)
  super

  case page_data
  in {items: Array => items}
    @items = items.map { EInvoiceAPI::Internal::Type::Converter.coerce(@model, _1) }
  else
  end
  @page = page_data[:page]
  @page_size = page_data[:page_size]
  @total = page_data[:total]
end

Instance Attribute Details

#itemsArray<generic<Elem>>?

Returns:

  • (Array<generic<Elem>>, nil)


20
21
22
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 20

def items
  @items
end

#pageInteger

Returns:

  • (Integer)


23
24
25
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 23

def page
  @page
end

#page_sizeInteger

Returns:

  • (Integer)


26
27
28
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 26

def page_size
  @page_size
end

#totalInteger

Returns:

  • (Integer)


29
30
31
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 29

def total
  @total
end

Instance Method Details

#auto_paging_each(&blk) {|| ... } ⇒ Object

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 51

def auto_paging_each(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  page = self
  loop do
    page.items&.each(&blk)

    break unless page.next_page?
    page = page.next_page
  end
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


87
88
89
90
91
92
93
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 87

def inspect
  # rubocop:disable Layout/LineLength
  model = EInvoiceAPI::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} page=#{page.inspect} page_size=#{page_size.inspect} total=#{total.inspect}>"
  # rubocop:enable Layout/LineLength
end

#next_pageself

Returns:

  • (self)

Raises:

  • (EInvoiceAPI::HTTP::Error)


38
39
40
41
42
43
44
45
46
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 38

def next_page
  unless next_page?
    message = "No more pages available. Please check #next_page? before calling ##{__method__}"
    raise RuntimeError.new(message)
  end

  req = EInvoiceAPI::Internal::Util.deep_merge(@req, {query: {page: (page || 1).to_i.succ}})
  @client.request(req)
end

#next_page?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/e_invoice_api/internal/documents_number_page.rb', line 32

def next_page?
  !items.to_a.empty? && (page.nil? || total.nil? || page < total)
end