Module: SlackRubyBotServer::Api::Helpers::CursorHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb

Instance Method Summary collapse

Instance Method Details

#paginate_and_sort_by_cursor(coll, options = {}) ⇒ Object



46
47
48
# File 'lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb', line 46

def paginate_and_sort_by_cursor(coll, options = {})
  Hashie::Mash.new(paginate_by_cursor(sort(coll, options), options))
end

#paginate_by_cursor(coll, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb', line 12

def paginate_by_cursor(coll, _options)
  raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)

  results = { results: [], next: nil }
  coll = coll.skip(params[:offset].to_i) if params.key?(:offset)
  size = (params[:size] || 10).to_i
  coll = coll.limit(size)
  coll.scroll(params[:cursor]) do |record, next_cursor|
    results[:results] << record if record
    results[:next] = next_cursor.to_s
    break if results[:results].count >= size
  end
  results[:total_count] = coll.count if params[:total_count] && coll.respond_to?(:count)
  results
end