Class: Rack::JSONP::StatusWrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ StatusWrapper

Returns a new instance of StatusWrapper.



5
6
7
# File 'lib/rack/jsonp/status_wrapper.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/jsonp/status_wrapper.rb', line 9

def call(env)
  if env["jsonp.callback"]
    # Call original app
    status, headers, @body = @app.call(env)

    if headers["Content-Type"] == "application/json"
      @pre, @post = '{"body": ', ', "status": ' + status.to_s + '}'
      headers["Content-Length"] = (@pre.size + headers["Content-Length"].to_i + @post.size).to_s
      [status, headers, self]
    else
      [status, headers, @body]
    end
  else
    @app.call(env)
  end
end

#each(&block) ⇒ Object



26
27
28
29
30
# File 'lib/rack/jsonp/status_wrapper.rb', line 26

def each(&block)
  block.call(@pre)
  @body.each(&block)
  block.call(@post)
end