Class: DripDrop::HTTPApp

Inherits:
Object
  • Object
show all
Defined in:
lib/dripdrop/handlers/http.rb

Constant Summary collapse

AsyncResponse =
[-1, {}, []].freeze

Instance Method Summary collapse

Constructor Details

#initialize(msg_format, &block) ⇒ HTTPApp

Returns a new instance of HTTPApp.



34
35
36
37
# File 'lib/dripdrop/handlers/http.rb', line 34

def initialize(msg_format,&block)
  @msg_format = msg_format
  @recv_cbak  = block
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dripdrop/handlers/http.rb', line 39

def call(env)
  body = HTTPDeferrableBody.new
  
  EM.next_tick do
    env['async.callback'].call([200, {'Content-Type' => 'text/plain', 'Access-Control-Allow-Origin' => '*'}, body])
    EM.next_tick do
      case @msg_format
      when :dripdrop_json
        msg = DripDrop::Message.decode_json(env['rack.input'].read)
        @recv_cbak.call(msg,body,env)
      else
        raise "Unsupported message type #{@msg_format}"
      end
    end
  end
   
  AsyncResponse
end