]>
Commit | Line | Data |
---|---|---|
1 | require 'sinatra' | |
2 | require 'haml' | |
3 | require 'less' | |
4 | require 'dm-core' | |
5 | require 'dm-validations' | |
6 | require 'dm-timestamps' | |
7 | require 'dm-migrations' | |
8 | require 'rqr' | |
9 | require 'rdiscount' | |
10 | ||
11 | #config | |
12 | set :haml, :format => :html5 | |
13 | ||
14 | #app | |
15 | get '/' do | |
16 | haml :index | |
17 | end | |
18 | ||
19 | post '/' do | |
20 | @grito = Grito.new(:title => params[:title], :text => params[:text]) | |
21 | @grito.save | |
22 | haml :index | |
23 | end | |
24 | ||
25 | get '/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://grita.heroku.com/'+params[:grito], rpr_filepath) | |
32 | end | |
33 | ||
34 | buffer = File.open(rpr_filepath).read | |
35 | ||
36 | content_type 'image/png' | |
37 | buffer | |
38 | end | |
39 | ||
40 | get '/style.css' do | |
41 | less :style | |
42 | end | |
43 | ||
44 | get '/: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 | |
51 | end | |
52 | ||
53 | error do | |
54 | haml :error | |
55 | end | |
56 | ||
57 | DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/grita') | |
58 | class 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 | |
71 | end |