summaryrefslogtreecommitdiff
path: root/grita.rb
blob: 180fe194dab08493c50658acfe1b67f6cd313859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'sinatra'
require 'haml'
require 'less'
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'dm-migrations'
require 'rqr'
require 'rdiscount'

#config
set :haml, :format => :html5

#app
get '/' do
  haml :index
end

post '/' do
  @grito = Grito.create(:title => params[:title], :text => params[:text])
  haml :index
end

get '/qr/:grito.png' do
   tmpfile = Tempfile.new('tmp')    
   tmpfile.close
   rpr_filepath = tmpfile.path + '.png'

   RQR::QRCode.create do |qr|
     qr.save('http://#{request.host}/'+params[:grito], rpr_filepath)
   end

   buffer = File.open(rpr_filepath).read

   content_type 'image/png'
   buffer
end

get '/style.css' do
  less :style
end

get '/:grito' do
    @grito = Grito.first(:id => params[:grito].to_i(36))
    raise "Invalid Post" if @grito.nil?
    
    @markdown = RDiscount.new(@grito.text)
    
    haml :grito, :layout => :layoutmini
end

error do
  haml :error
end

DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/grita')
class Grito
  include DataMapper::Resource
  property :id,           Serial
  property :title,        String
  property :text,         Text
  property :created_at,   DateTime
  
  validates_presence_of :title, :message => "· Debes incluír un título."
  validates_presence_of :text, :message => "· ¡No escribiste contenido!"
  
  def gethash() 
    self.id.to_s(36)
  end
end