File: server.rb

package info (click to toggle)
node-jquery-ujs 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: javascript: 3,989; ruby: 70; sh: 32; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 2,058 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'sinatra'
require 'json'

JQUERY_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 3.0.0].freeze

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}&cdn=#{params[:cdn]}'>#{version}</a>"
    end
  end

  def cdn_link cdn
    if params[:cdn] == cdn
      "[#{cdn}]"
    else
      "<a href='/?version=#{params[:version]}&cdn=#{cdn}'>#{cdn}</a>"
    end
  end

  def jquery_src
    if params[:version] == 'edge'
      "/vendor/jquery.js"
    elsif params[:cdn] && params[:cdn] == 'googleapis'
      "https://ajax.googleapis.com/ajax/libs/jquery/#{params[:version]}/jquery.min.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

  def jquery_versions
    JQUERY_VERSIONS
  end
end

get '/' do
  params[:version] ||= ENV['JQUERY_VERSION'] || '1.11.0'
  params[:cdn] ||= 'jquery'
  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('<', '&lt;').gsub('>', '&gt;')
      <<-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