Class: PetstoreApiClient::PaginatedCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/petstore_api_client/paginated_collection.rb

Overview

Wrapper for paginated API responses Provides metadata about pagination along with the data

This follows the Decorator pattern - it wraps an array with additional pagination information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, page: 1, per_page: 25, total_count: nil) ⇒ PaginatedCollection

Initialize a paginated collection

Parameters:

  • data (Array)

    The array of items for this page

  • page (Integer) (defaults to: 1)

    Current page number (1-indexed)

  • per_page (Integer) (defaults to: 25)

    Number of items per page

  • total_count (Integer, nil) (defaults to: nil)

    Total number of items across all pages (if known)



20
21
22
23
24
25
# File 'lib/petstore_api_client/paginated_collection.rb', line 20

def initialize(data:, page: 1, per_page: 25, total_count: nil)
  @data = Array(data)
  @page = page.to_i
  @per_page = per_page.to_i
  @total_count = total_count&.to_i
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/petstore_api_client/paginated_collection.rb', line 12

def data
  @data
end

#pageObject (readonly)

Returns the value of attribute page.



12
13
14
# File 'lib/petstore_api_client/paginated_collection.rb', line 12

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



12
13
14
# File 'lib/petstore_api_client/paginated_collection.rb', line 12

def per_page
  @per_page
end

#total_countObject (readonly)

Returns the value of attribute total_count.



12
13
14
# File 'lib/petstore_api_client/paginated_collection.rb', line 12

def total_count
  @total_count
end

Instance Method Details

#any?Boolean

Check if collection has any items

Returns:

  • (Boolean)


102
103
104
# File 'lib/petstore_api_client/paginated_collection.rb', line 102

def any?
  !empty?
end

#countObject Also known as: size, length

Number of items in current page



33
34
35
# File 'lib/petstore_api_client/paginated_collection.rb', line 33

def count
  data.count
end

#eachObject

Delegate enumerable methods to data array



28
29
30
# File 'lib/petstore_api_client/paginated_collection.rb', line 28

def each(&)
  data.each(&)
end

#empty?Boolean

Check if collection is empty

Returns:

  • (Boolean)


97
98
99
# File 'lib/petstore_api_client/paginated_collection.rb', line 97

def empty?
  data.empty?
end

#first_page?Boolean

Check if this is the first page

Returns:

  • (Boolean)


77
78
79
# File 'lib/petstore_api_client/paginated_collection.rb', line 77

def first_page?
  page == 1
end

#inspectObject

Inspect override for better debugging



128
129
130
131
# File 'lib/petstore_api_client/paginated_collection.rb', line 128

def inspect
  "#<PaginatedCollection page=#{page}/#{total_pages || "?"} " \
    "per_page=#{per_page} count=#{count} total=#{total_count || "?"}>"
end

#last_page?Boolean

Check if this is the last page Returns nil if total_count is unknown rubocop:disable Style/ReturnNilInPredicateMethodDefinition

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/petstore_api_client/paginated_collection.rb', line 84

def last_page?
  return nil if total_count.nil?

  page >= total_pages
end

#next_pageObject

Get next page number (nil if on last page or unknown)



57
58
59
# File 'lib/petstore_api_client/paginated_collection.rb', line 57

def next_page
  next_page? ? page + 1 : nil
end

#next_page?Boolean

Check if there are more pages available Returns true if we have total_count and current page isn’t the last Returns nil if total_count is unknown (can’t determine) rubocop:disable Style/ReturnNilInPredicateMethodDefinition

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/petstore_api_client/paginated_collection.rb', line 43

def next_page?
  return nil if total_count.nil?

  page < total_pages
end

#offsetObject

Get offset for current page (0-indexed)



92
93
94
# File 'lib/petstore_api_client/paginated_collection.rb', line 92

def offset
  (page - 1) * per_page
end

#pagination_infoObject

Summary information about pagination



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/petstore_api_client/paginated_collection.rb', line 113

def pagination_info
  {
    page: page,
    per_page: per_page,
    count: count,
    total_count: total_count,
    total_pages: total_pages,
    next_page: next_page,
    prev_page: prev_page,
    first_page: first_page?,
    last_page: last_page?
  }
end

#prev_pageObject Also known as: previous_page

Get previous page number (nil if on first page)



62
63
64
# File 'lib/petstore_api_client/paginated_collection.rb', line 62

def prev_page
  prev_page? ? page - 1 : nil
end

#prev_page?Boolean Also known as: previous_page?

Check if there’s a previous page

Returns:

  • (Boolean)


51
52
53
# File 'lib/petstore_api_client/paginated_collection.rb', line 51

def prev_page?
  page > 1
end

#to_aObject Also known as: to_ary

Convert to array (returns the data)



107
108
109
# File 'lib/petstore_api_client/paginated_collection.rb', line 107

def to_a
  data
end

#total_pagesObject

Calculate total number of pages Returns nil if total_count is unknown



69
70
71
72
73
74
# File 'lib/petstore_api_client/paginated_collection.rb', line 69

def total_pages
  return nil if total_count.nil?
  return 1 if total_count.zero?

  (total_count.to_f / per_page).ceil
end