Skip to content

Instantly share code, notes, and snippets.

@styliii
Created October 16, 2012 02:03
Show Gist options
  • Save styliii/3896880 to your computer and use it in GitHub Desktop.
Save styliii/3896880 to your computer and use it in GitHub Desktop.
app file for sinatra example
# app.rb
require 'sinatra'
require 'json'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'])
get '/' do
@messages = Message.all
erb :messages
end
get '/reset' do
DataMapper.auto_migrate!
"Messages reset!"
end
post '/' do
# TODO: Read the message contents, save to the database
message_contents = request.POST['message']
words = message_contents.split
links = words.map{|word| word if word.end_with?(".com")}.compact
pictures = words.map{|word| word if word.end_with?(".jpg") || word.end_with?(".png") || word.end_with?(".gif") }.compact
links.each do |link|
if link.start_with?("http://")
message_contents[link] = "<a href=\'#{link}\'> #{link} </a>"
else
message_contents[link] = "<a href=\'http://#{link}\'> #{link} </a>"
end
end
pictures.each do |pic|
if pic.start_with?("http://")
message_contents[pic] = "<img src='#{pic}'>"
else
message_contents[pic] = "<img src='http://#{pic}'>"
end
end
@message_row = Message.new(:body => message_contents)
@message_row.save
"Message posted!"
end
class Message
# TODO: Use this class as a table in the database
include DataMapper::Resource
property :id, Serial # Auto-increment integer id
property :body, Text # A longer text block
property :created_at, DateTime # Auto assigns data/time
end
DataMapper.finalize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment