UNPKG

2.05 kBPlain TextView Raw
1require 'sinatra'
2require 'json'
3
4JQUERY_VERSIONS = %w[ 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.10.0 1.10.1 1.10.2 1.11.0 2.0.0 2.1.0].freeze
5
6use Rack::Static, :urls => ["/src"], :root => File.expand_path('..', settings.root)
7
8helpers do
9 def jquery_link version
10 if params[:version] == version
11 "[#{version}]"
12 else
13 "<a href='/?version=#{version}&cdn=#{params[:cdn]}'>#{version}</a>"
14 end
15 end
16
17 def cdn_link cdn
18 if params[:cdn] == cdn
19 "[#{cdn}]"
20 else
21 "<a href='/?version=#{params[:version]}&cdn=#{cdn}'>#{cdn}</a>"
22 end
23 end
24
25 def jquery_src
26 if params[:version] == 'edge'
27 "/vendor/jquery.js"
28 elsif params[:cdn] && params[:cdn] == 'googleapis'
29 "https://ajax.googleapis.com/ajax/libs/jquery/#{params[:version]}/jquery.min.js"
30 else
31 "http://code.jquery.com/jquery-#{params[:version]}.js"
32 end
33 end
34
35 def test *names
36 names = ["/vendor/qunit.js", "settings"] + names
37 names.map { |name| script_tag name }.join("\n")
38 end
39
40 def script_tag src
41 src = "/test/#{src}.js" unless src.index('/')
42 %(<script src="#{src}" type="text/javascript"></script>)
43 end
44
45 def jquery_versions
46 JQUERY_VERSIONS
47 end
48end
49
50get '/' do
51 params[:version] ||= ENV['JQUERY_VERSION'] || '1.11.0'
52 params[:cdn] ||= 'jquery'
53 erb :index
54end
55
56[:get, :post, :put, :delete].each do |method|
57 send(method, '/echo') {
58 data = { :params => params }.update(request.env)
59
60 if request.xhr?
61 content_type 'application/json'
62 data.to_json
63 elsif params[:iframe]
64 payload = data.to_json.gsub('<', '&lt;').gsub('>', '&gt;')
65 <<-HTML
66 <script>
67 if (window.top && window.top !== window)
68 window.top.jQuery.event.trigger('iframe:loaded', #{payload})
69 </script>
70 <p>You shouldn't be seeing this. <a href="#{request.env['HTTP_REFERER']}">Go back</a></p>
71 HTML
72 else
73 content_type 'text/plain'
74 status 400
75 "ERROR: #{request.path} requested without ajax"
76 end
77 }
78end
79
80get '/error' do
81 status 403
82end