<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Beau Haldane Jorgensen on Medium]]></title>
        <description><![CDATA[Stories by Beau Haldane Jorgensen on Medium]]></description>
        <link>https://medium.com/@beauhaldane?source=rss-5e2b2c7ceb3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ExXHIuqEOIr4jCPP.jpg</url>
            <title>Stories by Beau Haldane Jorgensen on Medium</title>
            <link>https://medium.com/@beauhaldane?source=rss-5e2b2c7ceb3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:27:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@beauhaldane/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Gmail to Todoist]]></title>
            <link>https://medium.com/swlh/gmail-to-todoist-a69cb3a2ff65?source=rss-5e2b2c7ceb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a69cb3a2ff65</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[gmail]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[todoist]]></category>
            <dc:creator><![CDATA[Beau Haldane Jorgensen]]></dc:creator>
            <pubDate>Sun, 05 Apr 2020 00:55:40 GMT</pubDate>
            <atom:updated>2020-04-07T04:11:33.553Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Ruby to Search my Gmail Inbox and Create Tasks in Todoist</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*bOQYF3MDooxvDjEz4iIYPA.gif" /></figure><h3>The Problem</h3><p>I run a small <a href="http://www.haldanedrums.com/">drum making business</a> which has some items for sale on a site which I created with Squarespace. When a customer buys something from my site, I receive an email notifying me of the sale along with what the customer ordered, their shipping address, etc. Ordinarily, I’d copy the order information and the customer’s shipping address and add them both to a Todoist task which I’ll action at the next possible opportunity. This works great, except for when I forget to add the task to Todoist and accidentally leave the customer waiting to receive their item (which has only happened once, but that’s one too many times!)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*kcIyhNpFvneEAIKi" /></figure><h3>The Solution</h3><p>I decided to try to use Ruby to solve my problem — I’ve just started learning to program at <a href="https://coderacademy.edu.au/">Coder Academy</a> here in Brisbane, and I want to put my newly learned skills into practice with a real use case. I’m going to break things down on a pretty granular level here — if you just want to see my final code, jump to the end of the article.</p><p>For this project I’ll need to access both my Gmail and Todoist accounts using Ruby. A bit of research and testing led me to two Ruby gems, <a href="https://github.com/gmailgem/gmail">Gmail for Ruby</a>, and <a href="https://github.com/h6y3/todoist-ruby">Todoist Ruby</a>. The game plan is to write a program to do the following:</p><ul><li>Log in to my business gmail account using <a href="https://github.com/gmailgem/gmail">Gmail for Ruby</a></li><li>Search through my emails for any unread emails which tell me I’ve sold something</li><li>Extract necessary information (what the customer ordered and their address)</li><li>Mark the email as read so that it doesn’t trigger the program next time it searches</li><li>Add the order information to a new task in Todoist using <a href="https://github.com/h6y3/todoist-ruby">Ruby Todoist</a></li><li>Set the program up to run on a regular schedule (once or twice a day should be fine for my use)</li></ul><h3>The Implementation</h3><p><strong>Logging into Gmail using Gmail for Ruby</strong></p><p>First things first, I need to log into my business’ Gmail account. Gmail for Ruby gives you the option to use either XOAuth, or to use your email address and password to log in directly. I decided I’d use my email and password for now, and set up XOAuth later.</p><p>Following Gmail for Ruby’s github, I implement the following code to access my account and check whether it logged in successfully:</p><pre>gmail = Gmail.connect(‘youremail@yourhost.com’, ‘yourpassword’)<br>puts gmail.logged_in?<br>gmail.logout</pre><p>At first Google wouldn’t allow me to access my Gmail account using my ‘less secure app’, but after enabling less secure apps to log in, my program successfully logs in to my Gmail account — great!</p><h4><strong>Searching my emails for New Orders</strong></h4><p>Every time I sell something, I get an email from <a href="https://medium.com/no-reply@squarespace.com">no-reply@squarespace.com</a> and the email subject includes ‘A New Order has Arrived’. I use these two parameters as my search conditions and add that the search must only return unread emails:</p><pre>gmail.inbox.find(:unread, :from =&gt; “no-reply@squarespace.com”,:subject =&gt; (‘a new order has arrived’))</pre><h4><strong>Extract the order information</strong></h4><p>Now that I have my emails, I need to write some code to extract information from the body of any unread emails. When I receive a new order email, it always looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*smnSlkSHBamusWXf" /></figure><p>The information that I need to extract is under the ‘SHIPPING TO:’ and ‘ITEM’ headings. These are always present in these emails, so I’m going to use them to find and return the information I need. I start by printing the body of the entire email to the terminal, so I can see what I’m working with:</p><pre>gmail.inbox.find(:unread, :from =&gt; “no-reply@squarespace.com”,:subject =&gt; (‘a new order has arrived’)).each do |email|<br>  body = email.text_part ? email.text_part.body.decoded : nil<br>  puts body</pre><p>This returns a massive string of text, part of which contains our order information. Next, I define two new variables which each take the body string and split it into an array before using .drop to remove all elements up to the point which contains the information I need, and I print them to the terminal for inspection:</p><pre>gmail.inbox.find(:unread, :from =&gt; “no-reply@squarespace.com”,:subject =&gt; (‘a new order has arrived’)).each do |email|<br>  body = email.text_part ? email.text_part.body.decoded : nil<br>  shipping_start = body.split.drop(body.split.index(‘SHIPPING’)) <br>  order_start = shipping_start.drop(shipping_start.index(‘SUBTOTAL’) + 1) <br>  puts order_start.uniq<br>  puts shipping_start.uniq</pre><p>Here’s what each array returns in the terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*s0JDEeMLXT_TRArg" /></figure><p>Now I define two new variables which each take the arrays above and remove everything from the end of these arrays that isn’t the information I need, then I join each array back together as one string, and mark the email as read:</p><pre>shipping_info = shipping_start.take(shipping_start.index(‘Australia’)).uniq.join(‘ ‘) <br>order_info = order_start.take(order_start.index(‘Item’)).uniq.join(‘ ‘) <br>email.read! <br>puts “Order info: #{order_info}”<br>puts “Shipping info: #{shipping_info}”</pre><p>I print each string to the terminal and check that they contain the info I need:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/0*pPRuENqod3Yqi1Be" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*bNoHy_CvKZareDwKMY41Mw.gif" /></figure><p>Success! We now have our order_info and shipping_info variables which contain our information as strings. Note that to remove all information I don’t need from the arrays, I use words that are going to be constant in every email. For example, for shipping_info I use ‘SHIPPING’ and ‘Australia’, since the heading ‘SHIPPING’ is in every email, and the word ‘Australia’ is too as I only offer shipping in Australia. I do the same with order_info, and check that the code returns the right results when tested on a number of emails in my inbox.</p><h4>Add the order information to a new task in Todoist</h4><p>Following Ruby Todoist’s documentation, I access my account using my Todoist API token, and add a new project with the order_info string I extracted earlier.</p><pre>@client = Todoist::Client.create_client_by_token(“your_token_here”) <br>item = @client.misc_quick.add_item(“Ship #{order_info}”)</pre><p>Next I add a note to the task I’ve just created which includes the shipping_info string extracted earlier. (This took me at least an hour of searching through the Ruby Todoist documentation and testing code to figure out!)</p><pre>@client.sync_notes.add({content: “#{shipping_info}”, item_id: item.id}) # Adds comment to the task using the shipping info scraped from Gmail<br>@client.sync</pre><p>I run the program, wait nervously, refresh my Todoist, and…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kmfbrWUYgLUfSCBF" /></figure><p>You hear that? That’s the quiet hum of automation — sounds nice doesn’t it? Now whenever I make a sale on my website, a task will automatically be populated in my Todoist inbox with all relevant information. All I have to do is schedule a time to complete the task - which I will either do daily, or when I do my GTD weekly review (more on this in another article) - and complete the task when the time is right!</p><h4><strong>Set the program up to run on a regular schedule</strong></h4><p>Initially I tried setting up my ruby file to run automatically using cron on Ubuntu 18.04 LTS in Windows, but I couldn’t get cron to play ball no matter how much I tried. I decided to copy the project onto my Raspberry Pi (which I have permanently running in the kitchen) and set up a cron job to auto-run the script a few times a day. Open the terminal on the Pi and run:</p><pre>crontab -e</pre><p>This brings up the cron editor, to which I add my cron job. For now, I have the job running once per hour, but I’ll probably lower the frequency over time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*1hP6NU9hf7tKfGe_C0CLQw.jpeg" /></figure><p>There are plenty of cron guides around, but Janel Brandon’s <a href="https://github.com/janel-developer/twitter-bot-cron">twitter-bot-cron</a> git does a great job at showing how to do get it set up and running. Once I moved the project over to the Pi, cron ran the job perfectly fine!</p><h3><strong>The Conclusion</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*jD6ltJ86-iBkmVcDGWlfuw.gif" /></figure><p>I’m quite new to using Ruby, so I found this task quite challenging. I learned a lot about using gems, reading gem documentation (or more importantly, finding info when there is barely any useful documentation to speak of), and implementing my own solutions to my own real life use cases. I learned a lot, had a good time, had a bad time, had a good time again, and now have one less problem to think about when I’m trying to go to sleep.</p><p>Here’s my final code in full:</p><pre>gmail = Gmail.connect(‘youremail@yourhost.com’, ‘yourpassword’)</pre><pre>gmail.inbox.find(:unread, :from =&gt; “no-reply@squarespace.com”,:subject =&gt; (‘a new order has arrived’)).each do |email|<br> <br>  body = email.text_part ? email.text_part.body.decoded : nil<br>  shipping_start = body.split.drop(body.split.index(‘SHIPPING’))<br>  order_start = shipping_start.drop(shipping_start.index(‘SUBTOTAL’) + 1)<br>  shipping_info = shipping_start.take(shipping_start.index(‘-’)).uniq.join(‘ ‘)<br>  order_info = order_start.take(order_start.index(‘Item’)).uniq.join(‘ ‘)<br>  email.read!<br>  <br>  @client = Todoist::Client.create_client_by_token(“your_token_here”)<br>  item = @client.misc_quick.add_item(“Ship #{order_info} p1”)<br>  @client.sync_notes.add({content: “#{shipping_info}”, item_id: item.id})<br>  @client.sync</pre><pre>end<br>gmail.logout</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a69cb3a2ff65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/gmail-to-todoist-a69cb3a2ff65">Gmail to Todoist</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>