Tag Archives: code

Results are in – No One Likes Working With Time

15 Jan

20fswp3I recently performed a very Scientific survery asking ‘do you like working with time?’  I don’t mean ‘working with time constraints’, or ‘working with a person whos name is Time but they probably spell it like PThyhm (the P is silent).’  No, I mean working with time (timestamps, date formating, human-friendly time representation, etc) in your code — do you like it?

The results may or may not surprise you.  With a sample size of 1, most if not all of those surveyed responded that they very strongly dislike working with timestamps!

I strongly agree with the person surveyed, so that’s why I’m so elated that I stumbled upon this repo on github the other day — jQuery timeago.  Yes!  Finally I can just dump my timestamps straight from the database onto the page, call $.timeago(’selector’), and bam — a perfectly human readable, self updating time representation.

If you’re one of the many that agree with the extremely scientific survey above, then I highly recommend you give this jQuery plugin a try – here’s the plugins homepage.

Using CouchDB For Storing Google Geocoded JSON Data

15 Aug

Recently, I’ve been working on a project that requires a lot of geocoding.  Google provides a free service that takes any string as an input and returns a bunch of JSON encoded data if that string matches a physical location.  Check it out here.

Unfortunately, Google’s geocoding service is limited to 15k requests per day per IP.  Sounds like a lot, but for certain applications this limit can be reached very quickly.

What’s a good solution in these cases?  Why, how about use my favorite up-and-coming database, CouchDB, to store a repository of geo-data mined from Google?  Why use CouchDB?  As previously mentioned, google natively return JSON data for geocoding requests.  Perfect.  CouchDB just happens to store documents natively in JSON!

The following is a rough and tumble library I put together called GeoCouch that you can use to easily handle geocoded data in CouchDB.  This lib has a narrow scope right now – the requirements are:

  1. PHP 5+
  2. CouchDB
  3. A Google API Key (get one here)

Usage

Using the GeoCouch is dead simple.
Grab the file here.

conf parameters!
	 */
	$GeoCouch = new GeoCouch();

	/*
	 * The all-in-one method.
	 * This geocodes the string and writes it to CouchDB
	 * The second parameter is any other fields other
	 * than the Google data that you want to save along
	 * with this document.
	 *
	 * NOTE: if this address already exists in CouchDB
	 * a new revision is created.
	 *
	 * Returns the CouchDB response, i.e.:
	 * {"ok" : true, "rev":"3825793742", "id" : "dallas-tx" }
	 */
	$GeoCouch->save('Dallas, TX', array('custom_field' => 'value')); 

	/*
	 * Simply geo coding.
	 * Does not write to CouchDB.
	 * Returns an Google Geocoded Object.
	 */
	$geoObj = $GeoCouch->geoCode('Dallas, TX');

	/*
	 * Write some Geo JSON to CouchDB.
	 * First parameter is a unique name for the data
	 * Second parameter is the JSON - in
	 * this case the json_encoded $geoObj from above.
	 */
	$GeoCouch->put('Dallas, TX', json_encode($geoObj));

	/*
	 * Get some existing geo data
	 */
	$geoObj = $GeoCouch->get('Dallas, TX');
?>