]>
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 | ||
10 | #config | |
11 | set :haml, :format => :html5 | |
12 | ||
13 | #app | |
14 | get '/' do | |
15 | haml :index | |
16 | end | |
17 | ||
18 | post '/' do | |
19 | @grito = Grito.new(:title => params[:title], :text => params[:text]) | |
20 | @grito.save | |
21 | haml :index | |
22 | end | |
23 | ||
24 | get '/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://grita.heroku.com/'+params[:grito], rpr_filepath) | |
31 | end | |
32 | ||
33 | buffer = File.open(rpr_filepath).read | |
34 | ||
35 | content_type 'image/png' | |
36 | buffer | |
37 | end | |
38 | ||
39 | get '/style.css' do | |
40 | less :style | |
41 | end | |
42 | ||
43 | get '/:grito' do | |
44 | @grito = Grito.first(:id => params[:grito].to_i(36)) | |
45 | raise "Invalid Post" if @grito.nil? | |
46 | haml :grito | |
47 | end | |
48 | ||
49 | error do | |
50 | haml :error | |
51 | end | |
52 | ||
53 | DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/grita') | |
54 | class Grito | |
55 | include DataMapper::Resource | |
56 | property :id, Serial | |
57 | property :title, String | |
58 | property :text, Text | |
59 | property :created_at, DateTime | |
60 | ||
61 | validates_presence_of :title, :message => "· Debes incluír un título." | |
62 | validates_presence_of :text, :message => "· ¡No escribiste contenido!" | |
63 | ||
64 | def gethash() | |
65 | self.id.to_s(36) | |
66 | end | |
67 | end |