forked from konklone/ohnomymoney
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ohnomymoney.rb
executable file
·87 lines (71 loc) · 2.1 KB
/
ohnomymoney.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
# Dreamhost has some special...I don't want to talk about it
begin
require 'dreamhost'
rescue LoadError
end
require 'rubygems'
require 'sinatra'
require 'environment'
require 'erb'
require 'charting'
# For now, have the root URL act as if we visited the userpage of the first user
before do
if request.path_info == '/'
request.path_info = "/#{User.first.handle}"
elsif request.path_info == '/worth.xml'
request.path_info = "/#{User.first.handle}/worth.xml"
end
end
get "/:handle/?" do
halt 404 unless user = User.find_by_handle(params[:handle])
erb :index, :locals => {
:user => user,
:worth => user.accounts.worth.first,
:assets => user.accounts.assets.all,
:debts => user.accounts.debts.all
}
end
get "/:handle/account/:id/?" do
halt 404 unless user = User.find_by_handle(params[:handle])
halt 404 unless account = user.accounts.find(params[:id])
erb :account, :locals => {
:user => user,
:account => account,
:oldest => account.balances.first(:order => "created_at ASC"),
:newest => account.balances.first(:order => "created_at DESC"),
:balances => account.balances.all(:limit => 180, :order => "created_at DESC").reverse
}
end
get "/:handle/worth.xml" do
halt 404 unless user = User.find_by_handle(params[:handle])
halt 404 unless account = user.accounts.worth.first
response['Content-Type'] = 'application/rss+xml'
erb :worth, :locals => {
:user => user,
:account => account,
:balances => account.balances.all(:limit => 10, :order => "created_at DESC")
}
end
helpers do
def date(date)
date.strftime("%B ") + date.strftime("%d").gsub(/^0/, "")
end
def direction(balance)
balance >= 0 ? 'positive' : 'negative'
end
def format_amount(amount)
number = amount.to_f / 100
parts = ("%01.2f" % number).split('.')
answer = '$' + number_with_delimiter(parts[0]) + '.' + parts[1].to_s
if number > 0
answer = '+' + answer
elsif number < 0
answer = '-' + answer
end
answer
end
def midnight_stamp(date)
date.strftime "%a, %d %b %Y 00:00:00 EST"
end
end