Class: Enceladus::ApiPaginatedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/enceladus/models/api_paginated_collection.rb

Overview

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.

Direct Known Subclasses

MovieCollection, ProductionCompanyCollection

Constant Summary collapse

RESOURCE_CLASS =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params = {}) ⇒ ApiPaginatedCollection

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 })


13
14
15
16
17
18
19
# 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

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7

def params
  @params
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7

def path
  @path
end

#results_per_pageObject

Returns the value of attribute results_per_page.



7
8
9
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7

def results_per_page
  @results_per_page
end

#total_pagesObject

Returns the value of attribute total_pages.



7
8
9
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7

def total_pages
  @total_pages
end

#total_resultsObject

Returns the value of attribute total_results.



7
8
9
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7

def total_results
  @total_results
end

Instance Method Details

#allObject

Returns a collection of resources for the current page



22
23
24
# File 'lib/enceladus/models/api_paginated_collection.rb', line 22

def all
  get_results_per_page
end

#current_pageObject

Returns current page number.



40
41
42
# File 'lib/enceladus/models/api_paginated_collection.rb', line 40

def current_page
  self.params[:page]
end

#current_page=(page) ⇒ Object

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



45
46
47
48
# File 'lib/enceladus/models/api_paginated_collection.rb', line 45

def current_page=(page)
  self.params[:page] = page
  get_results_per_page
end

#firstObject

Returns the first resource for of the current page.



51
52
53
# File 'lib/enceladus/models/api_paginated_collection.rb', line 51

def first
  get_results_per_page.first
end

#lastObject

Returns the last resource for of the current page.



56
57
58
# File 'lib/enceladus/models/api_paginated_collection.rb', line 56

def last
  get_results_per_page.last
end

#next_pageObject

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



27
28
29
30
# File 'lib/enceladus/models/api_paginated_collection.rb', line 27

def next_page
  self.params[:page] += 1
  get_results_per_page
end

#previous_pageObject

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

Raises:

  • (ArgumentError)


33
34
35
36
37
# 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