This file contains 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
/*Enumeratee to manage writing to the file writer. Mapping any errors to Left*/ | |
type ErrorOrActivity = Either[Error,Activity] | |
def fileWriting: Enumeratee[Activity, ErrorOrActivity] = { | |
/* writeToFile returns a Promise, but the Enumeratee type constraint | |
* does not expect a Promise. flatMap will return an | |
* Enumeratee[Activity,ErrorOrActivity] given a function from Activity | |
* to Promise[ErrorOrActivity]. | |
*/ |
This file contains 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
/* | |
* Flatten an enumerator of enumerators of some type into an enumerator of some type | |
*/ | |
def flatten[T](enumerator: Enumerator[Enumerator[T]]): Enumerator[T] = new Enumerator[T] { | |
def step[A](it: Iteratee[T, A])(in: Input[Enumerator[T]]): Iteratee[Enumerator[T], Iteratee[T, A]] = { | |
in match { | |
case Input.EOF => Done(Iteratee.flatten(it.feed(Input.EOF)), Input.EOF) | |
case Input.Empty => Cont(step(it)) | |
case Input.El(e) => { | |
val promise = e |>> Cont(removeEof(it)) |
This file contains 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
val posts:Enumerator[List[Post]] = pagingEnumerator(postsUrl) map parseToPostList | |
/* parseToPostList does exactly that. Creates a list of Post objects from json*/ | |
val activities:Enumerator[Enumerator[Activity]] = posts.map{ | |
postList => | |
Enumerator.apply(postList:_*) map buildActivity | |
} |
This file contains 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
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) |
This file contains 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
import play.api.libs.ws._ | |
import play.api.libs.iteratee._ | |
def pagingEnumerator(url:String):Enumerator[JsValue]={ | |
var maybeNextUrl = Some(url) //Next url to fetch | |
Enumerator.fromCallback[JsValue] ( retriever = { | |
val maybeResponsePromise = | |
maybeNextUrl map { nextUrl=> | |
WS.url(nextUrl).get.map { reponse => | |
val json = response.json |
This file contains 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
import play.api.libs.ws._ | |
import play.api.libs.json._ | |
import scalaz._ | |
import Scalaz._ | |
import java.net.{ URL => JavaUrl } | |
/* This class makes url manipulation a piece of cake (dark chocolate). It also includes Play WS request generation. | |
Requires scalaz6 and Play 2.0.4 | |
Examples: |
NewerOlder