Class: WCC::API::JSON::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/api/json/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, url_for:) ⇒ Pagination

Returns a new instance of Pagination.


7
8
9
10
# File 'lib/wcc/api/json/pagination.rb', line 7

def initialize(query, url_for:)
  @query = query
  @url_for = url_for
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query


5
6
7
# File 'lib/wcc/api/json/pagination.rb', line 5

def query
  @query
end

#url_forObject (readonly)

Returns the value of attribute url_for


5
6
7
# File 'lib/wcc/api/json/pagination.rb', line 5

def url_for
  @url_for
end

Instance Method Details

#to_builderObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wcc/api/json/pagination.rb', line 12

def to_builder
  Jbuilder.new do |json|
    json.total query.total
    if query.paging
      json.size query.size
      json.limit query.limit
      json.offset query.offset
    end
    json.order_by query.order_by if query.respond_to?(:order_by)
    json.sort query.sort if query.respond_to?(:sort)
    json.filter query.filter
    json._links do
      json.self url_for.call(base_url_params)
      if has_previous_page?
        json.previous url_for.call(base_url_params.merge(offset: query.offset - query.limit))
      end
      if has_next_page?
        json.next url_for.call(base_url_params.merge(offset: query.offset + query.limit))
      end
    end
  end
end