-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinsnip.rb
executable file
·76 lines (63 loc) · 1.54 KB
/
sinsnip.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
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'sequel'
require 'haml'
require 'sass'
require 'coderay'
configure do
Sequel.connect('sqlite://snippets.db')
PREVIEW_LINES = 2
end
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'snippet'
require 'helpers'
before do
@most_recent = Snippet.most_recent
@most_viewed = Snippet.most_viewed
@tags = Snippet.map(:tags).join(' ').split.uniq.sort
end
get '/' do
@snippets = Snippet.all
haml :main
end
get '/stylesheets/layout.css' do
content_type 'text/css', :charset => 'utf-8'
sass :layout
end
get '/s/:snip' do
@snippet = Snippet[params[:snip]]
halt [ 404, "Page not found" ] unless @snippet
@snippet.view!
haml :show
end
get '/t/:tag' do
@tag = params[:tag]
@snippets = Snippet.filter(:tags.like("%#{@tag}%"))
haml :tagged
end
get '/new' do
haml :edit, :locals => { :snippet => Snippet.new, :url => '/new' }
end
post '/new' do
snippet = Snippet.new( :title => params[:title], :tags => params[:tags], :body => params[:body], :language => params[:language] )
snippet.save
redirect "/s/#{snippet.id}"
end
get '/e/:id' do
haml :edit, :locals => { :snippet => Snippet[params[:id]], :url => "/e/#{params[:id]}" }
end
post '/e/:id' do
snippet = Snippet[params[:id]]
snippet.title = params[:title]
snippet.tags = params[:tags]
snippet.body = params[:body]
snippet.language = params[:language]
snippet.updated_at = Time.now
snippet.save
redirect "/s/#{snippet.id}"
end
delete '/d/:id' do
Snippet[params[:id]].destroy
redirect '/'
end