async_sinatra 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/examples/basic.ru +7 -1
  2. data/lib/sinatra/async.rb +25 -4
  3. metadata +2 -2
data/examples/basic.ru CHANGED
@@ -4,6 +4,8 @@ require 'sinatra/async'
4
4
  class AsyncTest < Sinatra::Base
5
5
  register Sinatra::Async
6
6
 
7
+ enable :show_exceptions
8
+
7
9
  aget '/' do
8
10
  body "hello async"
9
11
  end
@@ -12,6 +14,10 @@ class AsyncTest < Sinatra::Base
12
14
  EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } }
13
15
  end
14
16
 
17
+ aget '/raise' do
18
+ raise 'boom'
19
+ end
20
+
15
21
  end
16
22
 
17
- run AsyncTest.new
23
+ run AsyncTest.new
data/lib/sinatra/async.rb CHANGED
@@ -57,11 +57,32 @@ module Sinatra #:nodoc:
57
57
  def ahead(path, opts={}, &bk); aroute 'HEAD', path, opts, &bk; end
58
58
 
59
59
  private
60
- def aroute(*args, &block) #:nodoc:
61
- self.send :route, *args do |*bargs|
60
+ def aroute(verb, path, opts = {}, &block) #:nodoc:
61
+ route(verb, path, opts) do |*bargs|
62
+ method = "A#{verb} #{path}".to_sym
63
+
62
64
  mc = class << self; self; end
63
- mc.send :define_method, :__async_callback, &block
64
- EM.next_tick { send(:__async_callback, *bargs) }
65
+ mc.send :define_method, method, &block
66
+ mc.send :alias_method, :__async_callback, method
67
+
68
+ EM.next_tick {
69
+ begin
70
+ send(:__async_callback, *bargs)
71
+ rescue ::Exception => boom
72
+ if options.show_exceptions?
73
+ # HACK: handle_exception! re-raises the exception if show_exceptions?,
74
+ # so we ignore any errors and instead create a ShowExceptions page manually
75
+ handle_exception!(boom) rescue nil
76
+ s, h, b = Sinatra::ShowExceptions.new(proc{ raise boom }).call(request.env)
77
+ response.status = s
78
+ response.headers.replace(h)
79
+ body(b)
80
+ else
81
+ body(handle_exception!(boom))
82
+ end
83
+ end
84
+ }
85
+
65
86
  throw :async
66
87
  end
67
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Tucker
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements: []
95
95
 
96
96
  rubyforge_project: libraggi
97
- rubygems_version: 1.3.1
97
+ rubygems_version: 1.3.5
98
98
  signing_key:
99
99
  specification_version: 2
100
100
  summary: Asynchronous response API for Sinatra and Thin