Class: JsonApiPaginator
- Inherits:
-
Object
- Object
- JsonApiPaginator
- Defined in:
- lib/json_api_paginator.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#result_set ⇒ Object
readonly
Returns the value of attribute result_set.
Instance Method Summary collapse
- #current_page ⇒ Object
- #first ⇒ Object
-
#initialize(result_set, data) ⇒ JsonApiPaginator
constructor
A new instance of JsonApiPaginator.
- #last ⇒ Object
- #next ⇒ Object
- #next_page ⇒ Object
- #per_page ⇒ Object
- #prev ⇒ Object
- #previous_page ⇒ Object
- #total_entries ⇒ Object (also: #total_count)
- #total_pages ⇒ Object
Constructor Details
#initialize(result_set, data) ⇒ JsonApiPaginator
Returns a new instance of JsonApiPaginator.
6 7 8 9 10 11 |
# File 'lib/json_api_paginator.rb', line 6 def initialize(result_set, data) @params = params_for_uri(result_set.uri) @result_set = result_set @links = data['links'] @meta = data['meta'] end |
Instance Attribute Details
#links ⇒ Object (readonly)
Returns the value of attribute links.
4 5 6 |
# File 'lib/json_api_paginator.rb', line 4 def links @links end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
4 5 6 |
# File 'lib/json_api_paginator.rb', line 4 def @meta end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/json_api_paginator.rb', line 4 def params @params end |
#result_set ⇒ Object (readonly)
Returns the value of attribute result_set.
4 5 6 |
# File 'lib/json_api_paginator.rb', line 4 def result_set @result_set end |
Instance Method Details
#current_page ⇒ Object
48 49 50 |
# File 'lib/json_api_paginator.rb', line 48 def current_page (params['page']['number'] || 1).to_i end |
#first ⇒ Object
21 22 23 |
# File 'lib/json_api_paginator.rb', line 21 def first links['first'] ? result_set.links.fetch_link('first') : nil end |
#last ⇒ Object
25 26 27 |
# File 'lib/json_api_paginator.rb', line 25 def last links['last'] ? result_set.links.fetch_link('last') : nil end |
#next ⇒ Object
13 14 15 |
# File 'lib/json_api_paginator.rb', line 13 def next links['next'] ? result_set.links.fetch_link('next') : nil end |
#next_page ⇒ Object
56 57 58 |
# File 'lib/json_api_paginator.rb', line 56 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#per_page ⇒ Object
44 45 46 |
# File 'lib/json_api_paginator.rb', line 44 def per_page params['page']['size'].to_i end |
#prev ⇒ Object
17 18 19 |
# File 'lib/json_api_paginator.rb', line 17 def prev links['prev'] ? result_set.links.fetch_link('prev') : nil end |
#previous_page ⇒ Object
52 53 54 |
# File 'lib/json_api_paginator.rb', line 52 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#total_entries ⇒ Object Also known as: total_count
39 40 41 |
# File 'lib/json_api_paginator.rb', line 39 def total_entries ['total_count'].to_i || per_page * total_pages end |
#total_pages ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/json_api_paginator.rb', line 29 def total_pages if links['last'] uri = result_set.links.link_url_for('last') last_params = params_for_uri(uri) last_params['page']['number'].to_i else current_page end end |