class Enceladus::ApiPaginatedCollection

Provides the implementation to handle collection of paginated resources. The collection of resources are cached locally, after going to next pages the previous pages do not make any request to obtain the resources again.

Constants

RESOURCE_CLASS

Attributes

params[RW]
path[RW]
results_per_page[RW]
total_pages[RW]
total_results[RW]

Public Class Methods

new(path, params={}) click to toggle source

The argument path refers to the TMDb api path that provides paginated resources. the argument params can be used when you need to send arguments to requests when fetching new pages. Example:

Enceladus::MovieCollection.new("movie/upcoming", { blah: true })
# File lib/enceladus/models/api_paginated_collection.rb, line 13
def initialize(path, params={})
  self.path = path
  self.params = params
  self.params[:page] = 1
  self.results_per_page = []
  get_results_per_page
end

Public Instance Methods

all() click to toggle source

Returns a collection of resources for the current page

# File lib/enceladus/models/api_paginated_collection.rb, line 22
def all
  get_results_per_page
end
current_page() click to toggle source

Returns current page number.

# File lib/enceladus/models/api_paginated_collection.rb, line 40
def current_page
  self.params[:page]
end
current_page=(page) click to toggle source

Request, fetch, cache and return the collection of resources for a given page number.

# File lib/enceladus/models/api_paginated_collection.rb, line 45
def current_page=(page)
  self.params[:page] = page
  get_results_per_page
end
first() click to toggle source

Returns the first resource for of the current page.

# File lib/enceladus/models/api_paginated_collection.rb, line 51
def first
  get_results_per_page.first
end
last() click to toggle source

Returns the last resource for of the current page.

# File lib/enceladus/models/api_paginated_collection.rb, line 56
def last
  get_results_per_page.last
end
next_page() click to toggle source

Request, fetch, cache and return the collection of resources for the next page.

# File lib/enceladus/models/api_paginated_collection.rb, line 27
def next_page
  self.params[:page] += 1
  get_results_per_page
end
previous_page() click to toggle source

Request, fetch, cache and return the collection of resources for the previous page.

# File lib/enceladus/models/api_paginated_collection.rb, line 33
def previous_page
  raise ArgumentError.new("current_page must be greater than 0") if self.params[:page] == 1
  self.params[:page] -= 1
  get_results_per_page
end

Private Instance Methods

fetch_colletion() click to toggle source
# File lib/enceladus/models/api_paginated_collection.rb, line 63
def fetch_colletion
  raise NotImplementedError.new("RESOURCE_CLASS must be defined") if self.class::RESOURCE_CLASS.nil?

  response = Enceladus::Requester.get(path, params)
  self.total_pages = response.total_pages
  self.total_results = response.total_results
  self.class::RESOURCE_CLASS.build_collection(response.results)
end
get_results_per_page() click to toggle source
# File lib/enceladus/models/api_paginated_collection.rb, line 72
def get_results_per_page
  self.results_per_page[current_page - 1] ||= fetch_colletion
end