]> git.r.bdr.sh - rbdr/grita/blobdiff - grita.rb
Fixes QR Codes.
[rbdr/grita] / grita.rb
index eaf59a715e23c03ef8b5d71d9262cf013694a353..cefcc909730834fdc0a00922947be6b9dcfdedb5 100644 (file)
--- a/grita.rb
+++ b/grita.rb
@@ -2,7 +2,11 @@ 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
@@ -13,15 +17,39 @@ get '/' do
 end
 
 post '/' do
-  @grito = Grito.first_or_create(:title => params[:title], :text => params[:text])
+  @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
 
-not_found do
+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
 
@@ -29,11 +57,14 @@ DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/g
 class Grito
   include DataMapper::Resource
   property :id,           Serial
-  property :title,        String,  :required => true
-  property :text,         Text,  :required => true
+  property :title,        String
+  property :text,         Text
   property :created_at,   DateTime
   
-  def geturl() 
+  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
\ No newline at end of file