-
Notifications
You must be signed in to change notification settings - Fork 1
/
ohnomymoney.rb
executable file
·96 lines (77 loc) · 2.24 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
88
89
90
91
92
93
94
95
96
#!/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'
# front-end specific requires
require 'erb'
# For now, have the root URL act as if we visited the userpage of the first user
before do
@user = User.first
if request.path_info !~ /^\/#{@user.handle}/
request.path_info = "/#{@user.handle}" + request.path_info
end
end
get "/:handle/?" do
halt 404 unless user = @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 # User.find_by_handle(params[:handle])
halt 404 unless account = user.accounts.find(params[:id])
erb :account, :locals => {
:user => user,
:account => account,
:balances => account.balances.all(:order => "date_of ASC")
}
end
get "/:handle/worth.xml" do
halt 404 unless user = @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 => "date_of DESC")
}, :layout => false
end
helpers do
def date(date)
date.strftime("%B ") + date.strftime("%d").gsub(/^0/, "")
end
def long_date(date)
date.strftime("%B ") + date.strftime("%d").gsub(/^0/, "") + ", #{date.strftime "%Y"}"
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
def number_with_delimiter(number)
number = number.to_i.abs
parts = number.to_s.split('.')
parts[0].gsub! /(\d)(?=(\d\d\d)+(?!\d))/, "\\1,"
parts.join '.'
end
end