Class: JsonApiClient::Paginating::NestedParamPaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/paginating/nested_param_paginator.rb

Overview

An alternate, more consistent Paginator that always wraps pagination query string params in a top-level wrapper_name, e.g. page=2, page=10.

Constant Summary collapse

DEFAULT_WRAPPER_NAME =
"page".freeze
DEFAULT_PAGE_PARAM =
"page".freeze
DEFAULT_PER_PAGE_PARAM =
"per_page".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_set, data) ⇒ NestedParamPaginator

Returns a new instance of NestedParamPaginator.


57
58
59
60
61
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 57

def initialize(result_set, data)
  @params = params_for_uri(result_set.uri)
  @result_set = result_set
  @links = data["links"]
end

Instance Attribute Details

Returns the value of attribute links


55
56
57
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55

def links
  @links
end

#paramsObject (readonly)

Returns the value of attribute params


55
56
57
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55

def params
  @params
end

#result_setObject (readonly)

Returns the value of attribute result_set


55
56
57
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55

def result_set
  @result_set
end

Class Method Details

.page_paramObject


25
26
27
28
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 25

def page_param
  @_page_param ||= DEFAULT_PAGE_PARAM
  "#{wrapper_name}[#{@_page_param}]"
end

.page_param=(param = DEFAULT_PAGE_PARAM) ⇒ Object

Raises:

  • (ArgumentError)

30
31
32
33
34
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 30

def page_param=(param = DEFAULT_PAGE_PARAM)
  raise ArgumentError, "don't wrap page_param" unless valid_param?(param)

  @_page_param = param.to_s
end

.per_page_paramObject


36
37
38
39
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 36

def per_page_param
  @_per_page_param ||= DEFAULT_PER_PAGE_PARAM
  "#{wrapper_name}[#{@_per_page_param}]"
end

.per_page_param=(param = DEFAULT_PER_PAGE_PARAM) ⇒ Object

Raises:

  • (ArgumentError)

41
42
43
44
45
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 41

def per_page_param=(param = DEFAULT_PER_PAGE_PARAM)
  raise ArgumentError, "don't wrap per_page_param" unless valid_param?(param)

  @_per_page_param = param
end

.wrapper_nameObject


15
16
17
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 15

def wrapper_name
  @_wrapper_name ||= DEFAULT_WRAPPER_NAME
end

.wrapper_name=(param = DEFAULT_WRAPPER_NAME) ⇒ Object

Raises:

  • (ArgumentError)

19
20
21
22
23
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 19

def wrapper_name=(param = DEFAULT_WRAPPER_NAME)
  raise ArgumentError, "don't wrap wrapper_name" unless valid_param?(param)

  @_wrapper_name = param.to_s
end

Instance Method Details

#current_pageObject


105
106
107
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 105

def current_page
  params.fetch(page_param, 1).to_i
end

#firstObject


71
72
73
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 71

def first
  result_set.links.fetch_link("first")
end

#lastObject


75
76
77
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 75

def last
  result_set.links.fetch_link("last")
end

#nextObject


63
64
65
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 63

def next
  result_set.links.fetch_link("next")
end

#next_pageObject


117
118
119
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 117

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject


95
96
97
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 95

def offset
  per_page * (current_page - 1)
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)

109
110
111
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 109

def out_of_bounds?
  current_page > total_pages
end

#page_paramObject


121
122
123
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 121

def page_param
  self.class.page_param
end

#per_pageObject Also known as: limit_value


99
100
101
102
103
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 99

def per_page
  params.fetch(per_page_param) do
    result_set.length
  end.to_i
end

#per_page_paramObject


125
126
127
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 125

def per_page_param
  self.class.per_page_param
end

#prevObject


67
68
69
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 67

def prev
  result_set.links.fetch_link("prev")
end

#previous_pageObject


113
114
115
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 113

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#total_countObject


93
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 93

def total_count; total_entries; end

#total_entriesObject

this is an estimate, not necessarily an exact count


90
91
92
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 90

def total_entries
  per_page * total_pages
end

#total_pagesObject


79
80
81
82
83
84
85
86
87
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 79

def total_pages
  if links["last"]
    uri = result_set.links.link_url_for("last")
    last_params = params_for_uri(uri)
    last_params.fetch(page_param, &method(:current_page)).to_i
  else
    current_page
  end
end