apiary 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- apiary (0.0.2)
4
+ apiary (0.0.4)
5
5
  callsite
6
- em-http-request
7
6
  http_router
8
7
  method-args
9
8
  rack
10
9
  thin
10
+ thin_async
11
11
 
12
12
  GEM
13
13
  remote: http://rubygems.org/
@@ -39,6 +39,8 @@ GEM
39
39
  daemons (>= 1.0.9)
40
40
  eventmachine (>= 0.12.6)
41
41
  rack (>= 1.0.0)
42
+ thin_async (0.1.1)
43
+ thin (>= 1.2.1)
42
44
  url_mount (0.2.1)
43
45
  rack
44
46
 
@@ -55,3 +57,4 @@ DEPENDENCIES
55
57
  rack
56
58
  rake
57
59
  thin
60
+ thin_async
@@ -35,4 +35,16 @@ And you'll get back
35
35
 
36
36
  74.21
37
37
 
38
- Currently, `get`, `post`, `put` and `delete` are supported. You can also supply a path after any verb to have a custom path. Also, the current Rack env hash is available under `rack_env` if you need to take a look at the current request.
38
+ Currently, `get`, `post`, `put` and `delete` are supported. You can also supply a path after any verb to have a custom path. Also, the current Rack env hash is available under `rack_env` if you need to take a look at the current request.
39
+
40
+ ## Async Usage
41
+
42
+ If you want to use this in async mode, put an `a` in front of the method annotation. For instance
43
+
44
+ aget
45
+ def c2f(val)
46
+ EM.add_timer(0.1) do
47
+ async_response << "The temperature is :"
48
+ async_response.end(Float(val) * 9 / 5 + 32)
49
+ end
50
+ end
@@ -24,8 +24,8 @@ Gem::Specification.new do |s|
24
24
  s.add_dependency 'thin'
25
25
  s.add_dependency 'http_router'
26
26
  s.add_dependency 'rack'
27
- s.add_dependency 'em-http-request'
27
+ s.add_dependency 'thin_async'
28
28
  s.add_development_dependency 'rake'
29
+ s.add_development_dependency 'em-http-request'
29
30
  s.add_development_dependency 'minitest', '~> 2.0.0'
30
-
31
31
  end
@@ -4,45 +4,41 @@ require 'http_router'
4
4
  require 'thin'
5
5
  require 'rack'
6
6
  require 'apiary/version'
7
+ require 'apiary/api_method'
8
+ require 'apiary/async_response'
7
9
 
8
10
  module Apiary
9
- ApiMethod = Struct.new(:method, :http_method, :path)
10
11
 
11
- attr_accessor :rack_env
12
+ attr_accessor :rack_env, :async_response
12
13
 
13
14
  module ClassMethods
14
15
 
15
- def get(path = nil)
16
- __set_routing(:get, path)
17
- end
18
-
19
- def post(path = nil)
20
- __set_routing(:get, path)
21
- end
22
-
23
- def put(path = nil)
24
- __set_routing(:put, path)
25
- end
16
+ [:get, :post, :put, :delete].each do |m|
17
+ class_eval "
18
+ def a#{m}(path=nil)
19
+ __set_routing(:get, path, true)
20
+ end
26
21
 
27
- def delete(path = nil)
28
- __set_routing(:put, path)
22
+ def #{m}(path=nil)
23
+ __set_routing(:get, path)
24
+ end
25
+ "
29
26
  end
30
27
 
31
28
  def version(number)
32
29
  @version = number
33
30
  end
34
31
 
35
- def __set_routing(method, path)
36
- @http_method, @path = method, path
32
+ def __set_routing(method, path, async = false)
33
+ @http_method, @path, @async = method, path, async
37
34
  end
38
35
 
39
36
  def method_added(m)
40
37
  if @http_method
41
38
  MethodArgs.register(Callsite.parse(caller.first).filename)
42
39
  @cmds ||= []
43
- @cmds << ApiMethod.new(m, @http_method, @path)
44
- @http_method = nil
45
- @path = nil
40
+ @cmds << ApiMethod.new(m, @http_method, @path, @async)
41
+ @http_method, @path, @async = nil, nil, nil
46
42
  end
47
43
  end
48
44
 
@@ -65,7 +61,19 @@ module Apiary
65
61
  route.to { |env|
66
62
  instance = (blk ? blk.call : new)
67
63
  instance.rack_env = env
68
- Rack::Response.new(instance.send(cmd.method, *env['router.response'].param_values).to_s).finish
64
+ response = AsyncResponse.new(env)
65
+ if cmd.async?
66
+ instance.async_response = response
67
+ instance.send(cmd.method, *env['router.response'].param_values)
68
+ else
69
+ EM.defer(proc{
70
+ response.status = 200
71
+ response << instance.send(cmd.method, *env['router.response'].param_values).to_s
72
+ }, proc{
73
+ response.done
74
+ })
75
+ end
76
+ response.finish
69
77
  }
70
78
  end
71
79
  router
@@ -0,0 +1,5 @@
1
+ module Apiary
2
+ class ApiMethod < Struct.new(:method, :http_method, :path, :async)
3
+ alias_method :async?, :async
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'thin/async'
2
+
3
+ module Apiary
4
+ class AsyncResponse < Thin::AsyncResponse
5
+ def end(out = nil)
6
+ self.<<(out.to_s) if out
7
+ done
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Apiary
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,21 @@
1
+ class BasicAsync
2
+ include Apiary
3
+
4
+ version '1.0'
5
+
6
+ def initialize(var = nil)
7
+ @var = var
8
+ end
9
+
10
+ aget
11
+ def ping
12
+ EM.add_timer(0.1) do
13
+ async_response.end 'ping'
14
+ end
15
+ end
16
+
17
+ aget
18
+ def var
19
+ async_response.end @var
20
+ end
21
+ end
@@ -1,6 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'em-http'
3
3
  require 'test/fixtures/basic'
4
+ require 'test/fixtures/basic_async'
4
5
 
5
6
  class MiniTest::Unit::TestCase
6
7
  def run_with(cls, optional_blk = nil, &blk)
@@ -0,0 +1,30 @@
1
+ class TestAsync < MiniTest::Unit::TestCase
2
+ def test_simple
3
+ run_with(BasicAsync) do
4
+ request('/1.0/ping') do |http|
5
+ assert_equal 200, http.response_header.status
6
+ assert_equal 'ping', http.response
7
+ done
8
+ end
9
+ end
10
+ end
11
+
12
+ def test_not_found
13
+ run_with(BasicAsync) do
14
+ request('/1.0/something_else') do |http|
15
+ assert_equal 404, http.response_header.status
16
+ done
17
+ end
18
+ end
19
+ end
20
+
21
+ def test_custom_initializer
22
+ run_with(BasicAsync, proc{ BasicAsync.new('hi there')}) do
23
+ request('/1.0/var') do |http|
24
+ assert_equal 200, http.response_header.status
25
+ assert_equal 'hi there', http.response
26
+ done
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apiary
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 -08:00
18
+ date: 2011-01-18 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -89,7 +89,7 @@ dependencies:
89
89
  type: :runtime
90
90
  version_requirements: *id005
91
91
  - !ruby/object:Gem::Dependency
92
- name: em-http-request
92
+ name: thin_async
93
93
  prerelease: false
94
94
  requirement: &id006 !ruby/object:Gem::Requirement
95
95
  none: false
@@ -117,9 +117,23 @@ dependencies:
117
117
  type: :development
118
118
  version_requirements: *id007
119
119
  - !ruby/object:Gem::Dependency
120
- name: minitest
120
+ name: em-http-request
121
121
  prerelease: false
122
122
  requirement: &id008 !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ type: :development
132
+ version_requirements: *id008
133
+ - !ruby/object:Gem::Dependency
134
+ name: minitest
135
+ prerelease: false
136
+ requirement: &id009 !ruby/object:Gem::Requirement
123
137
  none: false
124
138
  requirements:
125
139
  - - ~>
@@ -131,7 +145,7 @@ dependencies:
131
145
  - 0
132
146
  version: 2.0.0
133
147
  type: :development
134
- version_requirements: *id008
148
+ version_requirements: *id009
135
149
  description: Convert your existing class into an EM-based API.
136
150
  email:
137
151
  - joshbuddy@gmail.com
@@ -149,9 +163,13 @@ files:
149
163
  - Rakefile
150
164
  - apiary.gemspec
151
165
  - lib/apiary.rb
166
+ - lib/apiary/api_method.rb
167
+ - lib/apiary/async_response.rb
152
168
  - lib/apiary/version.rb
153
169
  - test/fixtures/basic.rb
170
+ - test/fixtures/basic_async.rb
154
171
  - test/helper.rb
172
+ - test/test_async.rb
155
173
  - test/test_basic.rb
156
174
  has_rdoc: true
157
175
  homepage: https://github.com/joshbuddy/apiary
@@ -189,5 +207,7 @@ specification_version: 3
189
207
  summary: Convert your existing class into an EM-based API
190
208
  test_files:
191
209
  - test/fixtures/basic.rb
210
+ - test/fixtures/basic_async.rb
192
211
  - test/helper.rb
212
+ - test/test_async.rb
193
213
  - test/test_basic.rb