]> git.r.bdr.sh - rbdr/grita/blame_incremental - grita.rb
Changes method for datamapper
[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.create(:title => params[:title], :text => params[:text])
21 haml :index
22end
23
24get '/qr/:grito.png' do
25 tmpfile = Tempfile.new('tmp')
26 tmpfile.close
27 rpr_filepath = tmpfile.path + '.png'
28
29 RQR::QRCode.create do |qr|
30 qr.save('http://#{request.host}/'+params[:grito], rpr_filepath)
31 end
32
33 buffer = File.open(rpr_filepath).read
34
35 content_type 'image/png'
36 buffer
37end
38
39get '/style.css' do
40 less :style
41end
42
43get '/:grito' do
44 @grito = Grito.first(:id => params[:grito].to_i(36))
45 raise "Invalid Post" if @grito.nil?
46
47 @markdown = RDiscount.new(@grito.text)
48
49 haml :grito, :layout => :layoutmini
50end
51
52error do
53 haml :error
54end
55
56DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/grita')
57class Grito
58 include DataMapper::Resource
59 property :id, Serial
60 property :title, String
61 property :text, Text
62 property :created_at, DateTime
63
64 validates_presence_of :title, :message => "· Debes incluír un título."
65 validates_presence_of :text, :message => "· ¡No escribiste contenido!"
66
67 def gethash()
68 self.id.to_s(36)
69 end
70end