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
link_to_first()
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 48 def link_to_first link_with_new_offset(0) end
link_to_last()
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 52 def link_to_last link_with_new_offset(max_limit_multiplier.clamp(0, Float::INFINITY) * limit) end
link_to_next()
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 63 def link_to_next next_offset = limit + offset return if next_offset >= count link_with_new_offset(next_offset) end
link_to_prev()
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 56 def link_to_prev return if offset == 0 prev_offset = offset - limit link_with_new_offset(prev_offset.clamp(0, Float::INFINITY)) end
link_with_new_offset(offset)
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 70 def link_with_new_offset(offset) URI::Generic.build(:path => request_uri.path, :query => query_hash_merge("offset" => offset.to_s)).to_s end
links_hash()
click to toggle source
# File lib/insights/api/common/paginated_response.rb, line 39 def links_hash @links_hash ||= { "first" => link_to_first, "last" => link_to_last, "prev" => link_to_prev, "next" => link_to_next, }.compact 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