forked from rails/jquery-ujs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
63 lines (54 loc) · 1.49 KB
/
server.rb
File metadata and controls
63 lines (54 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'sinatra'
require 'json'
use Rack::Static, :urls => ["/src"], :root => File.expand_path('..', settings.root)
helpers do
def jquery_link version
if params[:version] == version
"[#{version}]"
else
"<a href='/?version=#{version}'>#{version}</a>"
end
end
def jquery_src
if params[:version] == 'edge' then "/vendor/jquery.js"
else "http://code.jquery.com/jquery-#{params[:version]}.js"
end
end
def test *names
names = ["/vendor/qunit.js", "settings"] + names
names.map { |name| script_tag name }.join("\n")
end
def script_tag src
src = "/test/#{src}.js" unless src.index('/')
%(<script src="#{src}" type="text/javascript"></script>)
end
end
get '/' do
params[:version] ||= '1.4.4'
erb :index
end
[:get, :post, :put, :delete].each do |method|
send(method, '/echo') {
data = { :params => params }.update(request.env)
if request.xhr?
content_type 'application/json'
data.to_json
elsif params[:iframe]
payload = data.to_json.gsub('<', '<').gsub('>', '>')
<<-HTML
<script>
if (window.top && window.top !== window)
window.top.jQuery.event.trigger('iframe:loaded', #{payload})
</script>
<p>You shouldn't be seeing this. <a href="#{request.env['HTTP_REFERER']}">Go back</a></p>
HTML
else
content_type 'text/plain'
status 400
"ERROR: #{request.path} requested without ajax"
end
}
end
get '/error' do
status 403
end