class DaisybillApi::Ext::PageableCollection

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]

Attributes

entries[R]
options[R]

Public Class Methods

new(entries, options = {}) click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 17
def initialize(entries, options = {})
  @entries = entries
  @options = options
end

Public Instance Methods

current_page_number() click to toggle source

Returns the current page number

@providers.current_page_number # => 1

@return [Integer]

# File lib/daisybill_api/ext/pageable_collection.rb, line 79
def current_page_number
  retrieve_header_value(:x_page)
end
each(&block) click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 26
def each(&block)
  entries.each do |entry|
    block.call(entry)
  end
end
first_page() click to toggle source

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>

@return [PageableCollection]

# File lib/daisybill_api/ext/pageable_collection.rb, line 39
def first_page
  resource_class.all(default_options)
end
inspect() click to toggle source
# 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_page() click to toggle source

Returns the last page in the collection

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

@return [PageableCollection]

# 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_page() click to toggle source

Returns the next page in the collection

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

@return [PageableCollection, nil]

# 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_page() click to toggle source

Returns the previous page in the collection

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

@return [PageableCollection, nil]

# 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

Private Instance Methods

default_options() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 87
def default_options
  params.merge(per_page: results_per_page)
end
headers() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 91
def headers
  @headers ||= options[:headers] || {}
end
next_page_number() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 107
def next_page_number
  retrieve_header_value(:x_next_page)
end
params() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 95
def params
  @params ||= options[:params] || {}
end
previous_page_number() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 103
def previous_page_number
  retrieve_header_value(:x_prev_page)
end
resource_class() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 99
def resource_class
  @resource_class ||= options[:resource_class]
end
results_per_page() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 111
def results_per_page
  headers[:x_per_page]
end
retrieve_header_value(key) click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 119
def retrieve_header_value(key)
  headers[key].to_s.empty? ? nil : headers[key].to_i
end
total_page_count() click to toggle source
# File lib/daisybill_api/ext/pageable_collection.rb, line 115
def total_page_count
  headers[:x_total_pages]
end