Class: Insights::API::Common::PaginatedResponse
- Inherits:
-
Object
- Object
- Insights::API::Common::PaginatedResponse
- Defined in:
- lib/insights/api/common/paginated_response.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#sort_by ⇒ Object
readonly
Returns the value of attribute sort_by.
Instance Method Summary collapse
-
#initialize(base_query:, request:, limit: nil, offset: nil, sort_by: nil) ⇒ PaginatedResponse
constructor
A new instance of PaginatedResponse.
- #records ⇒ Object
- #response ⇒ Object
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 |
# 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 end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
5 6 7 |
# File 'lib/insights/api/common/paginated_response.rb', line 5 def limit @limit end |
#offset ⇒ Object (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_by ⇒ Object (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
#records ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/insights/api/common/paginated_response.rb', line 15 def records @records ||= begin res = @base_query.order(:id).limit(limit).offset(offset) = (res.klass) res = res.reorder() if .present? res end end |
#response ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/insights/api/common/paginated_response.rb', line 24 def response { "meta" => , "links" => links_hash, "data" => records } end |