sinatra-symphony 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ require 'rack'
2
+ require 'async_rack'
3
+
4
+ module Rack
5
+ class Mapper
6
+ def initialize map
7
+ @map = remap(map)
8
+ end
9
+
10
+ def remap map
11
+ map.map {|path, app| [path, app.respond_to?(:new) ? app_instance(app, path) : app]}
12
+ .sort_by {|path, app| path.size}.reverse
13
+ .map {|path, app| [Regexp.new("^#{path.chomp('/')}(?:/(.*)|$)"), path, app]}
14
+ end
15
+
16
+ def call env
17
+ rest, path, app = find_mapping(env['PATH_INFO'])
18
+ app ? dispatch(app, env, rest, path) : not_found(env)
19
+ end
20
+
21
+ def app_instance app, path
22
+ app.new do |instance|
23
+ instance.instance_variable_set(:@rack_mapper_path, path)
24
+ end
25
+ end
26
+
27
+ def dispatch app, env, rest, path
28
+ app.call(
29
+ env.merge(
30
+ 'PATH_INFO' => rest,
31
+ 'SCRIPT_NAME' => ::File.join(env['SCRIPT_NAME'], path),
32
+ 'REQUEST_PATH' => ::File.join(env['SCRIPT_NAME'], path, rest),
33
+ )
34
+ )
35
+ end
36
+
37
+ def not_found env
38
+ [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["no application mounted at #{env['REQUEST_PATH']}"]]
39
+ end
40
+
41
+ def find_mapping path
42
+ @map.each do |re, location, app|
43
+ if re.match(path)
44
+ return ["/#{$1}", location, app]
45
+ end
46
+ end
47
+ return nil
48
+ end
49
+
50
+ class Logger < Rack::CommonLogger
51
+ def log env, status, header, began_at
52
+ env_path_info = env['PATH_INFO']
53
+ env['PATH_INFO'] = env['REQUEST_PATH'] || ::File.join(env['SCRIPT_NAME'], env['PATH_INFO'])
54
+ super
55
+ ensure
56
+ env['PATH_INFO'] = env_path_info
57
+ end
58
+ end # Logger
59
+
60
+ class AsyncLogger < AsyncRack::CommonLogger
61
+ def log env, status, header, began_at
62
+ env_path_info = env['PATH_INFO']
63
+ env['PATH_INFO'] = env['REQUEST_PATH'] || ::File.join(env['SCRIPT_NAME'], env['PATH_INFO'])
64
+ super
65
+ ensure
66
+ env['PATH_INFO'] = env_path_info
67
+ end
68
+ end # AsyncLogger
69
+ end # Mapper
70
+ end # Rack
@@ -0,0 +1,10 @@
1
+ require 'async_rack'
2
+
3
+ module Rack
4
+ class MapperLogger < AsyncRack::CommonLogger
5
+ def log env, status, header, began_at
6
+ env['PATH_INFO'] = env['REQUEST_PATH']
7
+ super
8
+ end
9
+ end
10
+ end
@@ -9,12 +9,26 @@ module Sinatra
9
9
  disable :raise_errors, :show_exceptions
10
10
 
11
11
  def invoke
12
- EM.synchrony do
12
+ if self.class.symphony[:exclude].include? @request.path_info
13
13
  super
14
- env['async.callback'].call response.finish
14
+ else
15
+ EM.synchrony do
16
+ super
17
+ env['async.callback'].call response.finish
18
+ end
19
+
20
+ throw :async
15
21
  end
22
+ end
23
+
24
+ def self.synchronous *paths
25
+ paths.each do |path|
26
+ symphony[:exclude] << path
27
+ end
28
+ end
16
29
 
17
- throw :async
30
+ def self.symphony
31
+ @symphony ||= Hash.new {|h, k| h[k] = []}
18
32
  end
19
33
  end # Symphony
20
34
  end # Sinatra
data/test/helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'minitest/autorun'
3
3
  require 'em-synchrony/em-http'
4
4
  require 'sinatra-symphony'
5
5
  require 'sinatra/symphony/test'
6
+ require 'rack/mapper'
6
7
 
7
8
  class MiniTest::Spec
8
9
  include Rack::Test::Methods
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ describe 'Rack::Mapper' do
4
+ def app
5
+ Sinatra::Symphony::Test.new(Rack::Mapper.new('/hello' => @myapp))
6
+ end
7
+
8
+ it 'should run' do
9
+ mylogs = StringIO.new
10
+ @myapp = Class.new(Sinatra::Symphony) do
11
+ use Rack::Mapper::AsyncLogger, mylogs
12
+ get '/world' do
13
+ [@rack_mapper_path, '/world']
14
+ end
15
+ end
16
+
17
+ get '/hello/world'
18
+ assert_equal 200, last_response.status
19
+ assert_equal '/hello/world', last_response.body
20
+
21
+ mylogs.rewind
22
+ assert_match %r{GET /hello/world}, mylogs.read
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-symphony
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,120 +9,120 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: sinatra
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
20
+ none: false
22
21
  type: :runtime
23
- prerelease: false
22
+ name: sinatra
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ! '>='
28
26
  - !ruby/object:Gem::Version
29
27
  version: '0'
28
+ none: false
29
+ prerelease: false
30
30
  - !ruby/object:Gem::Dependency
31
- name: em-synchrony
32
31
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
32
  requirements:
35
33
  - - ! '>='
36
34
  - !ruby/object:Gem::Version
37
35
  version: '0'
36
+ none: false
38
37
  type: :runtime
39
- prerelease: false
38
+ name: em-synchrony
40
39
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
40
  requirements:
43
41
  - - ! '>='
44
42
  - !ruby/object:Gem::Version
45
43
  version: '0'
44
+ none: false
45
+ prerelease: false
46
46
  - !ruby/object:Gem::Dependency
47
- name: async-rack
48
47
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
48
  requirements:
51
49
  - - ! '>='
52
50
  - !ruby/object:Gem::Version
53
51
  version: '0'
52
+ none: false
54
53
  type: :runtime
55
- prerelease: false
54
+ name: async-rack
56
55
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
56
  requirements:
59
57
  - - ! '>='
60
58
  - !ruby/object:Gem::Version
61
59
  version: '0'
60
+ none: false
61
+ prerelease: false
62
62
  - !ruby/object:Gem::Dependency
63
- name: rake
64
63
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
64
  requirements:
67
65
  - - ! '>='
68
66
  - !ruby/object:Gem::Version
69
67
  version: '0'
68
+ none: false
70
69
  type: :development
71
- prerelease: false
70
+ name: rake
72
71
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
72
  requirements:
75
73
  - - ! '>='
76
74
  - !ruby/object:Gem::Version
77
75
  version: '0'
76
+ none: false
77
+ prerelease: false
78
78
  - !ruby/object:Gem::Dependency
79
- name: thin
80
79
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
80
  requirements:
83
81
  - - ! '>='
84
82
  - !ruby/object:Gem::Version
85
83
  version: '0'
84
+ none: false
86
85
  type: :development
87
- prerelease: false
86
+ name: thin
88
87
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
88
  requirements:
91
89
  - - ! '>='
92
90
  - !ruby/object:Gem::Version
93
91
  version: '0'
92
+ none: false
93
+ prerelease: false
94
94
  - !ruby/object:Gem::Dependency
95
- name: rack-test
96
95
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
96
  requirements:
99
97
  - - ! '>='
100
98
  - !ruby/object:Gem::Version
101
99
  version: '0'
100
+ none: false
102
101
  type: :development
103
- prerelease: false
102
+ name: rack-test
104
103
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
104
  requirements:
107
105
  - - ! '>='
108
106
  - !ruby/object:Gem::Version
109
107
  version: '0'
108
+ none: false
109
+ prerelease: false
110
110
  - !ruby/object:Gem::Dependency
111
- name: em-http-request
112
111
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
112
  requirements:
115
113
  - - ! '>='
116
114
  - !ruby/object:Gem::Version
117
115
  version: '0'
116
+ none: false
118
117
  type: :development
119
- prerelease: false
118
+ name: em-http-request
120
119
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
120
  requirements:
123
121
  - - ! '>='
124
122
  - !ruby/object:Gem::Version
125
123
  version: '0'
124
+ none: false
125
+ prerelease: false
126
126
  description: em-synchrony glue for sinatra.
127
127
  email:
128
128
  - deepfryed@gmail.com
@@ -130,11 +130,14 @@ executables: []
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - test/test_mapper.rb
133
134
  - test/helper.rb
134
135
  - test/test_app.rb
135
136
  - lib/sinatra/symphony/test.rb
136
137
  - lib/sinatra/symphony.rb
137
138
  - lib/sinatra-symphony.rb
139
+ - lib/rack/urlmap_logger.rb
140
+ - lib/rack/mapper.rb
138
141
  - README.md
139
142
  homepage: http://github.com/deepfryed/sinatra-symphony
140
143
  licenses: []
@@ -143,17 +146,20 @@ rdoc_options: []
143
146
  require_paths:
144
147
  - lib
145
148
  required_ruby_version: !ruby/object:Gem::Requirement
146
- none: false
147
149
  requirements:
148
150
  - - ! '>='
149
151
  - !ruby/object:Gem::Version
152
+ hash: -2775563060799593485
150
153
  version: '0'
151
- required_rubygems_version: !ruby/object:Gem::Requirement
154
+ segments:
155
+ - 0
152
156
  none: false
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
158
  requirements:
154
159
  - - ! '>='
155
160
  - !ruby/object:Gem::Version
156
161
  version: '0'
162
+ none: false
157
163
  requirements: []
158
164
  rubyforge_project:
159
165
  rubygems_version: 1.8.24