Skip to content

Commit

Permalink
Merge pull request #40 from ynab/update_transactions_example
Browse files Browse the repository at this point in the history
Update transactions example
  • Loading branch information
bradymholt authored Jan 22, 2019
2 parents ce9e413 + 288965a commit 2da8a56
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 139 deletions.
29 changes: 13 additions & 16 deletions examples/budget-list.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
require 'ynab'

def print_budget_list
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

begin
puts 'Fetching budgets...'
begin
budget_response = ynab.budgets.get_budgets
budgets = budget_response.data.budgets

puts '==========='
puts 'BUDGET LIST'
puts '==========='
budget_response = ynab.budgets.get_budgets
budgets = budget_response.data.budgets

budgets.each do |budget|
puts "[id: #{budget.id}, name: #{budget.name}]"
end
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
puts '==========='
puts 'BUDGET LIST'
puts '==========='

budgets.each do |budget|
puts "[id: #{budget.id}, name: #{budget.name}]"
end
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end

print_budget_list
48 changes: 23 additions & 25 deletions examples/budget-month.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
require 'ynab'
require 'date'

def print_budget_month
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']
first_day_of_month_iso = Date.new(Date.today.year, Date.today.month, 1).iso8601
budget_id = ENV['YNAB_BUDGET_ID']

begin
puts 'Fetching month...'

begin
puts 'Fetching month...'
current_date = Date.today
first_day_of_month = Date.new(current_date.year, current_date.month, 1)
month_response = ynab.months.get_budget_month(budget_id, first_day_of_month_iso)
budget_month = month_response.data.month
first_day_of_month_iso = Date.new(Date.today.year, Date.today.month, 1).iso8601
current_date = Date.today
first_day_of_month = Date.new(current_date.year, current_date.month, 1)
month_response = ynab.months.get_budget_month(budget_id, first_day_of_month_iso)
budget_month = month_response.data.month

puts "============"
puts "BUDGET MONTH"
puts "============"
puts " Month: #{budget_month.month}"
puts " Note: #{budget_month.note}"
puts "To Be Budgeted: #{budget_month.to_be_budgeted}"
puts " Age of Money: #{budget_month.age_of_money}"
puts " Categories: "
puts "============"
puts "BUDGET MONTH"
puts "============"
puts " Month: #{budget_month.month}"
puts " Note: #{budget_month.note}"
puts "To Be Budgeted: #{budget_month.to_be_budgeted}"
puts " Age of Money: #{budget_month.age_of_money}"
puts " Categories: "

budget_month.categories.each do |category|
puts " #{category.name} - $#{category.balance}"
end
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
budget_month.categories.each do |category|
puts " #{category.name} - $#{category.balance}"
end
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end

print_budget_month
37 changes: 17 additions & 20 deletions examples/category-balance.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
require 'ynab'

def print_category_info
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'
budget_id = ENV['YNAB_BUDGET_ID']
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'

begin
puts 'Fetching category...'
category_response = ynab.categories.get_category_by_id(budget_id, category_id)
category = category_response.data.category
begin
puts 'Fetching category...'

puts "==========="
puts "CATEGORY"
puts "==========="
puts " Name: #{category.name}"
puts " Budgeted: #{category.budgeted}"
puts " Balance: #{category.balance}"
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
end
category_response = ynab.categories.get_category_by_id(budget_id, category_id)
category = category_response.data.category

print_category_info
puts "==========="
puts "CATEGORY"
puts "==========="
puts " Name: #{category.name}"
puts " Budgeted: #{category.budgeted}"
puts " Balance: #{category.balance}"
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
74 changes: 36 additions & 38 deletions examples/create-multiple-transactions.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
require 'date'
require 'ynab'

def create_multiple_transactions
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']
account_id = 'f6fe07cb-c895-not-real-acfac0b7f026'
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'
budget_id = ENV['YNAB_BUDGET_ID']
account_id = 'f6fe07cb-c895-not-real-acfac0b7f026'
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'

begin
ynab.transactions.create_transaction(budget_id, {
transactions: [
{
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'A Test Payee',
memo: 'I was created through the API',
cleared: 'Cleared',
approved: true,
flag_color: 'Blue',
amount: 20000
},
{
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'Another Test Payee',
memo: 'I was also created through the API',
cleared: 'Uncleared',
approved: false,
flag_color: 'Red',
amount: 39453
}
]
})
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
end
data = {
transactions: [
{
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'A Test Payee',
memo: 'I was created through the API',
cleared: 'Cleared',
approved: true,
flag_color: 'Blue',
amount: 20000
},
{
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'Another Test Payee',
memo: 'I was also created through the API',
cleared: 'Uncleared',
approved: false,
flag_color: 'Red',
amount: 39453
}
]
}

create_multiple_transactions
begin
ynab.transactions.create_transaction(budget_id, data)
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
47 changes: 23 additions & 24 deletions examples/create-transaction.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
require 'date'
require 'ynab'

def create_transaction
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']
account_id = 'f6fe07cb-c895-not-real-acfac0b7f026'
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'
budget_id = ENV['YNAB_BUDGET_ID']
account_id = 'f6fe07cb-c895-not-real-acfac0b7f026'
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'

begin
ynab.transactions.create_transaction(budget_id, {
transaction: {
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'A Test Payee',
memo: 'I was created through the API',
cleared: 'Cleared',
approved: true,
flag_color: 'Blue',
amount: 20000
}
})
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
data = {
transaction: {
account_id: account_id,
category_id: category_id,
date: Date.today,
payee_name: 'A Test Payee',
memo: 'I was created through the API',
cleared: 'Cleared',
approved: true,
flag_color: 'Blue',
amount: 20000
}
}

begin
ynab.transactions.create_transaction(budget_id, data)
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end

create_transaction
34 changes: 18 additions & 16 deletions examples/update-category-budgeted.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
require 'date'
require 'ynab'

def update_category_budgeted
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)
access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'
month = '2018-09-01'
month_category = { budgeted: 20382 }
budget_id = ENV['YNAB_BUDGET_ID']
category_id = 'a191ac84-de09-not-real-6c5ed8cfdabe'
month = '2019-01-01'

begin
# Update budgeted amount to 203.82 in 2018-09-01 for category_id
response = ynab.categories.update_month_category(budget_id, '2018-09-01', category_id, month_category: month_category)
month_category_response = response.data.category
puts "[budgeted: #{month_category_response.budgeted}, balance: #{month_category_response.balance}]"
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end
data = {
category: {
budgeted: 20382
}
}

begin
# Update budgeted amount to 203.82 in 2018-09-01 for category_id
response = ynab.categories.update_month_category(budget_id, month, category_id, data)
month_category_response = response.data.category
puts "[budgeted: #{month_category_response.budgeted}, balance: #{month_category_response.balance}]"
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end

update_category_budgeted
20 changes: 20 additions & 0 deletions examples/update-multiple-transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'ynab'

access_token = ENV['YNAB_ACCESS_TOKEN']
ynab = YNAB::API.new(access_token)

budget_id = ENV['YNAB_BUDGET_ID']

begin
# Fetch all unapproved transactions and update them all as approved
unapproved_transaction_data = ynab.transactions.get_transactions(budget_id, { type: 'unapproved' }).data

data = unapproved_transaction_data.clone
data.transactions.each do |t|
t.approved = true
end

ynab.transactions.update_transactions(budget_id, update_data)
rescue YNAB::ApiError => e
puts "ERROR: id=#{e.id}; name=#{e.name}; detail: #{e.detail}"
end

0 comments on commit 2da8a56

Please sign in to comment.