async_sinatra 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ === 1.0.0 / 2012-02-20
2
+
3
+ * Fixed basic usage of #params inside route blocks.
4
+ * Introduced #aparams for use inside of asynchronous actions in place of #params
5
+ * Bumped the rack and sinatra dependencies. Yes, this might be rails
6
+ incompatible. Make your async apps standalone, it'll save you anyway.
7
+ * Various minor bug fixes
8
+
1
9
  === 0.5.0 / 2011-03-08
2
10
 
3
11
  * Fix support for conditional routes!
data/Rakefile CHANGED
@@ -6,8 +6,8 @@ Hoe.plugin :doofus, :git, :minitest, :gemspec2, :rubyforge
6
6
  Hoe.spec 'async_sinatra' do
7
7
  developer 'raggi', 'raggi@rubyforge.org'
8
8
 
9
- extra_deps << %w[rack >=1.2.1]
10
- extra_deps << %w[sinatra >=1.0]
9
+ extra_deps << %w[rack >=1.4.1]
10
+ extra_deps << %w[sinatra >=1.3.2]
11
11
 
12
12
  extra_dev_deps << %w(hoe-doofus >=1.0)
13
13
  extra_dev_deps << %w(hoe-seattlerb >=1.2)
data/lib/async_sinatra.rb CHANGED
@@ -2,5 +2,5 @@ require 'sinatra/async'
2
2
  # This is just a stub module for documentation and versioning. Please see
3
3
  # Sinatra::Async.
4
4
  module AsyncSinatra
5
- VERSION = '0.5.0'
5
+ VERSION = '1.0.0'
6
6
  end
data/lib/sinatra/async.rb CHANGED
@@ -71,17 +71,43 @@ module Sinatra #:nodoc:
71
71
  end
72
72
 
73
73
  module Helpers
74
+ # aparams are used when inside of something that has been scheduled
75
+ # asynchronously in an asynchronous route. Essentially it is the last
76
+ # parameters to be set by the router (for example, containing the route
77
+ # captures).
78
+ attr_reader :aparams
79
+
74
80
  # Send the given body or block as the final response to the asynchronous
75
81
  # request.
76
- def body(*args)
77
- if @async_running
78
- block_given? ? async_handle_exception { super(yield) } : super
82
+ def body(value = nil)
83
+ if @async_running && (value || block_given?)
84
+ if block_given?
85
+ super { async_handle_exception { yield } }
86
+ else
87
+ super
88
+ end
89
+
79
90
  if response.body.respond_to?(:call)
80
91
  response.body = Array(async_handle_exception {response.body.call})
81
92
  end
82
- request.env['async.callback'][
83
- [response.status, response.headers, response.body]
84
- ]
93
+
94
+ # Taken from Base#call
95
+ unless @response['Content-Type']
96
+ if Array === body and body[0].respond_to? :content_type
97
+ content_type body[0].content_type
98
+ else
99
+ content_type :html
100
+ end
101
+ end
102
+
103
+ result = response.finish
104
+
105
+ # We don't get the HEAD middleware, so just do something convenient
106
+ # here.
107
+ result[-1] = [] if request.head?
108
+
109
+ request.env['async.callback'][ result ]
110
+ body
85
111
  else
86
112
  super
87
113
  end
@@ -111,8 +137,17 @@ module Sinatra #:nodoc:
111
137
  throw :async
112
138
  end
113
139
 
114
- def async_runner(method, *bargs)
115
- async_schedule { __send__(method, *bargs); nil }
140
+ def async_runner(meth, *bargs)
141
+ @aparams = @params.dup
142
+ async_schedule do
143
+ begin
144
+ original, @params = @params, @aparams
145
+ method(meth).arity == 0 ? send(meth) : send(meth, *bargs)
146
+ ensure
147
+ @params = original
148
+ end
149
+ nil
150
+ end
116
151
  end
117
152
 
118
153
  def async_catch_execute(&b)
@@ -135,6 +170,7 @@ module Sinatra #:nodoc:
135
170
  response.status = s
136
171
  response.headers.replace(h)
137
172
  response.body = b
173
+ body(response.body)
138
174
  else
139
175
  body(handle_exception!(boom))
140
176
  end
@@ -78,11 +78,22 @@ class Sinatra::Async::Test
78
78
  # Executes the pending asynchronous blocks, required for the
79
79
  # aget/apost/etc blocks to run.
80
80
  def async_continue
81
- while b = app.options.async_schedules.shift
81
+ while b = settings.async_schedules.shift
82
82
  b.call
83
83
  end
84
84
  end
85
85
 
86
+ def settings
87
+ # This hack exists because sinatra is now returning a proper rack stack.
88
+ # We might need to consider alternative approaches in future.
89
+ app = app()
90
+ until app.nil? || app.is_a?(Sinatra::Base)
91
+ app = app.instance_variable_get(:@app)
92
+ end
93
+ raise "Cannot determine sinatra application from #{app()}" unless app
94
+ app.settings
95
+ end
96
+
86
97
  # Crank the eventmachine loop until a response is made, or timeout after a
87
98
  # particular period, by default 10s. If the timeout is nil, no timeout
88
99
  # will occur.
data/test/test_async.rb CHANGED
@@ -26,6 +26,10 @@ class TestSinatraAsync < MiniTest::Unit::TestCase
26
26
  body { 'hello async' }
27
27
  end
28
28
 
29
+ aget '/world' do
30
+ body 'world'
31
+ end
32
+
29
33
  aget '/em' do
30
34
  EM.add_timer(0.001) { body { 'em' }; EM.stop }
31
35
  end
@@ -90,6 +94,23 @@ class TestSinatraAsync < MiniTest::Unit::TestCase
90
94
  body { 'other' }
91
95
  end
92
96
 
97
+ set :call_count, 0
98
+ aget '/double_body_bug/:subdomain' do |subdomain|
99
+ body { settings.call_count += 1; '' }
100
+ end
101
+
102
+ aget '/param/:a' do |ma|
103
+ body { ma }
104
+ end
105
+
106
+ aget '/params/:a' do
107
+ body { params[:a] }
108
+ end
109
+
110
+ aget '/params/deferred/:a' do
111
+ async_schedule { body { aparams[:a] } }
112
+ end
113
+
93
114
  # Defeat the test environment semantics, ensuring we actually follow the
94
115
  # non-test branch of async_schedule. You would normally just call
95
116
  # async_schedule in user apps, and use test helpers appropriately.
@@ -223,4 +244,42 @@ class TestSinatraAsync < MiniTest::Unit::TestCase
223
244
  aget '/agents'
224
245
  assert_equal 'firefox', last_response.body
225
246
  end
247
+
248
+ def test_basic_async_head
249
+ head '/hello'
250
+ assert_async
251
+ async_continue
252
+ assert last_response.ok?
253
+ assert_equal '', last_response.body
254
+ assert_equal "11", last_response.headers['Content-Length']
255
+ end
256
+
257
+ def test_string_body
258
+ get '/world'
259
+ assert_async
260
+ async_continue
261
+ assert last_response.ok?
262
+ assert_equal 'world', last_response.body
263
+ end
264
+
265
+ def test_double_body_bug
266
+ aget '/double_body_bug/example'
267
+ assert_equal 1, settings.call_count
268
+ end
269
+
270
+ def test_block_with_no_args
271
+ aget '/params/test'
272
+ assert_equal 'test', last_response.body
273
+ end
274
+
275
+ def test_block_with_an_arg
276
+ aget '/param/test'
277
+ assert_equal 'test', last_response.body
278
+ end
279
+
280
+ def test_params_deferred
281
+ aget '/params/deferred/test'
282
+ assert_equal 'test', last_response.body
283
+ end
284
+
226
285
  end
metadata CHANGED
@@ -1,236 +1,175 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: async_sinatra
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 5
9
- - 0
10
- version: 0.5.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - raggi
14
9
  autorequire:
15
10
  bindir: bin
16
- cert_chain:
17
- - |
18
- -----BEGIN CERTIFICATE-----
19
- MIIDNDCCAhygAwIBAgIBADANBgkqhkiG9w0BAQUFADBAMQ4wDAYDVQQDDAVyYWdn
20
- aTEZMBcGCgmSJomT8ixkARkWCXJ1Ynlmb3JnZTETMBEGCgmSJomT8ixkARkWA29y
21
- ZzAeFw0xMDEyMjIyMDIyMzlaFw0xMTEyMjIyMDIyMzlaMEAxDjAMBgNVBAMMBXJh
22
- Z2dpMRkwFwYKCZImiZPyLGQBGRYJcnVieWZvcmdlMRMwEQYKCZImiZPyLGQBGRYD
23
- b3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvgeVrJxLGm2PgcrM
24
- DoB5mgQVC8PMYzpzJ64+75DQ1BgHa6wIeAuRphLY9qQh+sTTvKbUHtdgkWGghRVf
25
- GaRVmtFlDHRt6RHWBmxXhXmcywT6c1xeni+eC7p56/zSkZ2RFHaZ4BXC4+8lO56j
26
- sAh7c+YTcY1xd7teRX6g98aIep4MLxQlbEY+DcdDvtCIAswUYNVY+Nw08goIEw5Q
27
- Sjm4fjrFPBT0T6RsmkcI4ea78h993nrWPdIndgVniwBc/SxGuBUGrfm637yCXNwL
28
- FUzVd0qsJcIeMhXS8ZTii04oWKcK+G2B7KZ8/+olu7TwJu5mom9ORRMlflrTxJxi
29
- rd4TBQIDAQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU
30
- 2QvD5zSSmpVqxaJSYrFbamIWCn0wDQYJKoZIhvcNAQEFBQADggEBAHwCkRkMjr/h
31
- oYjGvs3BdEDuEYvzpKgWr6xv0hjsklBZ096dOjm0tLNSdplpn5MALUw3Cqtgp1KK
32
- EKEZgEi9fbhhyzhkQyoociuqLQQ0daZuigh2CESrznX1b8AawCLyB2bJ9blNylPi
33
- GWGaAjsqPBtAP6LoQib2aS0YuK7kVWxUTlupQ2D3Af5ag6NY8pS/etpS4IMVQU5l
34
- v9ctVo17bHsWM3sjLW3Lj6xzYheYo0r9YAMOLXz7c92JwDQahDN95W4rUTj68rAQ
35
- dHaj98YtwM8tjKyJsxZSeZEn+4wEtwTHuqU7v/HaK7OX+eIb090ZsV5ii5YpD/Fh
36
- WjgTOJft4S4=
37
- -----END CERTIFICATE-----
38
-
39
- date: 2011-03-08 00:00:00 -08:00
40
- default_executable:
41
- dependencies:
42
- - !ruby/object:Gem::Dependency
11
+ cert_chain: []
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
43
15
  name: rack
44
- prerelease: false
45
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70275016993560 !ruby/object:Gem::Requirement
46
17
  none: false
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- hash: 29
51
- segments:
52
- - 1
53
- - 2
54
- - 1
55
- version: 1.2.1
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.1
56
22
  type: :runtime
57
- version_requirements: *id001
58
- - !ruby/object:Gem::Dependency
59
- name: sinatra
60
23
  prerelease: false
61
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70275016993560
25
+ - !ruby/object:Gem::Dependency
26
+ name: sinatra
27
+ requirement: &70275016992740 !ruby/object:Gem::Requirement
62
28
  none: false
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- hash: 15
67
- segments:
68
- - 1
69
- - 0
70
- version: "1.0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.2
71
33
  type: :runtime
72
- version_requirements: *id002
73
- - !ruby/object:Gem::Dependency
74
- name: minitest
75
34
  prerelease: false
76
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70275016992740
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &70275016991820 !ruby/object:Gem::Requirement
77
39
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- hash: 11
82
- segments:
83
- - 2
84
- - 0
85
- - 2
86
- version: 2.0.2
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.5.1
87
44
  type: :development
88
- version_requirements: *id003
89
- - !ruby/object:Gem::Dependency
90
- name: rubyforge
91
45
  prerelease: false
92
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70275016991820
47
+ - !ruby/object:Gem::Dependency
48
+ name: rubyforge
49
+ requirement: &70275016990980 !ruby/object:Gem::Requirement
93
50
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- hash: 7
98
- segments:
99
- - 2
100
- - 0
101
- - 4
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
102
54
  version: 2.0.4
103
55
  type: :development
104
- version_requirements: *id004
105
- - !ruby/object:Gem::Dependency
106
- name: hoe-doofus
107
56
  prerelease: false
108
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *70275016990980
58
+ - !ruby/object:Gem::Dependency
59
+ name: hoe-doofus
60
+ requirement: &70275016990220 !ruby/object:Gem::Requirement
109
61
  none: false
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- hash: 15
114
- segments:
115
- - 1
116
- - 0
117
- version: "1.0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '1.0'
118
66
  type: :development
119
- version_requirements: *id005
120
- - !ruby/object:Gem::Dependency
121
- name: hoe-seattlerb
122
67
  prerelease: false
123
- requirement: &id006 !ruby/object:Gem::Requirement
68
+ version_requirements: *70275016990220
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe-seattlerb
71
+ requirement: &70275016989440 !ruby/object:Gem::Requirement
124
72
  none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- hash: 11
129
- segments:
130
- - 1
131
- - 2
132
- version: "1.2"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '1.2'
133
77
  type: :development
134
- version_requirements: *id006
135
- - !ruby/object:Gem::Dependency
136
- name: hoe-git
137
78
  prerelease: false
138
- requirement: &id007 !ruby/object:Gem::Requirement
79
+ version_requirements: *70275016989440
80
+ - !ruby/object:Gem::Dependency
81
+ name: hoe-git
82
+ requirement: &70275016988560 !ruby/object:Gem::Requirement
139
83
  none: false
140
- requirements:
141
- - - ">="
142
- - !ruby/object:Gem::Version
143
- hash: 9
144
- segments:
145
- - 1
146
- - 3
147
- version: "1.3"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '1.3'
148
88
  type: :development
149
- version_requirements: *id007
150
- - !ruby/object:Gem::Dependency
151
- name: hoe-gemspec2
152
89
  prerelease: false
153
- requirement: &id008 !ruby/object:Gem::Requirement
90
+ version_requirements: *70275016988560
91
+ - !ruby/object:Gem::Dependency
92
+ name: hoe-gemspec2
93
+ requirement: &70275016987460 !ruby/object:Gem::Requirement
154
94
  none: false
155
- requirements:
156
- - - ">="
157
- - !ruby/object:Gem::Version
158
- hash: 15
159
- segments:
160
- - 1
161
- - 0
162
- version: "1.0"
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '1.0'
163
99
  type: :development
164
- version_requirements: *id008
165
- - !ruby/object:Gem::Dependency
166
- name: rdoc
167
100
  prerelease: false
168
- requirement: &id009 !ruby/object:Gem::Requirement
101
+ version_requirements: *70275016987460
102
+ - !ruby/object:Gem::Dependency
103
+ name: rdoc
104
+ requirement: &70275017002960 !ruby/object:Gem::Requirement
169
105
  none: false
170
- requirements:
171
- - - ">="
172
- - !ruby/object:Gem::Version
173
- hash: 3
174
- segments:
175
- - 0
176
- version: "0"
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
177
110
  type: :development
178
- version_requirements: *id009
179
- - !ruby/object:Gem::Dependency
180
- name: eventmachine
181
111
  prerelease: false
182
- requirement: &id010 !ruby/object:Gem::Requirement
112
+ version_requirements: *70275017002960
113
+ - !ruby/object:Gem::Dependency
114
+ name: eventmachine
115
+ requirement: &70275017001380 !ruby/object:Gem::Requirement
183
116
  none: false
184
- requirements:
185
- - - ">="
186
- - !ruby/object:Gem::Version
187
- hash: 57
188
- segments:
189
- - 0
190
- - 12
191
- - 11
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
192
120
  version: 0.12.11
193
121
  type: :development
194
- version_requirements: *id010
195
- - !ruby/object:Gem::Dependency
196
- name: hoe
197
122
  prerelease: false
198
- requirement: &id011 !ruby/object:Gem::Requirement
123
+ version_requirements: *70275017001380
124
+ - !ruby/object:Gem::Dependency
125
+ name: rdoc
126
+ requirement: &70275016999680 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: '3.10'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *70275016999680
135
+ - !ruby/object:Gem::Dependency
136
+ name: hoe
137
+ requirement: &70275016998740 !ruby/object:Gem::Requirement
199
138
  none: false
200
- requirements:
201
- - - ">="
202
- - !ruby/object:Gem::Version
203
- hash: 41
204
- segments:
205
- - 2
206
- - 9
207
- - 1
208
- version: 2.9.1
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: '2.13'
209
143
  type: :development
210
- version_requirements: *id011
211
- description: |-
212
- A Sinatra plugin to provide convenience whilst performing asynchronous
144
+ prerelease: false
145
+ version_requirements: *70275016998740
146
+ description: ! 'A Sinatra plugin to provide convenience whilst performing asynchronous
147
+
213
148
  responses inside of the Sinatra framework running under async webservers.
214
-
149
+
150
+
215
151
  To properly utilise this package, some knowledge of EventMachine and/or
152
+
216
153
  asynchronous patterns is recommended.
217
-
154
+
155
+
218
156
  Currently, supporting servers include:
219
-
157
+
158
+
220
159
  * Thin
160
+
221
161
  * Rainbows
222
- * Zbatery
223
- email:
162
+
163
+ * Zbatery'
164
+ email:
224
165
  - raggi@rubyforge.org
225
166
  executables: []
226
-
227
167
  extensions: []
228
-
229
- extra_rdoc_files:
168
+ extra_rdoc_files:
230
169
  - Manifest.txt
231
170
  - CHANGELOG.rdoc
232
171
  - README.rdoc
233
- files:
172
+ files:
234
173
  - .gemtest
235
174
  - CHANGELOG.rdoc
236
175
  - Manifest.txt
@@ -242,40 +181,32 @@ files:
242
181
  - lib/sinatra/async/test.rb
243
182
  - test/gemloader.rb
244
183
  - test/test_async.rb
245
- has_rdoc: true
246
184
  homepage: http://github.com/raggi/async_sinatra
247
185
  licenses: []
248
-
249
186
  post_install_message:
250
- rdoc_options:
187
+ rdoc_options:
251
188
  - --main
252
189
  - README.rdoc
253
- require_paths:
190
+ require_paths:
254
191
  - lib
255
- required_ruby_version: !ruby/object:Gem::Requirement
192
+ required_ruby_version: !ruby/object:Gem::Requirement
256
193
  none: false
257
- requirements:
258
- - - ">="
259
- - !ruby/object:Gem::Version
260
- hash: 3
261
- segments:
262
- - 0
263
- version: "0"
264
- required_rubygems_version: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ required_rubygems_version: !ruby/object:Gem::Requirement
265
199
  none: false
266
- requirements:
267
- - - ">="
268
- - !ruby/object:Gem::Version
269
- hash: 3
270
- segments:
271
- - 0
272
- version: "0"
200
+ requirements:
201
+ - - ! '>='
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
273
204
  requirements: []
274
-
275
205
  rubyforge_project: libraggi
276
- rubygems_version: 1.6.0
206
+ rubygems_version: 1.8.17
277
207
  signing_key:
278
208
  specification_version: 3
279
- summary: A Sinatra plugin to provide convenience whilst performing asynchronous responses inside of the Sinatra framework running under async webservers
280
- test_files:
209
+ summary: A Sinatra plugin to provide convenience whilst performing asynchronous responses
210
+ inside of the Sinatra framework running under async webservers
211
+ test_files:
281
212
  - test/test_async.rb
data.tar.gz.sig DELETED
@@ -1,3 +0,0 @@
1
- ?��ͫ�-��*����$}k�������@o^\�Z�!,|�Z��R�BA7 �^�$�<��%(��E� e���C�9Xߘ����v���V��~�yX��{3E�m�I`cn��®�S�$U>���p����U��� ;�1Q��'�s��/-C� z��5���6�Y�'���xۣ@q�z��K��P�xr�q\�0�`
2
- �� �bdYU~U�~:���+�/N
3
- ev2��3;�~.p�鹏�Ⱦ��A�@$!�
metadata.gz.sig DELETED
Binary file