Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. case class Project(name: String, description: String)
  2.  
  3. def getScalaProjects: Seq[JsValue] = {
  4.  
  5. val url = "https://api.github.com/search/repositories?q=scala"
  6. val gitScalaRepos = Source.fromURL(url).getLines
  7.  
  8. val gitJSON = Try(Json.parse(gitScalaRepos.get mkString "n")) match {
  9. case Success(json) => json
  10. case Failure(f) => throw new AppException("Could not parse JSON.")
  11. }
  12.  
  13. implicit val projectReads: Reads[Project] = (
  14. (JsPath "name").read[String] and
  15. (JsPath "description").read[String]
  16. )(Project.apply _)
  17.  
  18. for {
  19. i <- 0 until 5
  20. p = (gitJSON "items")(i).validate[Project]
  21. p match {
  22. case s: JsSuccess[Project] => s.get
  23. case e: JsError => throw new AppException("Could not parse JSON: " +
  24. JsError.toFlatJson(e).toString())
  25. }
  26. } yield p
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement