├── config.ru
├── .gems
├── views
├── show.erb
├── new.erb
└── layout.erb
├── README
├── MIT-LICENSE
└── toopaste.rb
/config.ru:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'sinatra'
3 |
4 | require 'toopaste'
5 | run Sinatra::Application
6 |
--------------------------------------------------------------------------------
/.gems:
--------------------------------------------------------------------------------
1 | sinatra --version '> 1.0'
2 | dm-core
3 | dm-validations
4 | dm-timestamps
5 | dm-migrations
6 | dm-postgres-adapter
7 | syntaxi
8 |
--------------------------------------------------------------------------------
/views/show.erb:
--------------------------------------------------------------------------------
1 |
2 |
<%= @snippet.title %>
3 |
4 | <%= @snippet.formatted_body %>
5 |
6 |
7 | Created on <%= @snippet.created_at.strftime("%D at %T %Z") %>
8 |
9 |
10 |
Create new paste!
11 |
12 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | Toopaste
2 | ========
3 |
4 | A simple (and tiny) Pastie clone written to demonstrate Sinatra and DataMapper
5 | usage. Originally created by Nick Plante with contributions from Dave Everitt.
6 |
7 | http://blog.zerosum.org/2008/7/2/clone-pastie-with-sinatra-datamapper-redux
8 |
9 | To see a live version of the service, check out http://toopastie.heroku.com
10 |
--------------------------------------------------------------------------------
/views/new.erb:
--------------------------------------------------------------------------------
1 |
2 |
Paste a new code snippet below:
3 |
9 |
10 |
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2010 Nick Plante
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/toopaste.rb:
--------------------------------------------------------------------------------
1 | #!/usr/local/bin/ruby -rubygems
2 |
3 | require 'sinatra'
4 | require 'dm-core'
5 | require 'dm-validations'
6 | require 'dm-timestamps'
7 | require 'dm-migrations'
8 | require 'syntaxi'
9 |
10 | DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://toopaste.db")
11 |
12 | class Snippet
13 | include DataMapper::Resource
14 |
15 | property :id, Serial # primary serial key
16 | property :title, String, :required => true, :length => 32
17 | property :body, Text, :required => true
18 | property :created_at, DateTime
19 | property :updated_at, DateTime
20 |
21 | # validates_present :body
22 | # validates_length :body, :minimum => 1
23 |
24 | Syntaxi.line_number_method = 'floating'
25 | Syntaxi.wrap_at_column = 80
26 | #Syntaxi.wrap_enabled = false
27 |
28 | def formatted_body
29 | replacer = Time.now.strftime('[code-%d]')
30 | html = Syntaxi.new("[code lang='ruby']#{self.body.gsub('[/code]',
31 | replacer)}[/code]").process
32 | "#{html.gsub(replacer,
33 | '[/code]')}
"
34 | end
35 | end
36 |
37 | DataMapper.auto_upgrade!
38 | #File.open('toopaste.pid', 'w') { |f| f.write(Process.pid) }
39 |
40 | # new
41 | get '/' do
42 | erb :new
43 | end
44 |
45 | # create
46 | post '/' do
47 | @snippet = Snippet.new(:title => params[:snippet_title],
48 | :body => params[:snippet_body])
49 | if @snippet.save
50 | redirect "/#{@snippet.id}"
51 | else
52 | redirect '/'
53 | end
54 | end
55 |
56 | # show
57 | get '/:id' do
58 | @snippet = Snippet.get(params[:id])
59 | if @snippet
60 | erb :show
61 | else
62 | redirect '/'
63 | end
64 | end
65 |
--------------------------------------------------------------------------------
/views/layout.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | <%= @title || 'Toopaste!' %>
5 |
60 |
61 |
62 | <%= yield %>
63 |
64 |
65 |
--------------------------------------------------------------------------------