Created
June 15, 2012 20:47
-
-
Save andypetrella/2938623 to your computer and use it in GitHub Desktop.
Steal Play2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def both = stealUserNameAndId {case ((username, id)) => | |
Action { | |
Ok(username + " " + id) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Intercept[B] ( | |
steal: RequestHeader => ValidationNEL[String, B], | |
err: (RequestHeader, Failure[NonEmptyList[String], B]) => Result) extends Interceptor[B] { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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