Last active
August 29, 2015 14:03
-
-
Save Rydgel/a327cd8cadb1a30446c4 to your computer and use it in GitHub Desktop.
Print finished World Cup match results
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
| import scala.concurrent.duration._ | |
| import scala.concurrent.Await | |
| import dispatch._, Defaults._ | |
| import play.api.libs.json._ | |
| object Main { | |
| def formatMatch(jsonMatch: JsObject): Option[String] = for { | |
| cHome <- (jsonMatch \ "home_team" \ "country").asOpt[String] | |
| gHome <- (jsonMatch \ "home_team" \ "goals").asOpt[Int] | |
| cAway <- (jsonMatch \ "away_team" \ "country").asOpt[String] | |
| gAway <- (jsonMatch \ "away_team" \ "goals").asOpt[Int] | |
| } yield s"$cHome $gHome x $cAway $gAway" | |
| def parseJson(jsonString: String): String = | |
| Json.parse(jsonString).as[List[JsObject]] | |
| .collect { case i if (i \ "status").as[String] == "completed" => formatMatch(i).getOrElse("") } | |
| .mkString("\n") | |
| def matchesOver(): Future[String] = | |
| for (request <- Http(url("http://worldcup.sfg.io/matches") OK as.String)) yield parseJson(request) | |
| def main(args: Array[String]) = println(Await.result(matchesOver(), 10 seconds)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment