webrick-route_servlet 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5aa5c1743a4152eddc48fe428cfeaff57abc8f6e
4
- data.tar.gz: a0155a9bc5e559afc803494347769525426fcacc
3
+ metadata.gz: 4acab3896a5966e52a287750419797607524db9f
4
+ data.tar.gz: 782551c377a6ad7b591b8b60a1e9d62649ff209f
5
5
  SHA512:
6
- metadata.gz: 35267f848162f28b8305161b2775065febd3e43b924a0f07701a3c4801c31f9c420feb0ff7291e181729ef19f8dcf35b9bc71dbed02794d36caa078d6bef29e2
7
- data.tar.gz: 8f6faaa7cb0dbe580ec4df4c0bd9085f2de1a4bf2eb53413b9b9c5f35f3f8542519e4f51dd18c9ddb2a303299a04e902a2fe14fd14f2b317b62c4fb09e1657ef
6
+ metadata.gz: 0202005cc2f708ad284fe629bc0c24d692d81b324391781f121a04b6fc6262f7d214e4901dc0df1fc8dc9ebb76ed4df230a277fbb63e72ddb7f7a9538199b72a
7
+ data.tar.gz: 0770f6cf8afef2a15377e714a55b0bff18b46cefcfcd58c82a604a5b63921e9ed3ed5c60791aa26fb559de360dd10b73a17fddd059e4413c6a503cd3fb4081fc
@@ -4,44 +4,58 @@ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
4
4
 
5
5
  require 'webrick'
6
6
  require 'webrick/route_servlet'
7
+ require 'json'
7
8
 
8
9
  class IndexServlet < WEBrick::HTTPServlet::AbstractServlet
9
10
  def do_GET(req, res)
10
11
  res.content_type = "text/html"
11
12
  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
- }
13
+ res.body += "<h4>XxxApiServlet:</h4>"
14
+ res.body += "<a href='/api/people/@me/@frields'>/api/people/@me/@frields</a><br />"
15
+ res.body += "<a href='/api/no/match/api'>/api/no/match/api</a><br />"
17
16
  res.body += "<br />"
18
17
  res.body += "<h4>NotFoundServlet:</h4>"
19
18
  res.body += "<a href='/no/match/path'>/no/match/path</a><br />"
20
19
  end
21
20
  end
22
21
 
23
- class PageServlet < WEBrick::HTTPServlet::AbstractServlet
22
+ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
24
23
  def do_GET(req, res)
25
24
  res.content_type = "text/html"
26
- res.body = "<h2>PageServlet</h2>"
27
- res.body += "<p>page: #{req.params["page"]}</p>"
25
+ res.body = "<h2>NotFoundServlet</h2>"
26
+ res.body += "<p>path: #{req.params["path"]}</p>"
28
27
  res.body += "<a href='/'>index</a>"
29
28
  end
30
29
  end
31
30
 
32
- class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
31
+ class PeopleApiServlet < WEBrick::HTTPServlet::AbstractServlet
33
32
  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>"
33
+ res.content_type = "text/plain"
34
+ res.body = JSON.pretty_generate({
35
+ "servlet" => self.class.name,
36
+ "guid" => req.params["guid"],
37
+ "selector" => req.params["selector"],
38
+ })
39
+ end
40
+ end
41
+
42
+ class NotFoundApiServlet < WEBrick::HTTPServlet::AbstractServlet
43
+ def do_GET(req, res)
44
+ res.content_type = "text/plain"
45
+ res.body = JSON.pretty_generate({
46
+ "servlet" => self.class.name,
47
+ "path" => req.params["path"],
48
+ })
38
49
  end
39
50
  end
40
51
 
41
52
  server = WEBrick::HTTPServer.new(:Port=>3000)
42
53
  server.mount("/", WEBrick::RouteServlet.servlet{|s|
43
54
  s.root IndexServlet
44
- s.match "/:page", PageServlet
45
55
  s.match "/*path", NotFoundServlet
46
56
  })
57
+ server.mount("/api", WEBrick::RouteServlet.servlet{|s|
58
+ s.match "/people/:guid/:selector", PeopleApiServlet
59
+ s.match "/*path", NotFoundApiServlet
60
+ })
47
61
  server.start
@@ -0,0 +1,54 @@
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>ActionServlet:</h4>"
13
+ 3.times {
14
+ controller = ["/users", "/posts"].shuffle[0]
15
+ action = rand(3)==0 ? "" : ["/show", "/edit"].shuffle[0]
16
+ id = action.empty? || rand(3)==0 ? "" : "/#{rand(255)+1}"
17
+ format = [".html", ""].shuffle[0]
18
+ path = controller + action + id + format
19
+ res.body += "<a href='#{path}'>#{path}</a><br />"
20
+ }
21
+ res.body += "<br />"
22
+ res.body += "<h4>NotFoundServlet:</h4>"
23
+ res.body += "<a href='/no/match/path/route'>/no/match/path/route</a><br />"
24
+ end
25
+ end
26
+
27
+ class ActionServlet < WEBrick::HTTPServlet::AbstractServlet
28
+ def do_GET(req, res)
29
+ res.content_type = "text/html"
30
+ res.body = "<h2>PageServlet</h2>"
31
+ res.body += "controller: #{req.params["controller"]}<br />"
32
+ res.body += "action: #{req.params["action"]}<br />"
33
+ res.body += "id: #{req.params["id"]}<br />"
34
+ res.body += "format: #{req.params["format"]}<br />"
35
+ res.body += "<p><a href='/'>index</a></p>"
36
+ end
37
+ end
38
+
39
+ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
40
+ def do_GET(req, res)
41
+ res.content_type = "text/html"
42
+ res.body = "<h2>NotFoundServlet</h2>"
43
+ res.body += "path: #{req.params["path"]}"
44
+ res.body += "<p><a href='/'>index</a></p>"
45
+ end
46
+ end
47
+
48
+ server = WEBrick::HTTPServer.new(:Port=>3000)
49
+ server.mount("/", WEBrick::RouteServlet.servlet{|s|
50
+ s.root IndexServlet
51
+ s.match '/:controller(/:action(/:id))(.:format)', ActionServlet
52
+ s.match "/*path", NotFoundServlet
53
+ })
54
+ server.start
@@ -43,7 +43,7 @@ module WEBrick
43
43
  md = re.match(req.path_info)
44
44
  if md
45
45
  req.extend WEBrick::RouteServlet::HTTPRequest
46
- req.params = md
46
+ req.params = Hash[md.names.zip(md.captures)]
47
47
  return [servlet, options, 200]
48
48
  end
49
49
  end
@@ -65,9 +65,18 @@ module WEBrick
65
65
 
66
66
  def normalize_path_re(re)
67
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>.+?)')
68
+ # normalize slash
69
+ re = re.to_s.gsub(%r#/{2,}#, "/")
70
+ # escape
71
+ re = re.gsub(/([\.\-?*+\\^$])/, '\\\\\1')
72
+ # start end regexp
73
+ re = re.sub(%r#^/?#, "^/").sub(%r#/?$#, '/?$')
74
+ # normalize parentheses
75
+ re = re.gsub(")", ")?")
76
+ # named capture
77
+ re = re.gsub(%r#/:([^/()\.]+)#, '/(?<\1>[^/]+?)')
78
+ re = re.gsub(%r#\.:([^/()\.]+)#, '.(?<\1>[^/]+?)')
79
+ re = re.gsub(%r#/\\\*([^/]+)#, '/(?<\1>.+?)')
71
80
  re = Regexp.new(re)
72
81
  end
73
82
  re
@@ -1,5 +1,5 @@
1
1
  module WEBrick
2
2
  module RouteServlet
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrick-route_servlet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshida Tetsuya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-16 00:00:00.000000000 Z
11
+ date: 2013-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -50,7 +50,8 @@ files:
50
50
  - LICENSE.txt
51
51
  - README.md
52
52
  - Rakefile
53
- - example/simple_server.rb
53
+ - example/multi_routes.rb
54
+ - example/simple_routes.rb
54
55
  - lib/webrick/route_servlet.rb
55
56
  - lib/webrick/route_servlet/http_request.rb
56
57
  - lib/webrick/route_servlet/version.rb