Created
July 18, 2013 16:36
-
-
Save snelson/6030809 to your computer and use it in GitHub Desktop.
A script to convert Simple.com JSON transaction export to a CSV for importing into YNAB.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'csv' | |
class SimpleJsonExport | |
def initialize(filepath) | |
@filepath = filepath | |
end | |
def transactions | |
@transactions ||= data['transactions'].collect do |transaction| | |
SimpleTransaction.new(transaction) | |
end | |
end | |
private | |
attr_reader :filepath | |
def file | |
@file ||= File.read(filepath) | |
end | |
def data | |
@date ||= JSON.parse(file) | |
end | |
end | |
class SimpleTransaction | |
def initialize(data) | |
@data = data | |
end | |
def date | |
@date ||= Date.parse(data['times']['when_recorded_local']) | |
end | |
def payee | |
@payee ||= data['description'] | |
end | |
def category | |
@category ||= data['categories'].first['name'] | |
end | |
def memo | |
nil | |
end | |
def outflow | |
@outflow ||= type == :debit ? amount : nil | |
end | |
def inflow | |
@inflow ||= type == :credit ? amount : nil | |
end | |
private | |
def amount | |
@amount ||= "%.2f" % (data['amounts']['amount'] / 10000.00) | |
end | |
def type | |
@type ||= data['bookkeeping_type'].to_sym | |
end | |
attr_reader :data | |
end | |
class YnabCsvExport < Struct.new(:transactions) | |
def csv | |
@csv ||= CSV.generate do |csv| | |
csv << %w(Date Payee Category Memo Outflow Inflow) | |
transactions.each do |transaction| | |
date = transaction.date.strftime('%m/%d/%Y') | |
payee = transaction.payee | |
category = transaction.category | |
memo = transaction.memo | |
outflow = transaction.outflow | |
inflow = transaction.inflow | |
csv << [date, payee, category, memo, outflow, inflow] | |
end | |
end | |
end | |
end | |
filepath = 'simple_export.json' | |
simple_export = SimpleJsonExport.new(filepath) | |
transactions = simple_export.transactions | |
csv = YnabCsvExport.new(transactions).csv | |
puts csv | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment