Skip to content

Instantly share code, notes, and snippets.

@dstaley
Created December 10, 2012 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstaley/4252871 to your computer and use it in GitHub Desktop.
Save dstaley/4252871 to your computer and use it in GitHub Desktop.
Save Money with IronWorker and Dwolla

Save Money with IronWorker and Dwolla

This is a simple script that asks you for a savings goal and a deadline. It then calculates how much money you need to save per day to reach your goal, and then schedules withdrawals from one of your Dwolla funding sources. It aims to be a "set and forget" way to save money.

Requirements

The script requires that you have Ruby and the following gems installed:

  • iron_worker_ng
  • typhoeus
  • chronic
  • json

You'll also need to acquire a Dwolla API token for your account. You can do that here.

Usage

Simply download the files attached to this gist, change the values in iron.json to reflect your account details, and then run ruby save_money.rb from where you downloaded the files. Output is as follows:

$ ruby save_money.rb
How much would you like to save?
329
When is your deadline?
Jan 1
You need to save $14.95 a day in order to meet your goal of $329.0 by Tuesday January 01.
When would you like the first withdrawal to be made? Subsequent withdrawals will be made every 24 hours for the following 22 days.
in thirty seconds
What is your Dwolla API token?
[REDACTED]
What is your Dwolla PIN?
[REDACTED]
You have the following funding sources:
1. Chase - Checking
Enter the number for the funding source you'd like to use: 
1
Great! You're all set. You should have $329.0 in your Dwolla account a few days after Tuesday January 01. (It sometimes takes a few business days for all your deposits to clear.)

Help

If you need help, feel free to tweet me @dstaley.

require 'typhoeus'
require 'json'
require 'cgi'
url = "https://www.dwolla.com/oauth/rest/fundingsources/#{params['fundingsource']}/deposit?oauth_token=#{params['token']}"
response = Typhoeus::Request.post(url, :body => {'pin' => params['pin'], 'amount' => params['amount']}.to_json, :headers => {'Content-Type' => "application/json"})
puts response.body
runtime "ruby"
exec "dwolla.rb"
gem 'chronic'
{
"project_id": "YOUR IRON.IO PROJECT ID",
"token": "YOUR IRON.IO API TOKEN"
}
require 'chronic' # Natural language date parsing
require 'iron_worker_ng' # Upload our worker to IronWorker
require 'typhoeus' # Make HTTP requests to the Dwolla API
require 'json'
require 'cgi'
# Get user's savings goal
puts "How much would you like to save?\n"
goal = gets.chomp.to_f
# Get user's deadline, calculate how may days until then, and then calculate necessary daily savings
puts "When is your deadline?"
goal_date = Chronic.parse(gets.chomp)
timespan = goal_date - Time.now
days = (timespan/86400).round(0)
daily_savings = (goal/days).to_f.round(2)
# Get user's first withdrawal time
puts "You need to save $"+daily_savings.to_s+" a day in order to meet your goal of $"+goal.to_s+" by "+goal_date.strftime('%A %B %d')+".\n"
puts "When would you like the first withdrawal to be made? Subsequent withdrawals will be made every 24 hours for the following "+days.to_s+" days."
first_withdrawal = Chronic.parse(gets.chomp)
# Get user's Dwolla authentication credentials
# You can get an API token from here: http://ddevsite.herokuapp.com/dev/token
puts "What is your Dwolla API token?"
token = gets.chomp
token = CGI::escape(token)
puts "What is your Dwolla PIN?"
pin = gets.chomp
# Access user's available funding sources from the Dwolla API
data = Typhoeus.get("https://www.dwolla.com/oauth/rest/fundingsources?oauth_token=#{token}", followlocation: true)
response = JSON.parse(data.response_body)
if (response['Success']==true)
puts "You have the following funding sources:"
i = 1
response['Response'].each do |source|
puts i.to_s+". "+source['Name']
i += 1
end
puts "Enter the number for the funding source you'd like to use: "
index = gets.chomp
index = index.to_i - 1
source = response['Response'][index]['Id']
else
puts response
end
# Create a payload with all the information our worker needs to make withdrawals from user's funding source
payload = {:amount => daily_savings, :fundingsource => source, :pin => pin.to_i, :token => token}
client = IronWorkerNG::Client.new
schedule = client.schedules.create('dwolla', payload, {:start_at => first_withdrawal, :run_every => 86400, :run_times => days}) # Schedule worker to run every 24 hours for set amount of days
puts "Great! You're all set. You should have $"+goal.to_s+" in your Dwolla account a few days after "+goal_date.strftime('%A %B %d')+". (It sometimes takes a few business days for all your deposits to clear.)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment