Class: Thrift::RackApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift/server/rack_application.rb

Constant Summary collapse

THRIFT_HEADER =
'application/x-thrift'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, protocol_factory) ⇒ RackApplication

Returns a new instance of RackApplication.



34
35
36
37
38
# File 'lib/thrift/server/rack_application.rb', line 34

def initialize(processor, protocol_factory)
  @processor = processor
  @protocol_factory = protocol_factory
  @headers = { 'Content-Type' => THRIFT_HEADER }
end

Class Method Details

.for(path, processor, protocol_factory) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/thrift/server/rack_application.rb', line 26

def self.for(path, processor, protocol_factory)
  Rack::Builder.new do
    map path do
      run Thrift::RackApplication.new(processor, protocol_factory)
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/thrift/server/rack_application.rb', line 40

def call(env)
  req = Rack::Request.new(env)

  if valid_thrift_request?(req)
    successful_request(req)
  else
    failed_request
  end
end

#failed_requestObject



60
61
62
# File 'lib/thrift/server/rack_application.rb', line 60

def failed_request
  [404, @headers, 'Not found']
end

#successful_request(req) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/thrift/server/rack_application.rb', line 50

def successful_request(req)
  resp = Rack::Response.new([], 200, @headers)
  transport = IOStreamTransport.new req.body, resp
  protocol = @protocol_factory.get_protocol transport

  @processor.process protocol, protocol

  [resp.status, resp.headers, resp.body]
end

#valid_thrift_request?(req) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/thrift/server/rack_application.rb', line 64

def valid_thrift_request?(req)
  req.post? && req.env['CONTENT_TYPE'] == THRIFT_HEADER
end