sinarey 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
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
+ if response = apps_route(env["REQUEST_METHOD"], route, env)
16
+ response
17
+ else
18
+ @notfound_app.call(env)
19
+ end
20
+ end
21
+
22
+ def mount(app)
23
+ app_id = @apps.size + 1
24
+ @apps[app_id] = app
25
+ end
26
+
27
+ def notfound(app)
28
+ @notfound_app = app
29
+ end
30
+
31
+ private
32
+
33
+ def build_routing_table
34
+ @apps.each do |app_id, app|
35
+ regedit_turbo_routes(app_id, app)
36
+ regedit_basic_routes(app_id, app)
37
+ end
38
+ end
39
+
40
+ def regedit_turbo_routes(app_id, app)
41
+ return unless app.respond_to?(:turbo_routes)
42
+ app.turbo_routes.each do |verb, routes|
43
+ routes.each do |path, route|
44
+ route.tap do |block_id|
45
+ tmp = @turbo_routes[verb] ||= {}
46
+ tmp[path] = [block_id, app_id] unless tmp[path]
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def regedit_basic_routes(app_id, app)
53
+ return unless app.respond_to?(:routes)
54
+ app.routes.each do |verb, routes|
55
+ routes.each do |pattern, keys, conditions, block_id|
56
+ (@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
57
+ end
58
+ end
59
+ end
60
+
61
+ def apps_route(verb, path, env)
62
+ if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
63
+ turbo_route.tap do |block_id,app_id|
64
+ env['sinarey.router'] = {type: :turbo, block_id: block_id}
65
+ status, headers, response = @apps[app_id].call(env)
66
+ return status, headers, response
67
+ end
68
+ elsif routes = @routes[verb]
69
+ routes.each do |pattern, keys, conditions, block_id, app_id|
70
+ if match = pattern.match(path)
71
+ env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
72
+ status, headers, response = @apps[app_id].call(env)
73
+ return status, headers, response
74
+ end
75
+ end
76
+ end
77
+ nil
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Sinarey
2
+ VERSION = '1.0.0'
3
+ end
data/lib/sinarey.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sinarey/base'
data/sinarey.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH.unshift('lib')
3
+ require 'sinarey/version'
4
+
5
+ Gem::Specification.new 'sinarey',Sinarey::VERSION do |spec|
6
+ spec.authors = ["Jeffrey"]
7
+ spec.email = ["jeffrey6052@163.com"]
8
+ spec.description = "add turbo_routes and a fast multi module router for sinatra."
9
+ spec.summary = "Sinarey, use for large rack project."
10
+ spec.homepage = "https://github.com/maymay25/sinarey"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = ['lib/sinarey/version.rb',
14
+ 'lib/sinarey/base.rb',
15
+ 'lib/sinarey/router.rb',
16
+ 'lib/sinarey.rb',
17
+ 'demo/app.rb',
18
+ 'demo/app2.rb',
19
+ 'demo/notfound.rb',
20
+ 'demo/config.ru',
21
+ 'sinarey.gemspec',
22
+ 'README.md']
23
+ spec.add_dependency 'sinatra', '1.4.4'
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinarey
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.4
27
+ description: add turbo_routes and a fast multi module router for sinatra.
28
+ email:
29
+ - jeffrey6052@163.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/sinarey/version.rb
35
+ - lib/sinarey/base.rb
36
+ - lib/sinarey/router.rb
37
+ - lib/sinarey.rb
38
+ - demo/app.rb
39
+ - demo/app2.rb
40
+ - demo/notfound.rb
41
+ - demo/config.ru
42
+ - sinarey.gemspec
43
+ - README.md
44
+ homepage: https://github.com/maymay25/sinarey
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.14
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Sinarey, use for large rack project.
68
+ test_files: []