sinarey 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,125 +1,120 @@
1
- module Sinarey
2
- class Router
3
- def initialize(*args, &block)
4
- @notfound_app = lambda { |env| [404, {}, ['404']] }
5
- @apps = {}
6
- @turbo_routes = {}
7
- @routes = {}
8
- instance_eval(&block) if block
9
- build_routing_table
10
- end
11
-
12
- def call(env)
13
- route = env["PATH_INFO"]
14
- route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
15
-
16
- #pjax request support
17
- if route.chomp!('.pjax')
18
- env["IS_PJAX_REQUEST"] = true
19
- end
20
-
21
- if response = apps_route(env["REQUEST_METHOD"], route, env)
22
- response
23
- else
24
- @notfound_app.call(env)
25
- end
26
- end
27
-
28
- def mount(app)
29
- app_id = @apps.size + 1
30
- @apps[app_id] = app
31
- end
32
-
33
- def notfound(app)
34
- @notfound_app = app
35
- end
36
-
37
- private
38
-
39
- def build_routing_table
40
- @apps.each do |app_id, app|
41
- regedit_turbo_routes(app_id, app)
42
- regedit_basic_routes(app_id, app)
43
- end
44
- end
45
-
46
- def regedit_turbo_routes(app_id, app)
47
- return unless app.respond_to?(:turbo_routes)
48
- app.turbo_routes.each do |verb, routes|
49
- routes.each do |path, route|
50
- route.tap do |block_id|
51
- tmp = @turbo_routes[verb] ||= {}
52
- tmp[path] = [block_id, app_id] unless tmp[path]
53
- end
54
- end
55
- end
56
- end
57
-
58
- def regedit_basic_routes(app_id, app)
59
- return unless app.respond_to?(:routes)
60
- app.routes.each do |verb, routes|
61
- routes.each do |pattern, keys, conditions, block_id|
62
- (@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
63
- end
64
- end
65
- end
66
-
67
- case ENV["RACK_ENV"]
68
- when 'development'
69
-
70
- #development need support sinarey reloader.so here use dev logic.
71
- def apps_route(verb, path, env)
72
-
73
- #auto reload modified code
74
- @apps.each do |index,app|
75
- app.auto_reload if app.respond_to?(:auto_reload)
76
- end
77
-
78
- #rebuild route table
79
- @turbo_routes = {}
80
- @routes = {}
81
- build_routing_table
82
-
83
- if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
84
- turbo_route.tap do |block_id,app_id|
85
- env['sinarey.router'] = {type: :turbo, block_id: block_id}
86
- status, headers, response = @apps[app_id].call(env)
87
- return status, headers, response
88
- end
89
- elsif routes = @routes[verb]
90
- routes.each do |pattern, keys, conditions, block_id, app_id|
91
- if match = pattern.match(path)
92
- env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
93
- status, headers, response = @apps[app_id].call(env)
94
- return status, headers, response
95
- end
96
- end
97
- end
98
- nil
99
- end
100
-
101
- else
102
-
103
- def apps_route(verb, path, env)
104
- if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
105
- turbo_route.tap do |block_id,app_id|
106
- env['sinarey.router'] = {type: :turbo, block_id: block_id}
107
- status, headers, response = @apps[app_id].call(env)
108
- return status, headers, response
109
- end
110
- elsif routes = @routes[verb]
111
- routes.each do |pattern, keys, conditions, block_id, app_id|
112
- if match = pattern.match(path)
113
- env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
114
- status, headers, response = @apps[app_id].call(env)
115
- return status, headers, response
116
- end
117
- end
118
- end
119
- nil
120
- end
121
-
122
- end
123
-
124
- end
125
- end
1
+ module Sinarey
2
+ class Router
3
+ def initialize(*args, &block)
4
+ @notfound_app = lambda { |env| [404, {}, ['404']] }
5
+ @apps = {}
6
+ @turbo_routes = {}
7
+ @routes = {}
8
+ instance_eval(&block) if block
9
+ build_routing_table
10
+ end
11
+
12
+ def call(env)
13
+ route = env["PATH_INFO"]
14
+ route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
15
+
16
+ if response = apps_route(env["REQUEST_METHOD"], route, env)
17
+ response
18
+ else
19
+ @notfound_app.call(env)
20
+ end
21
+ end
22
+
23
+ def mount(app)
24
+ app_id = @apps.size + 1
25
+ @apps[app_id] = app
26
+ end
27
+
28
+ def notfound(app)
29
+ @notfound_app = app
30
+ end
31
+
32
+ private
33
+
34
+ def build_routing_table
35
+ @apps.each do |app_id, app|
36
+ regedit_turbo_routes(app_id, app)
37
+ regedit_basic_routes(app_id, app)
38
+ end
39
+ end
40
+
41
+ def regedit_turbo_routes(app_id, app)
42
+ return unless app.respond_to?(:turbo_routes)
43
+ app.turbo_routes.each do |verb, routes|
44
+ routes.each do |path, route|
45
+ route.tap do |block_id|
46
+ tmp = @turbo_routes[verb] ||= {}
47
+ tmp[path] = [block_id, app_id] unless tmp[path]
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def regedit_basic_routes(app_id, app)
54
+ return unless app.respond_to?(:routes)
55
+ app.routes.each do |verb, routes|
56
+ routes.each do |pattern, keys, conditions, block_id|
57
+ (@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
58
+ end
59
+ end
60
+ end
61
+
62
+ case ENV["RACK_ENV"]
63
+ when 'development'
64
+
65
+ #development need support sinarey reloader.so here use dev logic.
66
+ def apps_route(verb, path, env)
67
+
68
+ #auto reload modified code
69
+ @apps.each do |index,app|
70
+ app.auto_reload if app.respond_to?(:auto_reload)
71
+ end
72
+
73
+ #rebuild route table
74
+ @turbo_routes = {}
75
+ @routes = {}
76
+ build_routing_table
77
+
78
+ if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
79
+ turbo_route.tap do |block_id,app_id|
80
+ env['sinarey.router'] = {type: :turbo, block_id: block_id}
81
+ status, headers, response = @apps[app_id].call(env)
82
+ return status, headers, response
83
+ end
84
+ elsif routes = @routes[verb]
85
+ routes.each do |pattern, keys, conditions, block_id, app_id|
86
+ if match = pattern.match(path)
87
+ env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
88
+ status, headers, response = @apps[app_id].call(env)
89
+ return status, headers, response
90
+ end
91
+ end
92
+ end
93
+ nil
94
+ end
95
+
96
+ else
97
+
98
+ def apps_route(verb, path, env)
99
+ if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
100
+ turbo_route.tap do |block_id,app_id|
101
+ env['sinarey.router'] = {type: :turbo, block_id: block_id}
102
+ status, headers, response = @apps[app_id].call(env)
103
+ return status, headers, response
104
+ end
105
+ elsif routes = @routes[verb]
106
+ routes.each do |pattern, keys, conditions, block_id, app_id|
107
+ if match = pattern.match(path)
108
+ env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
109
+ status, headers, response = @apps[app_id].call(env)
110
+ return status, headers, response
111
+ end
112
+ end
113
+ end
114
+ nil
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+ end
@@ -1,3 +1,3 @@
1
- module Sinarey
2
- VERSION = '1.0.4'
3
- end
1
+ module Sinarey
2
+ VERSION = '1.0.5'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinarey
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-26 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -31,17 +31,17 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - lib/sinatra/sinarey_reloader.rb
35
- - lib/sinarey/version.rb
36
- - lib/sinarey/base.rb
37
- - lib/sinarey/router.rb
38
- - lib/sinarey.rb
34
+ - README.md
39
35
  - demo/app.rb
40
36
  - demo/app2.rb
41
- - demo/notfound.rb
42
37
  - demo/config.ru
38
+ - demo/notfound.rb
39
+ - lib/sinarey.rb
40
+ - lib/sinarey/base.rb
41
+ - lib/sinarey/router.rb
42
+ - lib/sinarey/version.rb
43
+ - lib/sinatra/sinarey_reloader.rb
43
44
  - sinarey.gemspec
44
- - README.md
45
45
  homepage: https://github.com/maymay25/sinarey
46
46
  licenses:
47
47
  - MIT
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  requirements: []
64
64
  rubyforge_project:
65
- rubygems_version: 2.0.14
65
+ rubygems_version: 2.4.5.1
66
66
  signing_key:
67
67
  specification_version: 4
68
68
  summary: Sinarey, use for large rack project.