Skip to content

Instantly share code, notes, and snippets.

@negator
Last active December 10, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negator/4366785 to your computer and use it in GitHub Desktop.
Save negator/4366785 to your computer and use it in GitHub Desktop.
Build an activity from a post, using its associated like and comments.
type CommentsOrLikes = Either[Comments, Likes]
def buildActivity(post: Post): Promise[Activity] = {
val likeUrl = LIKE_URL % (post.id, target.token)
val commentsUrl = COMMENT_URL % (post.id, target.token)
/*Construct paging enumerators, mapping each value to either Left or Right*/
val comments = pagingEnumerator(commentsUrl).map(Left.apply)
val likes = pagingEnumerator(likeUrl).map(Right.apply)
//Enumerate likes and comments in parallel with the 'interleave' function
val content:Enumerator[CommentsOrLikes] = likes interleave comments
/*Initial value for fold*/
val activity = Activity(Nil, post, Nil)
/*Fold over each enumerated value, building the activity as we go*/
val activityIterateePromise =
content |>> Iteratee.fold[CommentsOrLikes, Activity](activity) {
case Left(comments) => activity copy (comments = activity.comments ++ comments)
case Right(likes) => activity copy (likes = activity.likes ++ likes)
}
/* Finally, activityIterateePromise will be a Promise[Iteratee[Activity]],
* which we need to turn into an Iteratee[Activity] and then run it to
* actually build our activity */
Iteratee.flatten(activityIterateePromise).run
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment