Class: Enceladus::ApiPaginatedCollection
- Inherits:
-
Object
- Object
- Enceladus::ApiPaginatedCollection
- 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
Constant Summary collapse
- RESOURCE_CLASS =
nil
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#results_per_page ⇒ Object
readonly
Returns the value of attribute results_per_page.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
-
#total_results ⇒ Object
readonly
Returns the value of attribute total_results.
Instance Method Summary collapse
-
#all ⇒ Object
Returns a collection of resources for the current page.
-
#current_page ⇒ Object
Returns current page number.
-
#current_page=(page) ⇒ Object
Request, fetch, cache and return the collection of resources for a given page number.
-
#first ⇒ Object
Returns the first resource for of the current page.
-
#initialize(path, params = {}) ⇒ ApiPaginatedCollection
constructor
The argument path refers to the TMDb api path that provides paginated resources.
-
#last ⇒ Object
Returns the last resource for of the current page.
-
#next_page ⇒ Object
Request, fetch, cache and return the collection of resources for the next page.
-
#previous_page ⇒ Object
Request, fetch, cache and return the collection of resources for the previous page.
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
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/enceladus/models/api_paginated_collection.rb', line 7 def params @params end |
#path ⇒ Object
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_page ⇒ Object
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_pages ⇒ Object
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_results ⇒ Object
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
#all ⇒ Object
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_page ⇒ Object
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 |
#first ⇒ Object
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 |
#last ⇒ Object
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_page ⇒ Object
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_page ⇒ Object
Request, fetch, cache and return the collection of resources for the previous page.
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 |