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
object JS { | |
def default(js: JsValue): JsValue = js match { | |
case JsObject(fields) => JsObject(fields.map(f => f._1 -> default(f._2))) | |
case JsString(s) => JsString("") | |
case JsArray(a) => JsArray() | |
case JsNumber(n) => JsNumber(0.0) | |
case JsBoolean(b) => JsBoolean(false) | |
case _ => JsString("") | |
} | |
} |
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
# RESTful routes for Users collection | |
GET /users controllers.Users.list | |
POST /users controllers.Users.insert | |
DELETE /users controllers.Users.deleteAll | |
# RESTful routes by User | |
GET /users/:id controllers.Users.findById(id: String) | |
PUT /users/:id controllers.Users.update | |
DELETE /users/:id controllers.Users.delete(id: String) |
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
object Application extends Controller { | |
//store data | |
def findById(id: String) = Action { | |
val user: User = Cache.getOrElse[User]("user." + id) { | |
User.find(id) | |
} | |
Ok(views.html.show(user)) | |
} | |
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
//atomic action to log requests | |
case class Logged[A](action: Action[A]) extends Action[A] { | |
def apply(request: Request[A]): Future[SimpleResult] = { | |
Logger.info("Calling action") | |
action(request) | |
} | |
lazy val parser = action.parser | |
} |
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
//Stand-alone logging | |
def editUser = Logged { | |
val result = User.edit | |
Ok(result.toString) | |
} | |
//Stand-alone measured | |
def calc = Measured { | |
//calculate pi using taylor series | |
val result = Pi.calculate(1000) |
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
//Composed action to handle logging and measuring in same request | |
object LoggedMeasured extends ActionBuilder[Request] { | |
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[SimpleResult]) = block(request) | |
override def composeAction[A](action: Action[A]) = Logged(Measured(action)) | |
} | |
object Application extends Controller{ | |
def batch = LoggedMeasured { | |
val result = User.batchUpload | |
Ok(result.toString) |
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
object Global extends GlobalSettings { | |
def json(errorCode: String, message: String) = Json.obj("ERR" -> errorCode, "MESSAGE" -> message) | |
override def onError(request: RequestHeader, ex: Throwable) = { | |
Logger.error("Internal Error", ex) | |
Future.successful(InternalServerError(json("0001", ex.getStackTrace()))) | |
} | |
override def onHandlerNotFound(request: RequestHeader): Result = Future.successful(NotFound(json("0002", request.toString))) |
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
object MetaDataFilter extends EssentialFilter{ | |
def apply(next: EssentialAction) = new EssentialAction { | |
def apply(rh: RequestHeader): Iteratee[Array[Byte], Result] = { | |
val timeStart = System.currentTimeMillis | |
def bodyToStr: Enumeratee[Any, String] = Enumeratee.map(s => s.toString) | |
def addMetadata(result: PlainResult): Result = { | |
val timeElapsed = System.currentTimeMillis - timeStart | |
result match { |
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
//Scala worksheet | |
import com.josephpconley.jsonpath.JSONPath | |
import play.api.libs.json.Json | |
val store = Json.parse("""{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}}}""") | |
JSONPath.query("$.store.book[*].author", store) | |
JSONPath.query("$..author", store) | |
JSONPath.query("$.store.*", store) | |
JSONPath.query("$.store..price", store) | |
JSONPath.query("$..book[2]", store) |
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
$("a[href^='freedownload']").each(function(a){ | |
window.open($(this).attr("href")); | |
}); |