loicdescotte
4/21/2012 - 4:08 AM

humf

humf

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.ws._
import play.api.libs.iteratee._
import play.api.libs.concurrent._

object Application extends Controller {
	
	val EXPECTED_LENGTH = 15;
	
	def index = Action {
		val p1 = WS.url("http://linkedin.com").get().map{ _ => "linkedin".toList }.map(Some.apply)
		val p2 = WS.url("http://zenexity.com").get().map{ _ => "zenexity".toList }.map(Some.apply)
		val p3 = WS.url("http://news.ycombinator.com").get().map{ _ => "ycombinator".toList }.map(Some.apply)

		val r = ((Enumerator.fromCallback(() => p1) >-
		Enumerator.fromCallback(() => p2) >-
		Enumerator.fromCallback(() => p3)).flatMap{ xs => Enumerator(xs:_*) } &>
		Enumeratee.take(EXPECTED_LENGTH)) |>>
		Iteratee.fold(List[Char]()){ (es,e: Char) => e :: es }
			
		val p = r.flatMap( _.pureFold(
				(res, _) => res.toString,
				_ => "Ouate?",
				(err, _) =>  err.toString))
					
		AsyncResult(p.map(s => Ok(s)))
	}
}