moomerman / frog

Frog is a full-featured blog application written using the sinatra web framework

This URL has Read+Write access

frog / frog.rb
100755 103 lines (84 sloc) 1.893 kb
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
#!/usr/bin/env ruby
 
require 'rubygems'
require 'sinatra'
 
Dir["lib/*.rb"].each { |x| load x }
 
configure do
  set_option :sessions, true
end
 
before do
  if request.path_info =~ /admin/ and !logged_in?
    session['forward'] = request.path_info + (request.query_string.blank? ? '' : '?' + request.query_string)
    redirect '/login'
  end
  @blog = Blog.find(:first)
end
 
helpers do
  include Helpers
end
 
# Main Blog action
get '/' do
  @entries = @blog.entries
  erb :blog
end
 
# Permalink Entry action
get '/perm/:id' do
  @entry = @blog.entries.find(params[:id])
  erb :entry
end
 
get '/login' do
  erb :login
end
 
post '/login' do
  # TODO: store the hashed password on the blog model
  if params[:username] == 'admin' and params['password'] == 'admin'
    session[:user] = true
    redirect session['forward'] || '/'
  else
    redirect '/login'
  end
end
 
get '/logout' do
  session[:user] = nil
  redirect '/'
end
 
# -- Admin actions (require login)
 
get '/admin' do
  @entries = @blog.entries
  erb :admin
end
 
get '/admin/new' do
  @entry = Entry.new
  erb :new
end
 
get '/admin/update/:id' do
  @entry = @blog.entries.find(params[:id])
  erb :update
end
 
post '/admin/update/:id' do
  entry = @blog.entries.find(params[:id])
  entry.update_attributes(:title => params[:title], :url => params[:url], :text => params[:text])
  redirect "/perm/#{entry.id}"
end
 
get '/admin/destroy/:id' do
  @blog.entries.find(params[:id]).destroy
  redirect '/admin'
end
 
post '/admin/create' do
  entry = @blog.entries.create(
    :title => params[:title],
    :url => params[:url],
    :text => params[:text]
  )
  redirect "/perm/#{entry.id}"
end
 
# For use by the bookmarklet
# http://blog/admin/bookmark?url=http://somewhere.com/
get '/admin/bookmark' do
  url = params[:url]
  title = scrape_page_title(url)
  @blog.entries.create(
    :title => title,
    :url => url
  )
  redirect url
end