If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Friday, January 11, 2013

Rendering tweets with Play2.1

Tweet Controller
package controllers

import play.api.mvc.{Action, Controller}
import play.api.libs.functional.syntax._

import play.api.libs.json._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.ws.WS

case class Tweet(from: String, text: String)

object Tweets extends Controller {

  implicit val tweetReads = (
    (__ \ "from_user_name").read[String] and
    (__ \ "text").read[String]

  def tweetList(query: String) = Action {
    Async {
      val results = 10
      val responsePromise =
          .withQueryString("q" -> query, "rpp" -> results.toString).get
      responsePromise.map {
        response =>
          val tweets = Json.parse(response.body).\("results").as[Seq[Tweet]]

GET     /tweets/:query              controllers.Tweets.tweetList(query: String)

scala template:
@(tweets: Seq[Tweet])
@main("Tweets!") {


@tweets.map { tweet =>
  • @tweet.from: @tweet.text
} }

So invoking following URL http://localhost:9000/tweets/playframework now gives me the last 10 tweets containing playframework (case insensitive). I think it's pretty slick how little code is needed.


  1. Hey, you should be more consistent with labels. You have play and playframework already ;)

  2. yeah... well... if you launch a play application it will tell you "Welcome to Play 2.1". But when querying twitter it makes sense to use 'playframework' as i'm not interested in every tweet with keyword 'play' ;-)