Class: JsonApiClient::Paginating::Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_set, data) ⇒ Paginator

Returns a new instance of Paginator.



12
13
14
15
16
# File 'lib/json_api_client/paginating/paginator.rb', line 12

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.



10
11
12
# File 'lib/json_api_client/paginating/paginator.rb', line 10

def links
  @links
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/json_api_client/paginating/paginator.rb', line 10

def params
  @params
end

#result_setObject (readonly)

Returns the value of attribute result_set.



10
11
12
# File 'lib/json_api_client/paginating/paginator.rb', line 10

def result_set
  @result_set
end

Instance Method Details

#current_pageObject



62
63
64
# File 'lib/json_api_client/paginating/paginator.rb', line 62

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

#firstObject



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

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

#lastObject



30
31
32
# File 'lib/json_api_client/paginating/paginator.rb', line 30

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

#nextObject



18
19
20
# File 'lib/json_api_client/paginating/paginator.rb', line 18

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

#next_pageObject



74
75
76
# File 'lib/json_api_client/paginating/paginator.rb', line 74

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

#offsetObject



52
53
54
# File 'lib/json_api_client/paginating/paginator.rb', line 52

def offset
  per_page * (current_page - 1)
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/json_api_client/paginating/paginator.rb', line 66

def out_of_bounds?
  current_page > total_pages
end

#per_pageObject Also known as: limit_value



56
57
58
59
60
# File 'lib/json_api_client/paginating/paginator.rb', line 56

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

#prevObject



22
23
24
# File 'lib/json_api_client/paginating/paginator.rb', line 22

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

#previous_pageObject



70
71
72
# File 'lib/json_api_client/paginating/paginator.rb', line 70

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

#total_countObject



50
# File 'lib/json_api_client/paginating/paginator.rb', line 50

def total_count; total_entries; end

#total_entriesObject

this number may be off



47
48
49
# File 'lib/json_api_client/paginating/paginator.rb', line 47

def total_entries
  per_page * total_pages
end

#total_pagesObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/json_api_client/paginating/paginator.rb', line 34

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) do
      current_page
    end.to_i
  else
    current_page
  end
end