In one of my previous posts I showed how to dynamically generate a map from a set of coordinates in Ruby on Rails (like the map on top of this page). This is nice but still required me to manually update my location each time, not very useable. I have a Dopplr account where I enter all my future trips so it feels stupid to enter the same information again on my own website. We can do better than that.
Here is the idea: a script that checks my travel plans on Dopplr for to following day and creates a new map of that location if needed. Also: a perfect excuse to have a look at the Rake build language which I had never used.
The Dopplr API includes a Ruby client which makes our life easier, download the ‘Improved Ruby Client’ in your <yourapp>/lib folder so we can use it later on.
You will need a session key to use the Dopplr API, this key replaces your username and password (read ‘The password anti-pattern’ if you want to know why). Here is how to get one:
I wanted something outside this website that could run automatically, even when the website wasn’t opened. Rake offers you a way to do this: you can build and run scripts independently from the rest of the application while using the same environment as the application.
Here is a simplified version of the task I created. Copy this and save it in your <yourapp>/lib/tasks folder as map.rake.
Now, check your taks with rake -T. You’ll get an overview of all the Rake tasks for your app and there should be a ‘map:date’ and ‘map:update’ one. This means you can run these scripts interactively now:
Almost done. We can now create a new map whenever we want on any given date. Let’s run this script each night at 4 in the morning. Add the following line to your crontab:
0 4 * * * cd <app_path> && /usr/local/bin/rake map:update RAILS_ENV=production
That’s it. The script will now look for your current location in your Dopplr account and create a map based on that location.
This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!