Asyncrazy Temperature Server

Putting this all together, we can build a server that would very efficiently get your IP address banned by Google if you actually put it on the web. But it’s totally cool to run it locally. We think.

import dispatch._
import unfiltered.request._
import unfiltered.response._
import unfiltered.netty._

object Location extends
Params.Extract("location", Params.first ~> Params.nonempty)

object Temperature extends async.Plan with ServerErrorResponse {
  val http = new nio.Http
  def intent = {
    case req @ GET(_) =>
      req.respond(view("", None))
    case req @ POST(Params(Location(loc))) =>
      http(:/("") / "ig/api" <<? Map(
        "weather" -> loc) <> { reply =>
          val tempC = for {
            elem <- reply \\\\ "temp_c"
            attr <- elem.attribute("data")
          } yield attr.toString
          req.respond(view(loc, tempC.headOption))
  def view(loc: String, temp: Option[String]) = Html(
        <form method="POST">
          <input value={loc} name="location" />
          <input type="submit" />
        { { t => <p>It's {t}°C in {loc}!</p> }.toSeq }

Put all that into a console, then start it:


You can lookup the current temperature for almost anywhere; just type in a place name or postal code and Google will probably figure it out.

When you are done checking the temperature of exciting places around the world, shutdown the handler’s Dispatch executor.