Skip to content

Instantly share code, notes, and snippets.

@sjednac
Last active August 29, 2015 14:07
Show Gist options
  • Save sjednac/48c442fea3398b948e9c to your computer and use it in GitHub Desktop.
Save sjednac/48c442fea3398b948e9c to your computer and use it in GitHub Desktop.
An example Rapture IO client for a RESTful web service.
scalaVersion := "2.11.1"
mainClass := Some("RaptureIORedditReader")
libraryDependencies ++= Seq(
"com.propensive" % "rapture-io_2.11" % "0.10.0",
"com.propensive" % "rapture-uri_2.11" % "1.0.0",
"com.propensive" % "rapture-net_2.11" % "0.10.0",
"com.propensive" % "rapture-json_2.11" % "1.0.0",
"com.propensive" % "rapture-json-jackson_2.11" % "1.0.0"
)
import rapture._
import core._, io._, net._, uri._, json._, codec._
import encodings.`UTF-8`
import jsonBackends.jackson._
case class PostContainer(kind: String, data: Post)
case class Post(id: String, subreddit: String, score: Int, title: String, author: String, permalink: String) {
lazy val topComment = {
val json = Json.parse(uri"http://www.reddit.com/r/${subreddit}/comments/${id}.json?sort=top&limit=1&depth=1".slurp[Char])
json(1).data.children(0).data.as[Comment]
}
}
case class Comment(body: String) {
override def toString = body
}
object RaptureIORedditReader extends App {
val period = "day"
val count = 0
val limit = 5
val sort = "top"
val subreddit = "worldnews"
val postsJson = Json.parse(uri"http://www.reddit.com/r/${subreddit}/${sort}.json?t=${period}&count=${count.toString}&limit=${limit.toString}".slurp[Char])
val posts = postsJson.data.children.as[Seq[PostContainer]].map(_.data)
posts.foreach { post =>
println(s"* ${post.title} [${post.score}]")
println(s"http://reddit.com/${post.permalink}")
println(s"\n${post.topComment}\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment