lib/apia/request_environment.rb
# Set appropriate pagination for the given set based on the configuration
# specified for the endpoint
#
# @param set [#limit, #count, #page, #per, #to_a, #total_pages, #current_page, #without_count]
# @param large_set [Boolean] whether or not this is expected to be a large set
# @return [void]
Set appropriate pagination for the given set based on the configuration
specified for the endpoint
Set appropriate pagination for the given set based on the configuration specified for the endpoint
def paginate(set, potentially_large_set: false)
      paginated_field = @request.endpoint.definition.paginated_field
      if paginated_field.nil?
        raise Apia::RuntimeError, 'Could not paginate response because no pagination has been configured for the endpoint'
      end
      paginated = set.page(@request.arguments[:page] || 1)
      paginated = paginated.per(@request.arguments[:per_page] || 30)
      large_set = false
      if potentially_large_set
        total_count = set.limit(1001).count
        if total_count > 1000
          large_set = true
          paginated = paginated.without_count
        end
      end
      @response.add_field paginated_field, paginated.to_a
      pagination_info = {}
      pagination_info[:current_page] = paginated.current_page
      pagination_info[:per_page] = paginated.limit_value
      pagination_info[:large_set] = large_set
      unless large_set
        pagination_info[:total] = paginated.total_count
        pagination_info[:total_pages] = paginated.total_pages
      end
      @response.add_field :pagination, pagination_info
    end
def paginate ( set , potentially_large_set : false ) paginated_field = @request . endpoint . definition . paginated_field if paginated_field . nil? raise Apia :: RuntimeError , 'Could not paginate response because no pagination has been configured for the endpoint' end paginated = set . page ( @request . arguments [ :page ] || 1 ) paginated = paginated . per ( @request . arguments [ :per_page ] || 30 ) large_set = false if potentially_large_set total_count = set . limit ( 1001 ) . count if total_count > 1000 large_set = true paginated = paginated . without_count end end @response . add_field paginated_field , paginated . to_a pagination_info = { } pagination_info [ :current_page ] = paginated . current_page pagination_info [ :per_page ] = paginated . limit_value pagination_info [ :large_set ] = large_set unless large_set pagination_info [ :total ] = paginated . total_count pagination_info [ :total_pages ] = paginated . total_pages end @response . add_field :pagination , pagination_info end
Set appropriate pagination for the given set based on the configuration
specified for the endpoint
Set appropriate pagination for the given set based on the configuration specified for the endpoint
None
void
set
<not_specific>
potentially_large_set
<not_specific>
<not_specific>
None
large_set
<not_specific>
whether or not this is expected to be a large set
whether or not this is expected to be a large set