Class: RedboothRuby::Request::Collection
- Inherits:
-
Object
- Object
- RedboothRuby::Request::Collection
- Includes:
- Helpers
- Defined in:
- lib/redbooth-ruby/request/collection.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#all ⇒ Array(resource)
Returns an array of resuce objects built from each one of the response items.
-
#count ⇒ Integer
Return total elements.
-
#current_page ⇒ Integer
Returns elements per page.
-
#initialize(attributes = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#next_page ⇒ Redbooth::Request::Collection
Performs the request for the next page if exists.
-
#per_page ⇒ Integer
Returns elements per page.
-
#prev_page ⇒ Redbooth::Request::Collection
Performs the request for the next page if exists.
-
#total_pages ⇒ Integer
Returns total pages.
Methods included from Helpers
Constructor Details
#initialize(attributes = {}) ⇒ Collection
Returns a new instance of Collection.
7 8 9 10 11 12 13 |
# File 'lib/redbooth-ruby/request/collection.rb', line 7 def initialize(attributes={}) @response = attributes[:response] @params = attributes[:params] @method = attributes[:method] @resource = attributes[:resource] @session = attributes[:session] end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/redbooth-ruby/request/collection.rb', line 5 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/redbooth-ruby/request/collection.rb', line 5 def params @params end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/redbooth-ruby/request/collection.rb', line 5 def resource @resource end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
5 6 7 |
# File 'lib/redbooth-ruby/request/collection.rb', line 5 def response @response end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
5 6 7 |
# File 'lib/redbooth-ruby/request/collection.rb', line 5 def session @session end |
Instance Method Details
#all ⇒ Array(resource)
Returns an array of resuce objects built from each one of the response items
18 19 20 |
# File 'lib/redbooth-ruby/request/collection.rb', line 18 def all results_from(response) end |
#count ⇒ Integer
Return total elements
49 50 51 52 53 |
# File 'lib/redbooth-ruby/request/collection.rb', line 49 def count return all.size unless paginated? return all.size if total_pages.to_i <= 1 total_pages * per_page end |
#current_page ⇒ Integer
Returns elements per page
41 42 43 44 |
# File 'lib/redbooth-ruby/request/collection.rb', line 41 def current_page return unless response.headers['PaginationCurrentPage'] response.headers['PaginationCurrentPage'].to_i end |
#next_page ⇒ Redbooth::Request::Collection
Performs the request for the next page if exists
58 59 60 61 62 |
# File 'lib/redbooth-ruby/request/collection.rb', line 58 def next_page return nil unless paginated? return nil unless (total_pages - current_page) > 0 request_with(page: current_page + 1) end |
#per_page ⇒ Integer
Returns elements per page
33 34 35 36 |
# File 'lib/redbooth-ruby/request/collection.rb', line 33 def per_page return unless response.headers['PaginationPerPage'] response.headers['PaginationPerPage'].to_i end |
#prev_page ⇒ Redbooth::Request::Collection
Performs the request for the next page if exists
67 68 69 70 71 |
# File 'lib/redbooth-ruby/request/collection.rb', line 67 def prev_page return nil unless paginated? return nil unless current_page > 1 request_with(page: current_page - 1) end |
#total_pages ⇒ Integer
Returns total pages
25 26 27 28 |
# File 'lib/redbooth-ruby/request/collection.rb', line 25 def total_pages return unless response.headers['PaginationTotalPages'] response.headers['PaginationTotalPages'].to_i end |