Mentally Reframing Ruby – Part 2

Posted by Flatiron School  /  June 16, 2013

The following is a guest post by Kristen Curtis and originally appeared on her blog. Kristen is currently a student at The Flatiron School. You can follow her on Twitter here.

What the What with the Gmail

After my first experiment with Ruby and Arduino, I wanted to try writing a sketch that would build off of the code I had written from the blinking LED test. Using the blinking LED as an indicator, I thought it would be interesting to see if I could get my Ardunio talking with Gmail to alert me if I had any unread inbox messages. After doing some research, I found a Ruby gem called Gmail that allows me to do just that.

Requiring the Right Gems

Here we’re going to set up the sketch with the gems we’ll be running.

Blog post image: tumblr_inline_moc4atBApQ1qz4rgp.png

Board in the USA

Dino requires us to define our board, tell it that we’ll be inputing and outputing data, and tell it that we’re using a serial connection (since my Ardunio is hooked up via USB). Next, we’ll be defining our LED variable, which should be easy since it was in the previous sketch. Note: Dino also offers a good directory full of example code for setting up any given component. 

Setting up dino

Blog post image: tumblr_inline_moc4c9AKvq1qz4rgp.png

You’ve Got Mail

In the next few lines of code we: call on Gmail to let us access our inbox, check if our inbox is empty, set the led variable to a resting state, and define a variable equal to the number of unread emails. Note: In the code listed below, you’d actually pass in your real email address and password.

Blog post image: tumblr_inline_moc4g5cWFH1qz4rgp.png

Ruby Slippers

Now that we have access to our inbox, we can finish up this sketch by writing an if statement to blink our led when our inbox is not at zero.

Blog post image: tumblr_inline_moc4h7d1A31qz4rgp.png

Next Steps

I’m hoping to take this further by:

  • Writting in a delay and loop so that the sketch runs every 5-10 min

  • Adding an rgbLED so that I can light up a red LED when the inbox is full and a green LED when the inbox is empty.