Created
May 23, 2018 17:06
-
-
Save bdarfler/7fe3d3fd3ab35957cb6ed5ca8a23d03a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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