]> git.r.bdr.sh - rbdr/grita/blame_incremental - grita.rb
Better error reporting.
[rbdr/grita] / grita.rb
... / ...
CommitLineData
1require 'sinatra'
2require 'haml'
3require 'less'
4require 'dm-core'
5require 'dm-validations'
6require 'dm-timestamps'
7require 'dm-migrations'
8require 'rqr'
9require 'rdiscount'
10
11#config
12set :haml, :format => :html5
13
14#app
15get '/' do
16 haml :index
17end
18
19post '/' do
20 @grito = Grito.new(:title => params[:title], :text => params[:text])
21 @grito.save
22 haml :index
23end
24
25get '/qr/:grito.png' do
26 tmpfile = Tempfile.new('tmp')
27 tmpfile.close
28 rpr_filepath = tmpfile.path + '.png'
29
30 RQR::QRCode.create do |qr|
31 qr.save('http://#{request.host}/'+params[:grito], rpr_filepath)
32 end
33
34 buffer = File.open(rpr_filepath).read
35
36 content_type 'image/png'
37 buffer
38end
39
40get '/style.css' do
41 less :style
42end
43
44get '/:grito' do
45 @grito = Grito.first(:id => params[:grito].to_i(36))
46 raise "Invalid Post" if @grito.nil?
47
48 @markdown = RDiscount.new(@grito.text)
49
50 haml :grito, :layout => :layoutmini
51end
52
53error do
54 haml :error
55end
56
57DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/grita')
58class Grito
59 include DataMapper::Resource
60 property :id, Serial
61 property :title, String
62 property :text, Text
63 property :created_at, DateTime
64
65 validates_presence_of :title, :message => "· Debes incluír un título."
66 validates_presence_of :text, :message => "· ¡No escribiste contenido!"
67
68 def gethash()
69 self.id.to_s(36)
70 end
71end