-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_jsonp_app.rb
46 lines (32 loc) · 1.06 KB
/
json_jsonp_app.rb
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
#!/usr/bin/env ruby
# json_jsonp_app.rb
# Copyright 2013 Robert Jones ([email protected]) Craic Computing LLC
# This code and associated data files are distributed freely under the terms of the MIT license
require 'erb'
require 'json'
$:.unshift File.join(File.dirname(__FILE__))
class JsonJsonpApp < Sinatra::Base
set :root, File.dirname(__FILE__)
set :static, true
get '/' do
redirect to('index.html')
end
get '/json' do
content_type :json
# This content is whatever you want to send as long as it can be converted into JSON
content = { :response => 'Sent via JSON',
:timestamp => Time.now,
:random => rand(10000) }
content.to_json
end
# Generate the response as a function with the callback parameter as the function name
# e.g. <callback>( <json> )
get '/jsonp' do
callback = params['callback']
content_type :js
content = { :response => 'Sent via JSONP',
:timestamp => Time.now,
:random => rand(10000) }
"#{callback}(#{content.to_json})"
end
end