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.



5
6
7
8
9
# File 'lib/json_api_client/paginating/paginator.rb', line 5

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.



4
5
6
# File 'lib/json_api_client/paginating/paginator.rb', line 4

def links
  @links
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/json_api_client/paginating/paginator.rb', line 4

def params
  @params
end

#result_setObject (readonly)

Returns the value of attribute result_set.



4
5
6
# File 'lib/json_api_client/paginating/paginator.rb', line 4

def result_set
  @result_set
end

Instance Method Details

#current_pageObject



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

def current_page
  params.fetch("page", 1).to_i
end

#firstObject



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

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

#lastObject



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

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

#nextObject



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

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

#next_pageObject



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

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

#offsetObject



45
46
47
# File 'lib/json_api_client/paginating/paginator.rb', line 45

def offset
  per_page * (current_page - 1)
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/json_api_client/paginating/paginator.rb', line 59

def out_of_bounds?
  current_page > total_pages
end

#per_pageObject Also known as: limit_value



49
50
51
52
53
# File 'lib/json_api_client/paginating/paginator.rb', line 49

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

#prevObject



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

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

#previous_pageObject



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

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

#total_countObject



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

def total_count; total_entries; end

#total_entriesObject

this number may be off



40
41
42
# File 'lib/json_api_client/paginating/paginator.rb', line 40

def total_entries
  per_page * total_pages
end

#total_pagesObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json_api_client/paginating/paginator.rb', line 27

def total_pages
  if links["last"]
    uri = result_set.links.link_url_for("last")
    last_params = params_for_uri(uri)
    last_params.fetch("page") do
      current_page
    end.to_i
  else
    current_page
  end
end