Class: Stark::Rack::Metadata::Middleware

Inherits:
Object
  • Object
show all
Includes:
ContentNegotiation
Defined in:
lib/stark/rack/metadata.rb

Constant Summary

Constants included from ContentNegotiation

ContentNegotiation::THRIFT_CONTENT_TYPE, ContentNegotiation::THRIFT_JSON_CONTENT_TYPE

Instance Method Summary collapse

Methods included from ContentNegotiation

#accept_json?, #headers, #protocol_factory

Constructor Details

#initialize(app, metadata) ⇒ Middleware

Returns a new instance of Middleware.



17
18
19
20
21
# File 'lib/stark/rack/metadata.rb', line 17

def initialize(app, )
  @app = app
  @handler = Handler.new 
  @processor = Processor.new @handler
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stark/rack/metadata.rb', line 23

def call(env)
  env['rack.input'] = RewindableInput.new(env['rack.input'])
  status, hdr, body = @app.call env

  if status == 404
    env['rack.input'].rewind

    out       = StringIO.new
    transport = Thrift::IOStreamTransport.new env['rack.input'], out
    protocol  = protocol_factory(env).get_protocol transport

    if @processor.process(protocol, protocol)
      return [200, headers(env), [out.string]]
    end
  end

  [status, hdr, body]
end