Skip to content

Instantly share code, notes, and snippets.

View jkyamog's full-sized avatar

Jun Yamog jkyamog

View GitHub Profile
@jkyamog
jkyamog / gist:6521932
Created September 11, 2013 10:37
Play api json date reader and writer for extended mongo dates
object MeetingMongo {
val dateReader: Reads[Date] = new Reads[Date] {
def reads(jsValue: JsValue): JsResult[Date] = {
(jsValue \ "$date").validate[Long].map { l => new Date(l) }
}
}
val dateWriter: Writes[Date] = new Writes[Date] {
def writes(date: Date): JsValue = Json.obj("$date" -> JsNumber(date.getTime))
}
@jkyamog
jkyamog / gist:6521880
Last active December 22, 2015 19:49
consuming autosource findByStream and using for solr batch indexes.
def batchInsert(elems: Enumerator[Iterator[T]])(implicit ctx: ExecutionContext): Future[Unit] = {
val indexByChunks = Iteratee.foreach[Iterator[T]] { i =>
i.grouped(batchSize).foreach { chunk =>
val json = Json.toJson(chunk)(Writes.seq)
solr.index(json)
}
}
elems run indexByChunks
}
def in(header:String) : Any = // existing method
exchange.getIn.getHeader(header)
def in[T](header: String)(implicit manifest: Manifest[T]) : T = // new method that supports type
exchange.getIn.getHeader(header, manifest.runtimeClass).asInstanceOf[T]
// existing code
val fileName = ex.in("CamelFileName") // fileName is any, which is normally casted
// passing a type, better
@jkyamog
jkyamog / gist:5687702
Last active December 17, 2015 23:09
reactive mongo that can be used with angular resources. make sure to perform validation before inserts and updates. there are 2 json readers, this is used to transform mongo object to and from the front end (FE). Then 2 sample api calls to use them, there is an assumption that id or _id will always be there, otherwise its a runtime exception.
val mongoToFE =
__.json.update((__ \ 'id).json.copyFrom( (__ \ '_id \ '$oid).json.pick )) andThen
(__ \ '_id).json.prune
val FEtoMongo =
__.json.update((__ \ '_id \ '$oid).json.copyFrom( (__ \ 'id).json.pick )) andThen
(__ \ 'id).json.prune
Async {
val items = collection.find(BSONDocument("_id" -> objectId)).cursor[JsObject]
@jkyamog
jkyamog / gist:5674235
Created May 29, 2013 22:09
reactive mongo using gridfs to list files
import reactivemongo.api.gridfs.Implicits._
def listFiles(docId: String) = Action {
Async {
listOfFiles(docId).map (files => Ok(files))
}
}
def listOfFiles(docId: String) = {
val files = gridFS.find(BSONDocument("document" -> new BSONObjectID(docId)))
@jkyamog
jkyamog / gist:5617158
Created May 21, 2013 02:32
deadbolt-2 using ldap
// controller action
def create = SecuredAction(Seq("roleA"), parse.json) { implicit request =>
}
def ldapFind = Action { request =>
val results = LdapPlugin.filter("userPrincipalName", "foo", "dc=foo,dc=com", "cn")
Ok(results.map{ r => r.getAttributeValue("cn")}.mkString)
"Document Controller" should {
"create a document" in new WithApplication(FakeApplication(additionalConfiguration = additionalConfig)) {
val json = Json.obj("title" -> "Hello Bob",
"description" -> "description")
val result = DocumentController.createDocument(FakeRequest("POST", "/documents", FakeHeaders(), json))
status(result) must equalTo(OK)
}
@jkyamog
jkyamog / gist:4560541
Created January 17, 2013 22:42
reactivemongo, upload file and create from json
def uploadFile = Action(gridFSBodyParser(gridFS)) { request =>
val upload = request.body.files.head.ref
Async {
upload.map {
case file => {
Ok("File uploaded" + file.id)
}
}.recover {
case _ => BadRequest
@jkyamog
jkyamog / gist:2870693
Created June 4, 2012 20:32
left outer join
package experiments
object LeftOuterJoinMap {
def main(args: Array[String]) {
val map1 = Map(1 -> "a", 2 -> "b", 3 -> "c")
val map2 = Map(1 -> "1", 3 -> "3")
val joined = (new LeftOuterJoinMap(map1, map2)).join