Cron — Rake tasks & Google Drive API
How to write cron-rake tasks and make use of g-drive API

Background:
I am studying Electrical Engineering at Grove School of Engineering in NYC. I am also software engineering intern at Bandwagon Taxi-share. Below I am going to share a project I was given to work on at Bandwagon. I will share my process of how I approached to achieve some of the tasks at hand.
Task:
I was asked to create a weekly/daily digest that contains important metrics and analysis of customer feedback for the team to look at. The idea behind it was that the team could conveniently take actions on weekly/daily basis about product feature implementation and other decisions based on this information that the digest will provide.
Majority of our data lives in our data collection engine — Keen (https://www.keen.io). So I decided to use Keen and pull desired data points from there and append those analysis/metrics in a google doc.
Solution:
To take on this task I decided to use Cron-Rake tasks that runs some scripts daily/weekly and spits output in a Google document.
Learned new stuff
→ Google Drive API
→ how to write Cron — Rake tasks
→ Ruby — CSV
Google Drive API
To setup and use Google Drive API — I used this Ruby gem.
google-drive-ruby - A Ruby library to read/write files/spreadsheets in Google Drive/Docs.github.com
setting up and running with this gem is super simple.
- Install the gem $ sudo gem install google_drive
- Head over to google developers console to get your client id and client secret https://console.developers.google.com/apis/credentials

Click on credentials → OAuth client ID → other → save your client ID and client secret
Once you have your client ID and client secret. Create a new file in your project directory, name it config.json and paste your client ID and client secret in there
your config.json will look like this

Usage:
For my task I had to play around with read/write files in Google Drive.
To get yourself familiarize with different options available on how to read/write files in Google Drive — play with these
Explanation:
Save the above file with .rb extension and play with the output in terminal. The first time you run this file — you will be asked to go to a link and verify your google account. It is just a one time thing — any preceding calls using the api won’t need that.
→ lines 9–10 will print all of your google drive files
→ line 17 is creating a var file and capturing a file from your google drive named “hello.txt”
→ line 21 will update contents of your hello.txt(file in your g-drive) with a local file you specify the path.
This is pretty much what I needed to get up and running with my task. If you do explore some nifty tricks with G-Drive API I will appreciate if you do let me know :)
Setting up Cron — Rake tasks:
To accomplish my task, the next thing I had to learn was Cron (Task Scheduler)and how could write good Rake tasks.
The first question I had was what is Cron?
Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.
Also what the heck Rake does? This stack overflow post was helpful in answering.
Rake is a build language, similar in purpose to make and ant. Like make and ant it’s a Domain Specific Language, unlike those two it’s an internal DSL programmed in the Ruby language. In this article I introduce rake and describe some interesting things that came out of my use of rake to build this web site: dependency models, synthesized tasks, custom build routines and debugging the build script.
Additionally, these resources came in handy to learn a bunch about rake and cron
→ [1] http://www.stuartellis.eu/articles/rake/
→ [2] http://www.sitepoint.com/schedule-cron-jobs-whenever-gem
→ http://jasonseifer.com/2010/04/06/rake-tutorial
→ http://tutorials.jumpstartlab.com/topics/systems/automation.html
→ http://jerodsanto.net/2009/08/self-scheduling-ruby-scripts
→ Whenever gem: https://github.com/javan/whenever

You are welcome to spend time learning a bunch about Rake and how Cron works. But after understanding [1] and [2] — you can become a cron — rake ninja in 30 minutes. And with ruby whenever gem you can completely jump the curve of learning Cron at all — but its a good idea to learn a little bit about it before you move on.
Example:
This is an example metric getting uploaded in the google doc.

This is screen capture from my project dir Rake file.
→ Lines 53–56 is a rake task instantiating a class and running a method named “net_promoter_score” and passing Date.today as an argument
→ Lines 44–48 is a task uploading the output of net_promoter_score in a google doc
This is what net_promoter_score method looks like

This method is calculating weekly NPS average and putting output in CSV file. Which in turn updates the google doc when the cron-rake task runs every week. With the same approach I built the weekly digest.
To optimize on this task — next step will be to setup this in the cloud so my computer doesn’t need to be connected and open when the tasks are scheduled to run. There are a lot of options to use cloud computing services. I also have access to a rails server — that I can make this project part of and have the server run workers when desired.
I am currently working on it — will try to write a post when finished.