module ApiPaginationHeaders
Constants
- VERSION
Public Class Methods
config()
click to toggle source
Global settings for ApiPaginationHeaders
# File lib/api_pagination_headers/config.rb, line 13 def self.config @config ||= ApiPaginationHeaders::Configuration.new end
configure() { |config ||= configuration| ... }
click to toggle source
Configures global settings for ApiPaginationHeaders
ApiPaginationHeaders.configure do |config| config.total_count_header = 'X-Total-Count' end
# File lib/api_pagination_headers/config.rb, line 8 def self.configure(&block) yield @config ||= ApiPaginationHeaders::Configuration.new end
Protected Instance Methods
set_pagination_headers(name)
click to toggle source
# File lib/api_pagination_headers.rb, line 9 def set_pagination_headers(name) scope = instance_variable_get("@#{name}") pages = set_page_numbers(scope) links = create_links(pages, scope) headers['Link'] = links.join(', ') unless links.empty? headers[ApiPaginationHeaders.config.total_count_header] = defined?(Kaminari) ? "#{scope.total_count}" : "#{scope.total_entries}" end
Private Instance Methods
create_links(pages, scope)
click to toggle source
# File lib/api_pagination_headers.rb, line 20 def create_links(pages, scope) url_without_params = request.url.split('?').first if ApiPaginationHeaders.config.force_https url_without_params.sub! 'http://', 'https://' end if params[:per_page] per_page = params[:per_page].to_i else per_page = defined?(Kaminari) ? scope.default_per_page : scope.per_page end links = [] pages.each do |key, value| new_params = request.query_parameters.merge({ page: value, per_page: per_page }) links << "<#{url_without_params}?#{new_params.to_param}>; rel=\"#{key}\"" end links end
set_page_numbers(scope)
click to toggle source
# File lib/api_pagination_headers.rb, line 40 def set_page_numbers(scope) pages = {} pages[:first] = 1 if scope.total_pages > 1 && scope.current_page > 1 pages[:prev] = scope.current_page - 1 if scope.current_page > 1 pages[:next] = scope.current_page + 1 if scope.current_page < scope.total_pages pages[:last] = scope.total_pages if scope.total_pages > 1 && scope.current_page < scope.total_pages pages end