Skip to content

Instantly share code, notes, and snippets.

View andypetrella's full-sized avatar

Andy Petrella andypetrella

View GitHub Profile
@andypetrella
andypetrella / Build.scala
Created July 20, 2012 19:45
Gatling SBT Test
import sbt._
import Keys._
import GatlingPlugin._
object MinimalBuild extends Build {
val SNAPSHOT = "-SNAPSHOT"
val appName = "gatling-sbt-sample"
val buildVersion = "0.0.1-SNAPSHOT"
@andypetrella
andypetrella / security.scala
Created June 16, 2012 16:34
Interceptors typesafe'ly combined
val securedByUserName = Intercept(
r => Cache.getAs[String]("username").toSuccess(NonEmptyList("No username")),
(err, v:Failure[NonEmptyList[String], String]) => Unauthorized(v.e.list.mkString("\n"))
)
val securedByUserId = Intercept(
r => Cache.getAs[String]("userid").map(_.toInt).toSuccess(NonEmptyList("No userid")),
(err, v:Failure[NonEmptyList[String], Int]) => Unauthorized(v.e.list.mkString("\n"))
)
@andypetrella
andypetrella / both.scala
Created June 15, 2012 20:47
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 {
@andypetrella
andypetrella / monadTransformer.scala
Created June 11, 2012 09:14
Monad Transformer for Promise and Validation
case class ValidationPromised[E, A](promised: Promise[Validation[E, A]]) {
def map[B](f: A => B): ValidationPromised[E, B] =
ValidationPromised(promised map { valid =>
valid.fold(
fail => KO(fail),
suc => OK(f(suc))
)
})
def flatMap[B](f: A => ValidationPromised[E, B]): ValidationPromised[E, B] =
@andypetrella
andypetrella / boilerplate.scala
Created April 22, 2012 14:09
Specs for using the Gatling Play 2.0 plugin
//creates a fake Play 2.0 server running on 3333 which defines the gatling plugin automatically
val server = Util.createServer(3333)
//function that starts the server, should be used in a specs Step BEFORE the whole specification starts
def startServer {
server.start()
}
//function that stops the server, should be used in a specs Step AFTER the whole specification has ran
def stopServer {
@andypetrella
andypetrella / Build.scala
Created March 14, 2012 21:57
Gatling Within Play 2.0
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "gatling-play2-plugin"
val appVersion = "1.0"
/* OFFICIAL GATLING REPO */
@andypetrella
andypetrella / Build.scala
Created March 11, 2012 11:47
Salat and Play 2.0
//...
val novusSnapsRepo = "Novus Snapshots Repository" at "http://repo.novus.com/snapshots/"
val salatCore = "com.novus" %% "salat-core" % "0.0.8-SNAPSHOT" withSources
val salatUtil = "com.novus" %% "salat-util" % "0.0.8-SNAPSHOT" withSources
val appDependencies = Seq(
// Add your project dependencies here,
salatCore,
salatUtil
@andypetrella
andypetrella / Neo4JRestService.scala
Created February 29, 2012 21:17
Deploy Play and Neo4J on Heroku
//PREVIOUSLY ::>> val neoRest = :/("localhost", 7474)
//NOW use `as`
val neoRest = :/(NEO4J_URL, NEO4J_PORT) as (NEO4J_USERNAME, NEO4J_PASSWORD)
val neoRestBase = neoRest / "db" / "data"
val neoRestNode = neoRestBase / "node"
val neoRestRel = neoRestBase / "relationship"
val neoRestCypher = neoRestBase / "cypher"
def selfRestUriToId(uri: String) = uri.substring(uri.lastIndexOf('/') + 1).toInt
@andypetrella
andypetrella / Groups.scala
Created February 27, 2012 21:36
Create Views in Play 2.0 for Neo4J
object Groups extends Controller {
// create a new Group based on its given name
def create = Action {
implicit request => {
//a form that will create a Group based on the request body. See how apply and unapply functions are given after the mapping definition
Form[Group](
mapping(
"name" -> nonEmptyText
)(
(name: String) => Group(null.asInstanceOf[Int], name)
@andypetrella
andypetrella / GraphService.scala
Created February 24, 2012 21:40
Neo4J Play and DAO
//A trait defining some high level operation on graph, hasn't been cleaned but illustrates well what it might be meant for
trait GraphService[Node] {
//entry point of the graph
def root: Node
//get a Node based on its id
def getNode[T <: Node](id: Int): Option[T]
//return all nodes in the graph as a list