Class: Insights::API::Common::PaginatedResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/insights/api/common/paginated_response.rb

Direct Known Subclasses

PaginatedResponseV2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_query:, request:, limit: nil, offset: nil, sort_by: nil) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



7
8
9
10
11
12
13
14
# File 'lib/insights/api/common/paginated_response.rb', line 7

def initialize(base_query:, request:, limit: nil, offset: nil, sort_by: nil)
  @base_query = base_query
  @request    = request
  @limit      = (limit || 100).to_i.clamp(1, 1000)
  @offset     = (offset || 0).to_i.clamp(0, Float::INFINITY)
  @sort_by    = sort_by
  validate_sort_by
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/insights/api/common/paginated_response.rb', line 5

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



5
6
7
# File 'lib/insights/api/common/paginated_response.rb', line 5

def offset
  @offset
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



5
6
7
# File 'lib/insights/api/common/paginated_response.rb', line 5

def sort_by
  @sort_by
end

Instance Method Details

#recordsObject



16
17
18
19
20
21
22
23
# File 'lib/insights/api/common/paginated_response.rb', line 16

def records
  @records ||= begin
    res = @base_query.order(:id).limit(limit).offset(offset)
    order_options = sort_by_options(res.klass)
    res = res.reorder(order_options) if order_options.present?
    res
  end
end

#responseObject



25
26
27
28
29
30
31
# File 'lib/insights/api/common/paginated_response.rb', line 25

def response
  {
    "meta"  => ,
    "links" => links_hash,
    "data"  => records
  }
end