sinatra-symphony 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Sinatra Symphony
2
+
3
+ A simple glue for running Sinatra apps along with em-synchrony.
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ require 'sinatra-symphony'
9
+ require 'em-synchrony/em-http'
10
+
11
+ class MyApp < Sinatra::Symphony
12
+ get '/' do
13
+ http = EM::HttpRequest.new('http://www.google.com').get(redirects: 0)
14
+ "google returned http status of #{http.response_header.status}"
15
+ end
16
+ end
17
+ ```
18
+
19
+ ```
20
+ $ ab -n1 -c1 http://127.0.0.1:3000/
21
+
22
+ <snip>
23
+ Total transferred: 137 bytes
24
+ HTML transferred: 34 bytes
25
+ Requests per second: 3.09 [#/sec] (mean)
26
+ Time per request: 323.539 [ms] (mean)
27
+ Time per request: 323.539 [ms] (mean, across all concurrent requests)
28
+ Transfer rate: 0.41 [Kbytes/sec] received
29
+
30
+ $ ab -n2 -c2 http://127.0.0.1:3000/
31
+
32
+ <snip>
33
+ Total transferred: 274 bytes
34
+ HTML transferred: 68 bytes
35
+ Requests per second: 5.36 [#/sec] (mean)
36
+ Time per request: 373.161 [ms] (mean)
37
+ Time per request: 186.581 [ms] (mean, across all concurrent requests)
38
+ Transfer rate: 0.72 [Kbytes/sec] received
39
+ ```
40
+
41
+ # See Also
42
+ [https://github.com/kyledrake/sinatra-synchrony](https://github.com/kyledrake/sinatra-synchrony)
43
+
44
+ # License
45
+ [Creative Commons Attribution - CC BY](http://creativecommons.org/licenses/by/3.0)
@@ -0,0 +1,52 @@
1
+ require 'sinatra/base'
2
+ require 'em-synchrony'
3
+ require 'async-rack'
4
+
5
+ module Sinatra
6
+ class Symphony < Base
7
+
8
+ %w(get put post delete).each do |name|
9
+ define_singleton_method(name) do |path, options = {}, &block|
10
+ route(name.upcase, path, options) {|*args| async_invoke(*args, &block)}
11
+ end
12
+ end
13
+
14
+ # defaults
15
+ disable :raise_errors, :show_exceptions
16
+
17
+ module Helpers
18
+ def async_exec args, block
19
+ catch(:halt) do
20
+ begin
21
+ instance_exec(*args, &block)
22
+ rescue ::Exception => boom
23
+ handle_exception!(boom)
24
+ ensure
25
+ filter! :after unless env['sinatra.static_file']
26
+ end
27
+ end
28
+ end
29
+
30
+ def async_invoke *args, &block
31
+ EM.synchrony do
32
+ res = async_exec(args, block)
33
+ res = [res] unless res.kind_of?(Array)
34
+
35
+ if Array === res && Fixnum === res.first
36
+ status(res.shift)
37
+ body(res.pop)
38
+ headers(*res)
39
+ else
40
+ body(res)
41
+ end
42
+
43
+ env['async.callback'].call response.finish
44
+ end
45
+
46
+ throw :async
47
+ end
48
+ end
49
+
50
+ include Helpers
51
+ end
52
+ end
@@ -0,0 +1 @@
1
+ require 'sinatra/symphony'
data/test/helper.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'em-synchrony/em-http'
4
+ require 'sinatra-symphony'
5
+ require 'thin'
6
+ require 'uri'
7
+
8
+ class MiniTest::Spec
9
+ def schedule app, port = 4000, &block
10
+ res = nil
11
+ server = fork do
12
+ capture_io do
13
+ Thin::Server.start('127.0.0.1', port, app)
14
+ end
15
+ end
16
+
17
+ wait_for_service_start(port)
18
+ schedule_in_thread { res = block.call URI.parse("http://127.0.0.1:#{port}") }
19
+ return res
20
+ ensure
21
+ Process.kill 'KILL', server rescue nil
22
+ end
23
+
24
+ def schedule_in_thread &block
25
+ EM.run do
26
+ EM.synchrony do
27
+ block.call
28
+ EM.stop
29
+ end
30
+ end
31
+ end
32
+
33
+ def wait_for_service_start port, timeout = 5
34
+ (timeout / 0.1).to_i.times.each do
35
+ sleep 0.1
36
+ break if TCPSocket.new('127.0.0.1', port) rescue nil
37
+ end
38
+ end
39
+ end
data/test/test_app.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ describe 'sinatra-symphony app' do
4
+ it 'should run' do
5
+ myapp = Class.new(Sinatra::Symphony) do
6
+ get '/hello' do
7
+ 'world'
8
+ end
9
+ end
10
+
11
+ http = schedule(myapp) {|base_uri| EM::HttpRequest.new((base_uri + '/hello').to_s).get}
12
+ assert http
13
+ assert_equal 200, http.response_header.status
14
+ assert_equal 'world', http.response
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-symphony
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bharanee Rathna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-synchrony
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: async-rack
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: thin
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: em-http-request
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: em-synchrony glue for sinatra
111
+ email:
112
+ - deepfryed@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - test/helper.rb
118
+ - test/test_app.rb
119
+ - lib/sinatra/symphony.rb
120
+ - lib/sinatra-symphony.rb
121
+ - README.md
122
+ homepage: http://github.com/deepfryed/sinatra-symphony
123
+ licenses: []
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.21
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: em-synchrony glue for sinatra
146
+ test_files: []