Class: DaisybillApi::Ext::PageableCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daisybill_api/ext/pageable_collection.rb

Overview

Provides a paginated collection of objects that can iterated upon:

bp = DaisybillApi::Models::BillingProvider.find(14) # => <DaisyBillApi::Models::BillingProvider>
patients = bp.patients # => #<DaisybillApi::Ext::PageableCollection>

until patients.next_page.nil? do
  puts patients.map(&:id)
  patients = patients.next_page
end
# => [1, 2, 3, 4, 5]

Instance Method Summary collapse

Constructor Details

#initialize(entries, options = {}) ⇒ PageableCollection

Returns a new instance of PageableCollection.



17
18
19
20
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 17

def initialize(entries, options = {})
  @entries = entries
  @options = options
end

Instance Method Details

#current_page_numberInteger

Returns the current page number

@providers.current_page_number # => 1

Returns:

  • (Integer)


79
80
81
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 79

def current_page_number
  retrieve_header_value(:x_page)
end

#each(&block) ⇒ Object



26
27
28
29
30
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 26

def each(&block)
  entries.each do |entry|
    block.call(entry)
  end
end

#first_pagePageableCollection

Returns the first page of the collection

@patients = DaisybillApi::Models::Patient.all(billing_provider_id: 25) # => #<DaisybillApi::Ext::PageableCollection>

@patients.first_page # => #<DaisybillApi::Ext::PageableCollection>

Returns:



39
40
41
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 39

def first_page
  resource_class.all(default_options)
end

#inspectObject



22
23
24
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 22

def inspect
  "#<DaisybillApi::Ext::PageableCollection entries: #{entries.to_s[1..80]}..., previous_page: #{previous_page_number}, current_page: #{current_page_number}, next_page: #{next_page_number} >"
end

#last_pagePageableCollection

Returns the last page in the collection

@patients.last_page # => #<DaisybillApi::Ext::PageableCollection>

Returns:



48
49
50
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 48

def last_page
  resource_class.all(default_options.merge(page: total_page_count))
end

#next_pagePageableCollection?

Returns the next page in the collection

@providers.next_page # => #<DaisybillApi::Ext::PageableCollection>

Returns:



57
58
59
60
61
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 57

def next_page
  if next_page_number
    resource_class.all(default_options.merge(page: next_page_number))
  end
end

#previous_pagePageableCollection?

Returns the previous page in the collection

@providers.previous_page # => #<DaisybillApi::Ext::PageableCollection>

Returns:



68
69
70
71
72
# File 'lib/daisybill_api/ext/pageable_collection.rb', line 68

def previous_page
  if previous_page_number
    resource_class.all(default_options.merge(page: previous_page_number))
  end
end