Class: Jsonp::StatusWrapper
- Inherits:
-
Object
- Object
- Jsonp::StatusWrapper
- Defined in:
- lib/jsonp/status_wrapper.rb
Overview
Wraps JSONP response with HTTP status code and message. If the response contains no JSON data, it’s replaced by JSON status and raises no error in the browser.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ StatusWrapper
constructor
A new instance of StatusWrapper.
Constructor Details
#initialize(app, options = {}) ⇒ StatusWrapper
Returns a new instance of StatusWrapper.
7 8 9 10 |
# File 'lib/jsonp/status_wrapper.rb', line 7 def initialize(app, = {}) @app = app @callback_param = .fetch(:callback_param, "_callback") end |
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jsonp/status_wrapper.rb', line 12 def call(env) status, headers, response = @app.call(env) callback = Utils.extract_callback(env, @callback_param) if callback && ((200...300).include?(status.to_i) || (400...600).include?(status.to_i)) type = Utils.(status) prefix, suffix = %Q({"status":#{status},"type":"#{type}"), "}" if headers["Content-Type"] =~ %r{^(?:application/json|text/json|text/x-json)} prefix << %Q(,"content":) response = Utils::Wrapper.new(prefix, response, suffix) headers["Content-Length"] = (prefix.bytesize + headers["Content-Length"].to_i + suffix.bytesize).to_s else headers["Content-Type"] = "application/json" response = [prefix, suffix] headers["Content-Length"] = (prefix.bytesize + suffix.bytesize).to_s end status = 200 end [status, headers, response] end |