Skip to content

Instantly share code, notes, and snippets.

@mattroyer
Last active December 10, 2015 10:29
Show Gist options
  • Save mattroyer/4421531 to your computer and use it in GitHub Desktop.
Save mattroyer/4421531 to your computer and use it in GitHub Desktop.
Interface with Outlook Through Ruby

Snacks Code

At work, we have a snacks program. There is a soda fountain and the cupboards are filled with all kinds of goodies. Everything, soda and snacks, is $0.25 cents. We have an iPad setup with a little web interface that was created in-house so that we can pay for our purchases and the money is automatically deducted from our checks. We each have our own pin to use so that we can puchase these things securely.

When we purchase something, we get an e-mail stating what we purchased and the total items. It doesn't have an amount, just a total of the number of items we've purchased.

One night, I was curious about how much I have purchased through this snack program. I have a rule in Outlook that takes the incoming snack e-mails, marks them as read, and then moves them to a folder I created called "Snacks". I have every e-mail in that folder from when this program started.

So I created the two code files (and added the .bat file to my path) so that all I had to do on my command-line is type snacks in any directory (because I'm too lazy to move to my Dev directory and run the program directly) and it will print out how much I have spent so far on snacks.

I will comment the code shortly so I can explain (to myself for later review as much as others) what I have done to create this small command-line app.

@ruby %userprofile%\Documents\Dev\Ruby\Apps\Snacks\snacks.rb
require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
mapi = outlook.GetNamespace('MAPI')
inbox = mapi.GetDefaultFolder(6)
snacks = inbox.Folders.Item('Snacks')
total = 0
snacks.Items.each do |message|
data = []
items = /(Total Items: )\d/.match(message.body).to_s
data << items.gsub!(/(Total Items: )/, '')
data.each { |number| total += number.to_i }
end
total = total * 0.25
puts "$%.2f" % total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment