module Sinatra::Async
Normally Sinatra
expects that the completion of a request is # determined by the block exiting, and returning a value for the body.
In an async environment, we want to tell the webserver that we're not going to provide a response now, but some time in the future.
The a* methods provide a method for doing this, by informing the server of our asynchronous intent, and then scheduling our action code (your block) as the next thing to be invoked by the server.
This code can then do a number of things, including waiting (asynchronously) for external servers, services, and whatnot to complete. When ready to send the real response, simply setup your environment as you normally would for sinatra (using content_type, headers, etc). Finally, you complete your response body by calling the body method. This will push your body into the response object, and call out to the server to actually send that data.
Example¶ ↑
require 'sinatra/async' class AsyncTest < Sinatra::Base register Sinatra::Async aget '/' do body "hello async" end aget '/delay/:n' do |n| EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } } end end
Public Instance Methods
See aget
.
# File lib/sinatra/async.rb, line 56 def adelete(path, opts={}, &bk); aroute 'DELETE', path, opts, &bk; end
Similar to Sinatra::Base#get, but the block will be scheduled to run during the next tick of the EventMachine reactor. In the meantime, Thin will hold onto the client connection, awaiting a call to Async#body with the response.
# File lib/sinatra/async.rb, line 43 def aget(path, opts={}, &block) conditions = @conditions.dup aroute('GET', path, opts, &block) @conditions = conditions aroute('HEAD', path, opts, &block) end
See aget
.
# File lib/sinatra/async.rb, line 58 def ahead(path, opts={}, &bk); aroute 'HEAD', path, opts, &bk; end
See aget
# File lib/sinatra/async.rb, line 64 def alink(path, opts={}, &bk); aroute 'LINK', path, opts, &bk; end
See aget
# File lib/sinatra/async.rb, line 60 def aoptions(path, opts={}, &bk); aroute 'OPTIONS', path, opts, &bk; end
See aget
# File lib/sinatra/async.rb, line 62 def apatch(path, opts={}, &bk); aroute 'PATCH', path, opts, &bk; end
See aget
.
# File lib/sinatra/async.rb, line 54 def apost(path, opts={}, &bk); aroute 'POST', path, opts, &bk; end
See aget
.
# File lib/sinatra/async.rb, line 52 def aput(path, opts={}, &bk); aroute 'PUT', path, opts, &bk; end
See aget
# File lib/sinatra/async.rb, line 66 def aunlink(path, opts={}, &bk); aroute 'UNLINK', path, opts, &bk; end