Module: Sinatra::Jsonp

Defined in:
lib/sinatra/jsonp.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) jsonp(*args) Also known as: JSONP



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sinatra/jsonp.rb', line 6

def jsonp(*args)
  if args.size > 0
    data = MultiJson.dump args[0], :pretty => settings.respond_to?(:json_pretty) && settings.json_pretty
    if args.size > 1
      callback = args[1].to_s
    else
      ['callback','jscallback','jsonp','jsoncallback'].each do |x|
        callback = params.delete(x) unless callback
      end
    end
    if callback
      callback.tr!('^a-zA-Z0-9_$\.', '')
      content_type :js
      response = "#{callback}(#{data})"
    else
      content_type :json
      response = data
    end
    response
  end
end