Class: Grape::Async
- Inherits:
-
Middleware::Base
- Object
- Middleware::Base
- Grape::Async
- Defined in:
- lib/grape-async/async.rb,
lib/grape-async/extension.rb
Defined Under Namespace
Modules: Extension
Instance Method Summary collapse
Instance Method Details
#async_call(result) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/grape-async/async.rb', line 41 def async_call(result) if @env['async.callback'] async_io.call result elsif @env['rack.hijack'] status, headers, body = result begin async_io.write("HTTP/1.1 #{status}\r\n") headers.each do |key, value| async_io.write("#{key}: #{value}\r\n") end async_io.write("Connection: close\r\n") async_io.write("\r\n") body.each do |data| async_io.write(data) end ensure async_io.close end end end |
#async_io ⇒ Object
62 63 64 65 66 |
# File 'lib/grape-async/async.rb', line 62 def async_io @async_io ||= @env['async.callback'] || begin @env.key?('rack.hijack') ? @env['rack.hijack'].call : nil end end |
#call!(env) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/grape-async/async.rb', line 4 def call!(env) @env = env if endpoint.async_route? && !async_io.nil? if endpoint.async_route?(:em) proc = lambda { EM.next_tick do super endpoint.deferred_resp.callback do resp = endpoint.file || [endpoint.body] async_call [endpoint.status, endpoint.header, resp] end end } if !EM.reactor_running? EM.run do proc.call end else proc.call end else Thread.new do result = super async_call result yield end end [-1, {}, []] # Return async response else super end end |
#endpoint ⇒ Object
68 69 70 |
# File 'lib/grape-async/async.rb', line 68 def endpoint @env[Grape::Env::API_ENDPOINT] end |