Module: SimpleFeed::Providers::Hash::Paginator

Included in:
Provider
Defined in:
lib/simplefeed/providers/hash/paginator.rb

Overview

Include this module into any provider etc that has access to the feed.fetch methods, and it will provide paginate method based on all.

Of course this is not very efficient, because it requires fetching all events for the user.

Instance Method Summary collapse

Instance Method Details

#order_events(events, &block) ⇒ Object


22
23
24
25
26
27
# File 'lib/simplefeed/providers/hash/paginator.rb', line 22

def order_events(events, &block)
  return nil unless events
  events.sort do |a, b|
    block ? yield(a, b) : b.at <=> a.at
  end
end

#paginate(user_ids:, page: nil, per_page: feed.per_page, &block) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/simplefeed/providers/hash/paginator.rb', line 10

def paginate(user_ids:, page: nil, per_page: feed.per_page, &block)
  response = feed.fetch(user_ids: user_ids)
  response = SimpleFeed::Response.new(response.to_h)
  response.transform do |*, events|
    paginate_items(order_events(events, &block), page: page, per_page: per_page)
  end
end

#paginate_items(items, page: nil, per_page: nil) ⇒ Object


18
19
20
# File 'lib/simplefeed/providers/hash/paginator.rb', line 18

def paginate_items(items, page: nil, per_page: nil)
  (page && page > 0) ? items[((page - 1) * per_page)...(page * per_page)] : items
end