-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_note.rb
134 lines (114 loc) · 2.74 KB
/
web_note.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'rubygems'
require 'sinatra'
require "sinatra/reloader"
require 'web_note_mongo'
require 'haml'
require 'uri'
MONGO_ID_REGEX = /\/\h{24}/
@@TAGS = ['title', 'text', 'tags', 'otp']
before do
WebNoteMongo.connect()
end
def method_string(regexp)
Regexp.new(MONGO_ID_REGEX.to_s + regexp.to_s)
end
get '/' do
haml :edit
end
post '/' do
if not (params['text'].empty? and params['title'].empty?)
save_note
else
redirect to "#{params['tags'].delete(' ','').tr(',','/')}"
end
end
get method_string(/\/edit/) do
@note = WebNoteMongo.find_by_id(get_id)
@note['tags'] = @note['tags'].join(',')
haml :edit
end
post method_string(/\/edit/) do
params['_id'] = get_id
save_note
end
get method_string(/\/delete/) do
haml :delete
end
post method_string(/\/delete/) do
if WebNoteMongo.check_pin(params['otp'])
WebNoteMongo.delete(get_id)
redirect to '/'
else
redirect to "#{get_id}/delete"
end
end
get MONGO_ID_REGEX do
@note = WebNoteMongo.find_by_id(get_id)
if @note
haml :show
else
redirect to "/"
end
end
get '/:tag' do
#show notes tagged with :tag
render_note_list([ params[:tag] ])
end
get /(\/[^\/]+){2,}/ do
path = URI.unescape(request.path_info)
render_note_list( path.split('/').drop(1) )
end
def render_note_list(tags)
@tagged_noteset = WebNoteMongo.find_by_tag(tags)
case @tagged_noteset.count
when 0 then halt 404, "no notes found with tag(s):#{tags.join(',')}"
when 1 then redirect to "/#{@tagged_noteset.first['_id']}"
else
haml :tag
end
end
def save_note()
if WebNoteMongo.check_pin(params['otp'])
params.delete('otp')
#split tags on commas; then split any tags with spaces and add individual words as tags as well; remove duplicates
params['tags'] = params['tags'].split(',').collect{|t| t.strip}.collect{|t| t=~/\s/ ? [t,t.split(/\s/)].flatten : t}.flatten.uniq
redirect to("/#{WebNoteMongo.save(params).to_s}")#this may be too clever: calling 'save' from inside a string interpolation (programming-by-side-effect FTL)
else
params['otp'] = "INVALID PIN: #{params['otp']}"
@note = params
haml :edit
end
end
def get_id
request.path_info.split('/').last
end
helpers do
def note_link_text(note)
note["title"] || note["text"][0...80]
end
def note_output(note)
text = note['text']
language = note['language']
begin
case language
when nil
text
when 'ruby'
eval text
when 'javascript'
"<script>#{text}</script>"
else
input = <<RUBY
#{language} <<'BASH'
#{text}
BASH
RUBY
%x(#{input})
end
rescue Exception => e
error_message = "#{e.message}\n#{e.backtrace.join("\n")}"
STDERR.puts error_message
"#{text}\n#{error_message}"
end
end
end