X-Git-Url: https://git.r.bdr.sh/rbdr/grita/blobdiff_plain/3e43ab35988daae6dc69e6682aad4285f693ee8e..787621c343b12bbbb0922cc1faf4cdcaa5f4c245:/grita.rb diff --git a/grita.rb b/grita.rb index 17ed55e..406dde7 100644 --- a/grita.rb +++ b/grita.rb @@ -1,7 +1,12 @@ require 'sinatra' require 'haml' require 'less' -require 'mongo' +require 'dm-core' +require 'dm-validations' +require 'dm-timestamps' +require 'dm-migrations' +require 'rqr' +require 'rdiscount' #config set :haml, :format => :html5 @@ -11,10 +16,56 @@ get '/' do haml :index end +post '/' do + @grito = Grito.new(:title => params[:title], :text => params[:text]) + @grito.save + 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://grita.heroku.com/'+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 - "Estos no son los droides que estás buscando." +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 +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 \ No newline at end of file