Skip to content
October 12, 2012 / edeustace

My first play plugin: Mongo Rest Layer

I’ve been working on an internal project where we needed a quick and easy way to interact with a mongo db over rest. Mongolab.com has an existing rest layer that does this, but we wanted to route it through our Play! 2 app.

So I created a Play! plugin called Mongo Rest Layer. Its a simple library that adds REST capabilities to your play app with little setup. You just need to pass in a mongo uri and override the onRouteRequest method of GlobalSettings. The REST behaviour is modelled on the mongolab.com api, but you can add your own flavour by implementing: brokers.RequestBroker.

May be of use to others.

 

August 20, 2012 / edeustace

A little Json processing library

I’ve recently had to perform some bulk imports into our project database. The data was sent to us as spreadsheets and we need to insert this into our mongo db.

The steps I took were:

1. Convert xls to csv

2. Convert csv to json using: http://www.cparker15.com/code/utilities/csv-to-json/

3. Process json keys to match our data model keys

4. Run mongo js script to bulk insert the json

For step 3, I couldn’t find a tool that was suitable for the task of mapping the json, so I put together a little library that did it for me.

The usage is fairly straightforward:

java jar json-processor-fat.jar source.json map.json output.json

Where the source.json is the json with the data that you wish to change, the map.json is a set of mapping rules to use and output.json is the name to use for the generated json.

As an example if you had this as your source:

[ { "fruit" : "Apple" } ]

And a map with the following:

{ "fruit" : "favouriteFruits" }

The output would be:

[ { "favouriteFruits" : "Apple" } ]

In this example, the map defines a new key for a key in the source aka fruit -> favouriteFruits

There are some additions commands you can add in the map that perform various actions:

  • !ignore – suppresses that item from the output
  • !camelCase – convert the existing key to camelCase eg: “First Name” to “firstName”
  • !merge-> merge a couple of the keys into one keyed value. With merge you have to specify a new key name and also provide a template for that new key. So if you add “!merge->newKey”, “newKey” will be the name of the new keyed item, which will use a template called “newKey” that adds the other items into it.
  • !insert – insert key/value into output.

There is more documentation on github.

May be of use to someone.




August 1, 2012 / edeustace

Mongo gem for make backing up with mongo easier

On my current project we are using mongo as our db. This means alot of backing up, copying and restoring dbs. mongo has great tools for this namely mongodump and mongorestore, but I was getting tired of having to add my configurations for different dbs into different shell scripts.

Also I didn’t really have a back up system in place.

To make my life easier – I’ve put together a little gem called *mongo-db-utils*. Its a wrapper on top of mongodump and mongorestore.

The nice thing about it is that you can save your configuration, so the next time you launch it it’ll remember the db’s or amazon buckets you work with.

Also it backs up the dbs using its own convention so you’ll always know where the latest db is. The config file and the backups are stored in ~/.mongo-db-utils.

Its on github, it may be a little rough around the edges (let me know if you’ve any issues), but I’ve been using it daily here for the last week and its working good. Unfortunately it’ll only run on Linux/OSX at the moment.

July 14, 2012 / edeustace

Adding routes programmatically to Play 2.* apps

There’s some discussion about how one can programmatically add routes to a Play 2.* app. It appears that you can’t add them to the generated Routes class, but you can handle the web requests yourself by overriding ‘onRouteRequest’.

</code>
<code>override def onRouteRequest(request: RequestHeader): Option[Handler] = {
  //do our own path matching first - otherwise pass it onto play.
  request.path match {
    case "/injectedRoute" => Some(controllers.Application.customRoute)
    case _ => Play.maybeApplication.flatMap(_.routes.flatMap {
      router =>
      router.handlerFor(request)
    })
  }
}</code>
<code>

Here’s an example on github: https://github.com/edeustace/play-injected-routes-example

See this question too: http://stackoverflow.com/questions/10116286/programatically-adding-a-route-in-play2-0/11483556

May 15, 2012 / edeustace

Powershell script do download a zip and copy its contents to specific locations on downloaded machine

On my current project we are using windows servers for our deployments. So I’ve had to write a bit of Powershell. The script below downloads a zip from a given url and copies the contents to locations on the destination hard drive.

gist is here

April 13, 2012 / edeustace

Stored procedure to create a history table and triggers for a given table

Here’s a wee stored procedure for sql server 2008 that generates a history table and triggers for a given table:

https://gist.github.com/2377468

March 30, 2012 / edeustace

Scala – generate random alphanumeric sequence

For Scala Puzzles I need to create a random sequence to allow people to link to anonymous puzzles. I found an example here, but it was using toString instead of mkString, so I’ve tweaked it a tiny bit (gist is here)


import scala.util.Random

def uniqueRandomKey(chars: String, length: Int, uniqueFunc: String=>Boolean) : String =
{
 val newKey = (1 to length).map(
   x =>
   {
     val index = Random.nextInt(chars.length)
     chars(index)
   }
  ).mkString("")
  
 if (uniqueFunc(newKey))
  newKey
 else
  uniqueRandomKey(chars, length, uniqueFunc)
}

/**
 * implement your own is unique here
 */
def isUnique(s:String):Boolean = true

val chars = ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9') ++ ("-!£$")
val key = uniqueRandomKey(chars.mkString(""), 8, isUnique)
println(key)