Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 105 lines (87 sloc) 2.414 kB
2b8ae37 @mislav chunky bacon!
authored
1 require 'sinatra'
2 require 'sass'
af0325d @mislav routes for testing; use local docco style
authored
3 require 'pp'
c6fc6a9 @mislav initial processor; code fetching from URL
authored
4 require 'code'
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
5 require 'mongo'
6
194a23a @mislav writeable Sass cache location
authored
7 set :sass, { :cache_location => File.join(ENV['TMPDIR'], '.sass-cache') }
8
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
9 configure :development do
10 connection = Mongo::Connection.from_uri 'mongodb://localhost'
11 db = connection.db('explainruby')
12 ExplainRuby::Code.mongo = db.collection('results')
13 end
af0325d @mislav routes for testing; use local docco style
authored
14
b2d37e7 @mislav connect to MongoHQ
authored
15 configure :production do
16 connection = Mongo::Connection.from_uri ENV['MONGOHQ_URL']
17 db = connection.db(connection.auths.last['db_name'])
18 ExplainRuby::Code.mongo = db.collection('results')
19 end
20
2b8ae37 @mislav chunky bacon!
authored
21 require 'mustache/sinatra'
22 set :mustache, { :templates => './templates', :views => './views' }
23
133deb6 @mislav switch rocco from pygments to ultraviolet
authored
24 require 'rocco_ext'
2b8ae37 @mislav chunky bacon!
authored
25
26 helpers do
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
27 def redirect_to(code)
28 redirect "/#{code.slug}"
2b8ae37 @mislav chunky bacon!
authored
29 end
30
f205833 @mislav rocco: link to original source URL (if any)
authored
31 def rocco(options = {}, &block)
133deb6 @mislav switch rocco from pygments to ultraviolet
authored
32 options = settings.rocco.merge(options)
f205833 @mislav rocco: link to original source URL (if any)
authored
33 Rocco.new(default_title, [], options, &block).to_html
c6fc6a9 @mislav initial processor; code fetching from URL
authored
34 rescue Racc::ParseError
35 status 500
36 @message = "There was a parse error when trying to process Ruby code"
37 mustache :error
2b8ae37 @mislav chunky bacon!
authored
38 end
39
c6fc6a9 @mislav initial processor; code fetching from URL
authored
40 def default_title
41 "Explain Ruby"
2b8ae37 @mislav chunky bacon!
authored
42 end
133deb6 @mislav switch rocco from pygments to ultraviolet
authored
43
44 def sass_with_caching(name)
45 time = ::File.mtime ::File.join(settings.views, "#{name}.sass")
46 expires 500, :public, :must_revalidate if settings.environment == :production
47 last_modified time
48 content_type 'text/css'
49 sass name
50 end
2b8ae37 @mislav chunky bacon!
authored
51 end
52
53 get '/' do
a9e8f2c @mislav redirect from explainruby.heroku.com to explainruby.net
authored
54 if request.host == 'explainruby.heroku.com'
55 redirect 'http://explainruby.net'
56 else
57 mustache :home
58 end
2b8ae37 @mislav chunky bacon!
authored
59 end
60
c1ae6af @mislav enable the bookmarklet
authored
61 get '/url/*' do
62 code = ExplainRuby::Code.from_url params[:splat].join('')
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
63 redirect_to code
64 end
65
c6fc6a9 @mislav initial processor; code fetching from URL
authored
66 post '/' do
67 if not params[:url].empty?
68 code = ExplainRuby::Code.from_url params[:url]
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
69 redirect_to code
c6fc6a9 @mislav initial processor; code fetching from URL
authored
70 elsif not params[:code].empty?
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
71 code = ExplainRuby::Code.create params[:code]
72 redirect_to code
c6fc6a9 @mislav initial processor; code fetching from URL
authored
73 else
74 status "400 Not Chunky"
75 @message = "Please paste some code or enter a URL"
76 mustache :error
77 end
78 end
79
af0325d @mislav routes for testing; use local docco style
authored
80 get '/f/:name' do
d51ba1a @mislav fix inspecting sexps when manual testing
authored
81 code = ExplainRuby::Code.from_test_fixture(params[:name])
f205833 @mislav rocco: link to original source URL (if any)
authored
82 rocco(:url => code.url) { code.to_s }
af0325d @mislav routes for testing; use local docco style
authored
83 end
84
c6fc6a9 @mislav initial processor; code fetching from URL
authored
85 get '/f/:name/sexp' do
86 content_type 'text/plain'
d51ba1a @mislav fix inspecting sexps when manual testing
authored
87 code = ExplainRuby::Code.from_test_fixture(params[:name])
88 code.pretty_inspect
af0325d @mislav routes for testing; use local docco style
authored
89 end
90
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
91 get '/explain.css' do
92 sass_with_caching :explain
2b8ae37 @mislav chunky bacon!
authored
93 end
af0325d @mislav routes for testing; use local docco style
authored
94
95 get '/docco.css' do
133deb6 @mislav switch rocco from pygments to ultraviolet
authored
96 sass_with_caching :docco
af0325d @mislav routes for testing; use local docco style
authored
97 end
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
98
99 get %r!^/([a-z0-9]{3,})$! do
100 code = ExplainRuby::Code.find params[:captures][0]
101 halt 404 unless code
102 etag code.md5
f205833 @mislav rocco: link to original source URL (if any)
authored
103 rocco(:url => code.url) { code.to_s }
94eb7ba @mislav persist with mongodb; better homepage, styles
authored
104 end
Something went wrong with that request. Please try again.