-
Notifications
You must be signed in to change notification settings - Fork 0
/
copypasty_app.rb
89 lines (71 loc) · 1.94 KB
/
copypasty_app.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
require "rubygems"
require "sinatra/base"
require "data_mapper"
require "dm-migrations"
require "configure_app"
require "lib/pasty"
class ArnkCloudy < Sinatra::Base
get '/' do
redirect "/codes/"
end
get "/codes" do
redirect "/codes/"
end
get '/codes/' do
@kinds = %w{Plain Bash Java JScript Scala Ruby Diff Css Python CSharp Xml Php Cpp}
erb :code_new
end
post '/codes/' do
unless params[:code].empty? then
redirect "/codes/" + Pasty.create(
:code => params[:code],
:kind => params[:language]
).hashkey
else
halt 401, 'Correctly fill the form'
end
end
get '/codes/stats/count' do
Pasty.count.to_s
end
# Try to get code by id ...
get '/codes/:id' do
pass unless params[:id] =~ /^\d+$/
@pasty = Pasty.get(params[:id])
@encoder = HTMLEntities.new
halt 404 if @pasty.nil?
#"<pre>#{@pasty.code}</pre>"
erb :code_view
end
# Try to get code by hashkey
get '/codes/:key' do
pass unless params[:key] =~ /^[a-zA-Z]+$/
@pasty = Pasty.last(:hashkey => params[:key])
@encoder = HTMLEntities.new
halt 404 if @pasty.nil?
#"<pre>#{@pasty.code}</pre>"
erb :code_view
end
get '/codes/:key/id' do
pass unless params[:key] =~ /^[a-zA-Z]+$/
pasty = Pasty.last(:hashkey => params[:key])
pasty.id.to_s unless pasty.nil?
end
get '/codes/:id/hashkey' do
pass unless params[:id] =~ /^\d+$/
pasty = Pasty.get(params[:id])
pasty.hashkey unless pasty.nil?
end
get '/codes/:key/change/:new' do
pass unless params[:new] =~ /^[a-zA-Z]+$/
pasty = Pasty.last(:hashkey => params[:key]) if params[:key] =~ /^[a-zA-Z]+$/
pasty = Pasty.get(params[:key]) if params[:key] =~ /^\d+$/
pass if pasty.nil?
halt 401 unless pasty.update(:hashkey => params[:new])
redirect "/codes/#{pasty.hashkey}"
end
get '/codes/*' do
halt 401, 'Wrong key format ...'
end
run! if "arnk_cloudy_app.rb" == $0
end