class Insights::API::Common::PaginatedResponse

Attributes

limit[R]
offset[R]
sort_by[R]

Public Class Methods

new(base_query:, request:, limit: nil, offset: nil, sort_by: nil) click to toggle source
# 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

Public Instance Methods

records() click to toggle source
# 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
response() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 25
def response
  {
    "meta"  => metadata_hash,
    "links" => links_hash,
    "data"  => records
  }
end

Private Instance Methods

count() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 90
def count
  @count ||= @base_query.count
end
max_limit_multiplier() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 86
def max_limit_multiplier
  @max_limit_multiplier ||= ((count - 1) / limit)
end
metadata_hash() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 35
def metadata_hash
  @metadata_hash ||= {"count" => count, "limit" => limit, "offset" => offset}
end
parsed_query() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 78
def parsed_query
  @parsed_query ||= Rack::Utils.parse_query(request_uri.query)
end
query_hash_merge(new_hash) click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 74
def query_hash_merge(new_hash)
  parsed_query.merge(new_hash).to_query
end
request_uri() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 82
def request_uri
  @request_uri ||= URI.parse(@request.original_url)
end
sort_by_options(model) click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 94
def sort_by_options(model)
  @sort_by_options ||= begin
    Array(sort_by).collect do |selection|
      sort_attr, sort_order = selection.split(':')
      sort_order ||= "asc"
      arel = model.arel_attribute(sort_attr)
      (sort_order == "desc") ? arel.desc : arel.asc
    end
  end
end
validate_sort_by() click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 105
def validate_sort_by
  return unless sort_by.present?
  raise ArgumentError, "Invalid sort_by parameter specified \"#{sort_by}\"" unless sort_by.kind_of?(String) || sort_by.kind_of?(Array)
  Array(sort_by).each { |key| validate_sort_by_directive(key) }
end
validate_sort_by_directive(key) click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 111
def validate_sort_by_directive(key)
  raise ArgumentError, "Invalid sort_by directive specified \"#{key}\"" unless key.match?(/^[a-z\\-_]+(:asc|:desc)?$/)
end