1
0
Fork 0

Organized

This commit is contained in:
Gregory Eremin 2013-07-09 14:53:49 +07:00
parent 409d3f26e2
commit ab5072c1c5
9 changed files with 76 additions and 33 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
config.rb
app/config.rb

View File

@ -7,3 +7,6 @@ gem 'pygments.rb'
gem 'haml'
gem 'sass'
gem 'unicorn'
gem 'shotgun'

View File

@ -3,6 +3,7 @@ GEM
specs:
haml (4.0.3)
tilt
kgio (2.8.0)
pg (0.15.1)
posix-spawn (0.3.6)
pygments.rb (0.5.1)
@ -11,13 +12,20 @@ GEM
rack (1.5.2)
rack-protection (1.5.0)
rack
raindrops (0.11.0)
sass (3.2.9)
sequel (4.0.0)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1)
unicorn (4.6.3)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
yajl-ruby (1.1.0)
PLATFORMS
@ -29,4 +37,6 @@ DEPENDENCIES
pygments.rb
sass
sequel
shotgun
sinatra
unicorn

8
app/database.rb Normal file
View File

@ -0,0 +1,8 @@
DB = Sequel.connect(CONFIG[:database_url])
DB.create_table(:pastes) do
primary_key :id
String :handle
String :type
Text :contents
end unless DB.table_exists?(:pastes)

20
app/paste.rb Normal file
View File

@ -0,0 +1,20 @@
module Paste
def get(params)
table.where(params).first
end
def add(params)
id = table.insert(params)
return unless id.is_a?(Integer)
get(id: id)
end
private
def table
DB[:pastes]
end
extend self
end

View File

@ -1,32 +0,0 @@
require 'securerandom'
require 'bundler/setup'
require 'sinatra'
require 'sequel'
require 'haml'
require File.expand_path('../config', __FILE__)
DB = Sequel.connect(CONFIG[:database_url])
DB.create_table :pastes do
primary_key :id
String :handle
String :type
Text :contents
end unless DB.table_exists? :pastes
set :public_folder, 'public'
get '/' do
haml :form, layout: :default
end
post '/' do
id = DB[:pastes].insert(handle: SecureRandom.hex, contents: params[:contents])
record = DB[:pastes].where(id: id).first
redirect "/#{id}/#{record[:handle]}"
end
get '/:id/:handle' do
@record = DB[:pastes].where(id: params[:id], handle: params[:handle]).first
haml :show, layout: :default
end

6
config.ru Normal file
View File

@ -0,0 +1,6 @@
$:.unshift File.dirname(__FILE__)
require 'pastemaster'
use Rack::ShowExceptions
run Pastemaster.new

28
pastemaster.rb Normal file
View File

@ -0,0 +1,28 @@
require 'securerandom'
require 'bundler/setup'
require 'sinatra'
require 'sequel'
require 'haml'
require 'app/config'
require 'app/database'
require 'app/paste'
class Pastemaster < Sinatra::Application
set :public_folder, 'public'
get '/' do
haml :form, layout: :default
end
post '/' do
record = Paste.add(handle: SecureRandom.hex, contents: params[:contents])
redirect record ? "/#{id}/#{record[:handle]}" : ''
end
get '/:id/:handle' do
@record = Paste.get(id: params[:id], handle: params[:handle])
haml :show, layout: :default
end
end