Class: Jsonp::CallbackWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonp/callback_wrapper.rb

Overview

Wraps JSONP response with appropriate callback.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CallbackWrapper

Returns a new instance of CallbackWrapper.



5
6
7
8
# File 'lib/jsonp/callback_wrapper.rb', line 5

def initialize(app, options = {})
  @app = app
  @callback_param = options.fetch(:callback_param, "_callback")
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jsonp/callback_wrapper.rb', line 10

def call(env)
  status, headers, response = @app.call(env)
  callback = Utils.extract_callback(env, @callback_param)

  if callback
    prefix, suffix = "#{callback}(", ")"
    response = Utils::Wrapper.new(prefix, response, suffix)
    headers["Content-Length"] = (prefix.bytesize + headers["Content-Length"].to_i + suffix.bytesize).to_s
  end

  [status, headers, response]
end