Friday, December 20, 2013

Last Day of the Month: Javascript

Calculating the last day of the current month or subsequent months is a common populated form field for quotesrot contracts where an expiry date is required.  Goes a little something like this:

Today's date in MM/DD/YYYY:
// today's date
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();
var vdate = month + "/" + day + "/" + year;

Last day of the next month:
// last day of next month
if (d.getMonth() + 1 == 12) {
  var nmonth = 1;
  var nyear = d.getFullYear() + 1;
  var lastday = new Date(nyear, 1, 0).getDate();
else {
  var nmonth = d.getMonth() + 2;
  var nyear = d.getFullYear();
  var lastday = new Date(nyear, d.getMonth() + 2, 0).getDate();
var expdate = nmonth + "/" + lastday + "/" + nyear;

There are a dozen ways to get to this result and always interested in other ways to get the same info.

Sunday, July 7, 2013

Cyclemeter Upload to Strava with Google Apps Script

As a long time Cyclemeter user I have logged about 6000 off-road miles and love the app and it's feature set.  When Strava made it way onto the scene, I was impressed with the community and analytic features that were offered but since Strava has their own mobile apps I have waited long enough for Cyclemeter to add a direct auto-share to their app so I wrote a Google Apps Script to do this for me.

If you use GMail, you are in luck since Google give you Apps Script to add some major automation to a number of their services.  Here is how I did it...

Step 1: Setup Cyclemeter to email you the GPX link to your GMail account.  Go to More -> Settings -> Email Updates (turn on) ->Auto Send Settings -> Done ->Just add the GPX link to the email.

Step 2: Then in GMail setup a filter to label these emails "StravaUpload" by selecting specific information in the Subject.  In my case I used " Cyclemeter Cycle (Mountain)" so it only uploads my MTB rides and not my other activities like runs.  I am going to assume you know how to add filters and labels in GMail.

Step 3: Now the Script.  This part might be a stretch for some but I will share a simple way to do this.  Google Apps Scripts can exist on their own in Google Drive but to create them you need to connect the Google Apps Script to Drive by clicking on the "Connect More Apps" link at the bottom and searching for "Google Apps Script".  You can also create a Doc or Sheet and go to the "Tools" menu and select "Script Editor".  I write a lot of code for GAS so I use the direct method.  Either way, you can get into the script editor and add this function:

 function uploadToStrava() {  
  //Call GMail to look for StraveUpload labeled emails (should only be one)  
  var ulemail = GmailApp.getUserLabelByName('StravaUpload').getThreads();  
  //Quit if email is missing  
  if (ulemail.length != 0){  
   //Get only the first email in the matching array  
   var ulmessage = ulemail[0].getMessages();  
   //Get the entire body of the email  
   var ulbody = ulmessage[0].getBody();  
   //Calculate start position of gpx url  
   var ulbstart = ulbody.indexOf('');  
   //Calculate start position of extension of gpx  
   var ulbend = ulbody.indexOf('.gpx');  
   //Get only substring of link  
   var ullink = ulbody.substr(ulbstart, (ulbend - ulbstart+4));  
   //Fetch gpx file from url  
   var gpxfile = UrlFetchApp.fetch(ullink).getBlob();  
   var gpxname = gpxfile.getName();  
   //Send email to Strava Upload email with gpx attachment  
   MailApp.sendEmail( {  
    to: '',  
    subject: 'Upload to Strava',   
    htmlBody: "Upload attached.",  
    attachments: gpxfile  
   //Move Cyclemeter email to trash  

Since Cyclemeter does not create an attachment in the Auto Send options, I had to get the URL and get the attachment from the link.  Now we need this script to trigger daily... I also didn't care for it to parse a number of rides at once so I left out the looping for uploads for now. 

Step 4: Trigger the script to run at whatever interval you like.  I usually ride in the am so I had the script trigger between 9 - 10am but you can pick the interval you like.

Now I can complete my ride, hit Done in Cyclemeter and my email will send and once the trigger for the GAS runs, the email is evaluated, an email is sent to Strava's upload email with a GPX attachment and my ride shows up in Strava without me having to manually upload it.  Works for me but I hope Cyclemeter adds this into the app or simply adds email GPX attachments in their auto share settings.  Hope this helps you. ~Lou

Saturday, February 23, 2013

Migrating from Posterous to Blogger

At the news that Twitter was buying Posterous, it was only a matter of time before the end would be announced, so I started reevaluating another solution for blogging.  I had written off Blogger for years since it was pretty primitive and lacked features that mattered to me.  With the further integration of Google + and clients on most mobile devices, I decided to go Google and give it a try.

In order to move from Posterout to Blogger you need to use Wordpress as a middle man first and then use an open source app called Wordpress2Blogger conversion utility.  Here are the steps:

Posterous Export:

  1. Visit
  2. Click on the "Request Backup" button next to the space you want to backup.  Depending on the size of the blog this could take minutes or hours.  My 200 post blog took about 10 min or so.  When it is ready you will receive an email.
  3. Go back to and click the download button to receive the zip file of your space.  Although this appears to be in a format for Wordpress to import, the WordPress2Blogger tool will not work directly on this file. Import/Export:
  1. Login to 
  2. Create a new blog with the same name
  3. Go to Tools -> Import and select Posterous
  4. Select the Wordpress_export_1.xml file in the unzipped package download from Posterous
  5. Follow the instructions to import.
  6. Once complete, go to Tools -> Export and export the xml file.
Convert Wordpress Export:
  1. Visit
  2. Click "Choose file" and select the xml file download from Wordpress
  3. Click Convert.  This will convert the file to a blogger import file
  4. Once complete, a file will download in the blogger format.
Blogger Import:
  1. Create your new blog with your blog's name
  2. Go to Settings -> Other and click on the "Import blog" link under "Blog tools".
  3. Select the xml file from Wordpress2blogger
  4. Start blogging on Blogger.
The process sounds elaborate but the process is pretty fast and I was able to move 4 blogs in about 30 minutes so after the first one it is somewhat routine.  There are some differences with Blogger but overall there seems to be ways to do most of what I did before without too much hassel.  I do miss the simplicity of Posterous but as you know it had it's frustrations as well.  ~Lou

Wednesday, February 13, 2013

Prep Ruby on Rails Apps for GitHub Publishing


When I started to publish code on GitHub I began searching for the best way to share my code without accidentially giving away my keys, passcodes or usernames. I tried to fork a version for publishing but branch history will still allow the public to see what was in previous versions. After trying a few methods, I opted to store all my passcodes in a *.yml file in config folder and just add it to the .gitignore. This required a few notes to let cloners know what to do with the code in the case they were to use their own keys directly.

Create a file under the ‘/config’ folder ‘ app_passwords.yml’

defaults: &defaults 
    app_key: my_app_key
    app_secret: my_app_secret 

     <<: *defaults

     <<: *defaults 

     <<: *defaults

In order to load ‘app_passwords.yml’ it needs to reside at the top of the ‘/config/initializers’ folder. I placed a file called ‘01_app_passwords.rb’ (I added the 01 to put it at the top of the file list since initializers are processed in order).

In ‘config/initializers/01_app_passwords.rb’

MY_PASSWORDS = YAML.load_file("#{Rails.root}/config/app_passwords.yml")[Rails.env]

Now that the passwords are loaded into the Rails envionment, they can be called from the controller.

In ‘my_controller.rb’


Now that the app’s sensitive data is located in a single place, the ‘app_passwords.yml’ can be added to your ‘.gitignore’ file and it will not be committed to GitHub (as long as you don’t have other commits to your code history). There are now two options for your README for letting users who download your code to change in order to use it with their own passwords.
  1. Change ‘MY_PASSWORDS['app_key’]‘ and 'MY_PASSWORDS['app_secret’] with their own keys and passwords. or….
  2. Add their own ‘app_passwords.yml’ to the ‘/config’ folder.
This seems to get the worry out of the way and let you code and commit without worry (well at least after the first commit and check!). ~Lou