webrick-route_servlet 1.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5aa5c1743a4152eddc48fe428cfeaff57abc8f6e
4
+ data.tar.gz: a0155a9bc5e559afc803494347769525426fcacc
5
+ SHA512:
6
+ metadata.gz: 35267f848162f28b8305161b2775065febd3e43b924a0f07701a3c4801c31f9c420feb0ff7291e181729ef19f8dcf35b9bc71dbed02794d36caa078d6bef29e2
7
+ data.tar.gz: 8f6faaa7cb0dbe580ec4df4c0bd9085f2de1a4bf2eb53413b9b9c5f35f3f8542519e4f51dd18c9ddb2a303299a04e902a2fe14fd14f2b317b62c4fb09e1657ef
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webrick-route_servlet.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yoshida Tetsuya
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # WEBrick::RouteServlet
2
+
3
+ WEBrick::RouteServlet is like a Rails routes.rb.
4
+ This servlet recognizes URLs and dispatches them to another servlet.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'webrick-route_servlet'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install webrick-route_servlet
19
+
20
+ ## Usage
21
+
22
+ server = WEBrick::HTTPServer.new(:Port=>3000)
23
+ server.mount("/", WEBrick::RouteServlet.servlet{|s|
24
+ s.root IndexServlet
25
+ s.match "/:page", PageServlet
26
+ s.match "/*path", NotFoundServlet
27
+ })
28
+ server.start
29
+
30
+ ## Example
31
+
32
+ https://github.com/yoshida-eth0/ruby-webrick-route_servlet/tree/master/example
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
4
+
5
+ require 'webrick'
6
+ require 'webrick/route_servlet'
7
+
8
+ class IndexServlet < WEBrick::HTTPServlet::AbstractServlet
9
+ def do_GET(req, res)
10
+ res.content_type = "text/html"
11
+ res.body = "<h2>IndexServlet</h2>"
12
+ res.body += "<h4>PageServlet:</h4>"
13
+ 3.times {
14
+ page = ("a".."z").to_a.shuffle[0..7].join
15
+ res.body += "<a href='/#{page}'>/#{page}</a><br />"
16
+ }
17
+ res.body += "<br />"
18
+ res.body += "<h4>NotFoundServlet:</h4>"
19
+ res.body += "<a href='/no/match/path'>/no/match/path</a><br />"
20
+ end
21
+ end
22
+
23
+ class PageServlet < WEBrick::HTTPServlet::AbstractServlet
24
+ def do_GET(req, res)
25
+ res.content_type = "text/html"
26
+ res.body = "<h2>PageServlet</h2>"
27
+ res.body += "<p>page: #{req.params["page"]}</p>"
28
+ res.body += "<a href='/'>index</a>"
29
+ end
30
+ end
31
+
32
+ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
33
+ def do_GET(req, res)
34
+ res.content_type = "text/html"
35
+ res.body = "<h2>NotFoundServlet</h2>"
36
+ res.body += "<p>path: #{req.params["path"]}</p>"
37
+ res.body += "<a href='/'>index</a>"
38
+ end
39
+ end
40
+
41
+ server = WEBrick::HTTPServer.new(:Port=>3000)
42
+ server.mount("/", WEBrick::RouteServlet.servlet{|s|
43
+ s.root IndexServlet
44
+ s.match "/:page", PageServlet
45
+ s.match "/*path", NotFoundServlet
46
+ })
47
+ server.start
@@ -0,0 +1,94 @@
1
+ require 'webrick'
2
+ require 'webrick/route_servlet/http_request'
3
+ require "webrick/route_servlet/version"
4
+
5
+ module WEBrick
6
+ module RouteServlet
7
+ def service(req, res)
8
+ e = nil
9
+
10
+ servlet, options, status = select_route(req)
11
+
12
+ if servlet
13
+ # 200 or 404
14
+ begin
15
+ res.status = status
16
+ servlet.get_instance(@config, *options).service(req, res)
17
+ return
18
+ rescue Exception => e
19
+ end
20
+ end
21
+
22
+ # 500
23
+ res.status = 500
24
+ if self.class.route500
25
+ begin
26
+ self.class.route500.get_instance(@config).servlet(req, res)
27
+ return
28
+ rescue Exception => e
29
+ end
30
+ end
31
+
32
+ if e
33
+ # default 500
34
+ res.body = "<h2>#{e.class}: #{e.message}</h2>\n<pre>#{e.backtrace.join("\n")}</pre>"
35
+ else
36
+ # routing error
37
+ res.body = "RoutingError: #{req.path_info}"
38
+ end
39
+ end
40
+
41
+ def select_route(req)
42
+ self.class.routes.each do |re, servlet, options|
43
+ md = re.match(req.path_info)
44
+ if md
45
+ req.extend WEBrick::RouteServlet::HTTPRequest
46
+ req.params = md
47
+ return [servlet, options, 200]
48
+ end
49
+ end
50
+ nil
51
+ end
52
+
53
+ module ClassMethods
54
+ attr_accessor :route500
55
+
56
+ def match(re, servlet, *options)
57
+ @routes ||= []
58
+ @routes << [normalize_path_re(re), servlet, options]
59
+ end
60
+
61
+ def root(servlet, *options)
62
+ @routes ||= []
63
+ @routes.unshift([normalize_path_re("/"), servlet, *options])
64
+ end
65
+
66
+ def normalize_path_re(re)
67
+ unless Regexp===re
68
+ re = re.to_s.gsub(%r#/{2,}#, "/").sub(%r#^/?#, "^/").sub(%r#/?$#, '/?$')
69
+ re = re.gsub(%r#/:([^/]+)#, '/(?<\1>[^/]+)')
70
+ re = re.gsub(%r#/\*([^/]+)#, '/(?<\1>.+?)')
71
+ re = Regexp.new(re)
72
+ end
73
+ re
74
+ end
75
+
76
+ def routes
77
+ @routes || []
78
+ end
79
+ end
80
+
81
+ class << self
82
+ def servlet
83
+ servlet = Class.new(WEBrick::HTTPServlet::AbstractServlet)
84
+ servlet.send(:include, RouteServlet)
85
+ yield servlet if block_given?
86
+ servlet
87
+ end
88
+
89
+ def included(klass)
90
+ klass.extend ClassMethods
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,13 @@
1
+ module WEBrick
2
+ module RouteServlet
3
+ module HTTPRequest
4
+ def params
5
+ @params
6
+ end
7
+
8
+ def params=(params)
9
+ @params = params
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module WEBrick
2
+ module RouteServlet
3
+ VERSION = "1.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webrick/route_servlet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webrick-route_servlet"
8
+ spec.version = WEBrick::RouteServlet::VERSION
9
+ spec.authors = ["Yoshida Tetsuya"]
10
+ spec.email = ["yoshida.eth0@gmail.com"]
11
+ spec.description = %q{WEBrick::RouteServlet is like a Rails routes.rb.}
12
+ spec.summary = %q{WEBrick::RouteServlet is like a Rails routes.rb. This servlet recognizes URLs and dispatches them to another servlet.}
13
+ spec.homepage = "https://github.com/yoshida-eth0/ruby-webrick-route_servlet"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webrick-route_servlet
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yoshida Tetsuya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: WEBrick::RouteServlet is like a Rails routes.rb.
42
+ email:
43
+ - yoshida.eth0@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - example/simple_server.rb
54
+ - lib/webrick/route_servlet.rb
55
+ - lib/webrick/route_servlet/http_request.rb
56
+ - lib/webrick/route_servlet/version.rb
57
+ - webrick-route_servlet.gemspec
58
+ homepage: https://github.com/yoshida-eth0/ruby-webrick-route_servlet
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.0
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: WEBrick::RouteServlet is like a Rails routes.rb. This servlet recognizes
82
+ URLs and dispatches them to another servlet.
83
+ test_files: []