Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created May 23, 2018 17:06
Show Gist options
  • Save bdarfler/7fe3d3fd3ab35957cb6ed5ca8a23d03a to your computer and use it in GitHub Desktop.
Save bdarfler/7fe3d3fd3ab35957cb6ed5ca8a23d03a to your computer and use it in GitHub Desktop.
class ActivityAggregator
def initialize(streams)
@streams = streams
end
def next_activities(limit)
activities = []
while (activities.size < limit) && more_activites? do
activities << next_activity
end
activities
end
private
def next_activity
@streams.select{ |s| has_next?(s) }.sort_by{ |s| s.peek.created_at }.last.next
end
def more_activities?
@streams.any?{ |s| has_next?(s) }
end
def has_next?(stream)
stream.peek
true
rescue
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment