]> git.r.bdr.sh - rbdr/grita/blame - grita.rb
Adds Variable domain to view too.
[rbdr/grita] / grita.rb
CommitLineData
3e43ab35
BB
1require 'sinatra'
2require 'haml'
3require 'less'
5573b3c2 4require 'dm-core'
0e0a67f4 5require 'dm-validations'
5573b3c2 6require 'dm-timestamps'
0e0a67f4
BB
7require 'dm-migrations'
8require 'rqr'
b7be4f63 9require 'rdiscount'
3e43ab35
BB
10
11#config
12set :haml, :format => :html5
13
14#app
15get '/' do
16 haml :index
17end
18
5573b3c2 19post '/' do
0e0a67f4
BB
20 @grito = Grito.new(:title => params[:title], :text => params[:text])
21 @grito.save
5573b3c2
BB
22 haml :index
23end
24
0e0a67f4
BB
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|
c194753d 31 qr.save('http://#{request.host}/'+params[:grito], rpr_filepath)
0e0a67f4
BB
32 end
33
34 buffer = File.open(rpr_filepath).read
35
36 content_type 'image/png'
37 buffer
38end
39
3e43ab35
BB
40get '/style.css' do
41 less :style
42end
43
0e0a67f4
BB
44get '/:grito' do
45 @grito = Grito.first(:id => params[:grito].to_i(36))
46 raise "Invalid Post" if @grito.nil?
b7be4f63
BB
47
48 @markdown = RDiscount.new(@grito.text)
49
396b06b8 50 haml :grito, :layout => :layoutmini
0e0a67f4
BB
51end
52
53error do
5573b3c2
BB
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
0e0a67f4
BB
61 property :title, String
62 property :text, Text
5573b3c2
BB
63 property :created_at, DateTime
64
0e0a67f4
BB
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()
5573b3c2
BB
69 self.id.to_s(36)
70 end
3e43ab35 71end