Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created June 15, 2012 20:47
Show Gist options
  • Save andypetrella/2938623 to your computer and use it in GitHub Desktop.
Save andypetrella/2938623 to your computer and use it in GitHub Desktop.
Steal Play2.0
val stealUserName = Intercept(
r => Cache.getAs[String]("username").toSuccess(NonEmptyList("No username")),
(err, v:Failure[NonEmptyList[String], String]) => BadRequest(v.e.list.mkString("\n"))
)
val stealUserId = Intercept(
r => Cache.getAs[String]("userid").map(_.toInt).toSuccess(NonEmptyList("No userid")),
(err, v:Failure[NonEmptyList[String], Int]) => BadRequest(v.e.list.mkString("\n"))
)
val stealUserNameAndId = Intercept(
r => for {
s1 <- stealUserName.steal(r);
s2 <- stealUserId.steal(r)
} yield (s1, s2),
(err, v:Failure[NonEmptyList[String], (String, Int)]) => BadRequest(v.e.list.mkString("\n"))
)
def both = stealUserNameAndId {case ((username, id)) =>
Action {
Ok(username + " " + id)
}
}
val stealDummyString = Intercept(
r => "dummy".success[NonEmptyList[String]],
(err, v:Failure[NonEmptyList[String], String]) => BadRequest(v.e.list.mkString("\n"))
)
def dummy = (stealUserName compose stealDummyString) {case usernameDummy =>
Action {
Ok(usernameDummy)
}
}
case class Intercept[B] (
steal: RequestHeader => ValidationNEL[String, B],
err: (RequestHeader, Failure[NonEmptyList[String], B]) => Result) extends Interceptor[B] {
}
trait Interceptor[B] {
def steal: RequestHeader => ValidationNEL[String, B]
def err: (RequestHeader, Failure[NonEmptyList[String], B]) => Result
def apply[A](a: B => Action[A]): Action[(Action[A], A)] = {
//[...]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment