webrick-route_servlet 1.1.1 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 397fa019560396894135b4744be9920b854e8214
4
- data.tar.gz: a5103f261531146f365564acb6de35529515eb2d
3
+ metadata.gz: 8c0162b442d50867355a76985facd0a6557453f6
4
+ data.tar.gz: 1e5b074844be091adeaf2bd27c9c5a5a4a9a57f3
5
5
  SHA512:
6
- metadata.gz: ce1e23750eefeb89db43af8ac9b7130915bbf222f613740c4668fa2c22e49d8ef39a3607e1d37898abe11a7b0bfe9c9486c4a8e53659d718fa2da978224d8a70
7
- data.tar.gz: e113ad6d9ba8e7d81371c04bb189a0e840b29e272be31e54715861ba812044cefe444ae5b6cb70d3c51f652ec1c0bc19fbc10da8b15a641d4cd9f1c18642dd06
6
+ metadata.gz: 9c634ba1c14372d967976930d112aec4ad1f57135eb8a2deb0f2fa524e67280d405d050064fb62fce55e7dc5b2a9e729023263073811f8015516b92ad6073e53
7
+ data.tar.gz: 2831c6281d71e3f2175838e3d0ee6196c093442af1f5078b140d6ef12db0d38dfa03018cc8ed6c374ed69fe0fd5e61810d0f4fe33bf1cdd4a09ab45a08081710
@@ -24,7 +24,7 @@ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
24
24
  def do_GET(req, res)
25
25
  res.content_type = "text/html"
26
26
  res.body = "<h2>NotFoundServlet</h2>"
27
- res.body += "<p>path: #{req.params["path"]}</p>"
27
+ res.body += "<p>path: #{req.params[:path]}</p>"
28
28
  res.body += "<a href='/'>index</a>"
29
29
  end
30
30
  end
@@ -34,11 +34,11 @@ class OpenSocialApiServlet < WEBrick::HTTPServlet::AbstractServlet
34
34
  res.content_type = "text/plain"
35
35
  res.body = JSON.pretty_generate({
36
36
  "servlet" => self.class.name,
37
- "type" => req.params["type"],
38
- "guid" => req.params["guid"],
39
- "selector" => req.params["selector"],
40
- "appid" => req.params["appid"],
41
- "resourceid" => req.params["resourceid"],
37
+ "type" => req.params[:type],
38
+ "guid" => req.params[:guid],
39
+ "selector" => req.params[:selector],
40
+ "appid" => req.params[:appid],
41
+ "resourceid" => req.params[:resourceid],
42
42
  })
43
43
  end
44
44
  end
@@ -48,7 +48,7 @@ class NotFoundApiServlet < WEBrick::HTTPServlet::AbstractServlet
48
48
  res.content_type = "text/plain"
49
49
  res.body = JSON.pretty_generate({
50
50
  "servlet" => self.class.name,
51
- "path" => req.params["path"],
51
+ "path" => req.params[:path],
52
52
  })
53
53
  end
54
54
  end
@@ -0,0 +1,99 @@
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>\n"
12
+ res.body += "<h4>UserServlet:</h4>\n"
13
+ res.body += create_link("GET", "/users", "index")
14
+ res.body += create_link("POST", "/users", "create")
15
+ res.body += create_link("GET", "/users/new", "new")
16
+ res.body += create_link("GET", "/users/1/edit", "edit")
17
+ res.body += create_link("GET", "/users/1", "show")
18
+ res.body += create_link("PUT", "/users/1", "update", true)
19
+ res.body += create_link("DELETE", "/users/1", "destroy", true)
20
+ res.body += "<br />"
21
+ res.body += "<h4>NotFoundServlet:</h4>"
22
+ res.body += "<a href='/no/match/path/route'>/no/match/path/route</a><br />"
23
+ end
24
+
25
+ def create_link(method, path, action, disabled=false)
26
+ disabled = disabled ? " disabled='disabled'" : ""
27
+ "<form action='#{path}' method='#{method}'>\n" +
28
+ "<input type='submit' value='#{method} #{path} => #{action}'#{disabled} />\n" +
29
+ "</form>\n"
30
+ end
31
+ end
32
+
33
+ class UserServlet < WEBrick::RouteServlet::ActionServlet
34
+ def index(req, res)
35
+ res.content_type = "text/html"
36
+ res.body = "<h2>RestServlet#index</h2>\n"
37
+ res.body += "<p>params: #{req.params}</p>\n"
38
+ res.body += "<p><a href='/'>index</a></p>\n"
39
+ end
40
+
41
+ def create(req, res)
42
+ res.content_type = "text/html"
43
+ res.body = "<h2>RestServlet#create</h2>\n"
44
+ res.body += "<p>params: #{req.params}</p>\n"
45
+ res.body += "<p><a href='/'>index</a></p>\n"
46
+ end
47
+
48
+ def new(req, res)
49
+ res.content_type = "text/html"
50
+ res.body = "<h2>RestServlet#new</h2>\n"
51
+ res.body += "<p>params: #{req.params}</p>\n"
52
+ res.body += "<p><a href='/'>index</a></p>\n"
53
+ end
54
+
55
+ def edit(req, res)
56
+ res.content_type = "text/html"
57
+ res.body = "<h2>RestServlet#edit</h2>\n"
58
+ res.body += "<p>params: #{req.params}</p>\n"
59
+ res.body += "<p><a href='/'>index</a></p>\n"
60
+ end
61
+
62
+ def show(req, res)
63
+ res.content_type = "text/html"
64
+ res.body = "<h2>RestServlet#show</h2>\n"
65
+ res.body += "<p>params: #{req.params}</p>\n"
66
+ res.body += "<p><a href='/'>index</a></p>\n"
67
+ end
68
+
69
+ def update(req, res)
70
+ res.content_type = "text/html"
71
+ res.body = "<h2>RestServlet#update</h2>\n"
72
+ res.body += "<p>params: #{req.params}</p>\n"
73
+ res.body += "<p><a href='/'>index</a></p>\n"
74
+ end
75
+
76
+ def destroy(req, res)
77
+ res.content_type = "text/html"
78
+ res.body = "<h2>RestServlet#destroy</h2>\n"
79
+ res.body += "<p>params: #{req.params}</p>\n"
80
+ res.body += "<p><a href='/'>index</a></p>\n"
81
+ end
82
+ end
83
+
84
+ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
85
+ def do_GET(req, res)
86
+ res.content_type = "text/html"
87
+ res.body = "<h2>NotFoundServlet</h2>"
88
+ res.body += "path: #{req.params[:path]}"
89
+ res.body += "<p><a href='/'>index</a></p>"
90
+ end
91
+ end
92
+
93
+ server = WEBrick::HTTPServer.new(:Port=>3000)
94
+ server.mount("/", WEBrick::RouteServlet.servlet{|s|
95
+ s.root IndexServlet
96
+ s.resources "/users", UserServlet
97
+ s.match "/*path", NotFoundServlet
98
+ })
99
+ server.start
@@ -27,11 +27,11 @@ end
27
27
  class ActionServlet < WEBrick::HTTPServlet::AbstractServlet
28
28
  def do_GET(req, res)
29
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 />"
30
+ res.body = "<h2>ActionServlet</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
35
  res.body += "<p><a href='/'>index</a></p>"
36
36
  end
37
37
  end
@@ -40,7 +40,7 @@ class NotFoundServlet < WEBrick::HTTPServlet::AbstractServlet
40
40
  def do_GET(req, res)
41
41
  res.content_type = "text/html"
42
42
  res.body = "<h2>NotFoundServlet</h2>"
43
- res.body += "path: #{req.params["path"]}"
43
+ res.body += "path: #{req.params[:path]}"
44
44
  res.body += "<p><a href='/'>index</a></p>"
45
45
  end
46
46
  end
@@ -1,5 +1,6 @@
1
1
  require 'webrick'
2
2
  require 'webrick/route_servlet/http_request'
3
+ require 'webrick/route_servlet/action_servlet'
3
4
  require "webrick/route_servlet/version"
4
5
 
5
6
  module WEBrick
@@ -7,13 +8,14 @@ module WEBrick
7
8
  def service(req, res)
8
9
  e = nil
9
10
 
10
- servlet, options, status = select_route(req)
11
+ req.extend WEBrick::RouteServlet::HTTPRequest
12
+ servlet, servlet_options = select_route(req)
11
13
 
12
14
  if servlet
13
- # 200 or 404
15
+ # 200
14
16
  begin
15
- res.status = status
16
- servlet.get_instance(@config, *options).service(req, res)
17
+ res.status = 200
18
+ servlet.get_instance(@config, *servlet_options).service(req, res)
17
19
  return
18
20
  rescue Exception => e
19
21
  end
@@ -21,9 +23,9 @@ module WEBrick
21
23
 
22
24
  # 500
23
25
  res.status = 500
24
- if self.class.route500
26
+ if self.class.error500
25
27
  begin
26
- self.class.route500.get_instance(@config).servlet(req, res)
28
+ self.class.error500.get_instance(@config).servlet(req, res)
27
29
  return
28
30
  rescue Exception => e
29
31
  end
@@ -39,31 +41,68 @@ module WEBrick
39
41
  end
40
42
 
41
43
  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 = Hash[md.names.zip(md.captures)]
47
- return [servlet, options, 200]
44
+ self.class.routes.each do |method, re, servlet, servlet_options, request_options|
45
+ if method==:* || method==req.request_method.to_sym
46
+ md = re.match(req.path_info)
47
+ if md
48
+ params = Hash[md.names.map(&:to_sym).zip(md.captures)]
49
+ params.delete_if{|k,v| v.nil?}
50
+ req.params = request_options.merge(params)
51
+ return [servlet, servlet_options]
52
+ end
48
53
  end
49
54
  end
50
55
  nil
51
56
  end
52
57
 
53
58
  module ClassMethods
54
- attr_accessor :route500
59
+ attr_accessor :error500
55
60
 
56
- def match(re, servlet, *options)
61
+ def match(re, servlet, *servlet_options, **request_options)
57
62
  @routes ||= []
58
- @routes << [normalize_path_re(re), servlet, options]
63
+ @routes << [:*, _normalize_path_re(re), servlet, servlet_options, request_options]
59
64
  end
60
65
 
61
- def root(servlet, *options)
66
+ def root(servlet, *servlet_options, **request_options)
62
67
  @routes ||= []
63
- @routes.unshift([normalize_path_re("/"), servlet, *options])
68
+ @routes.unshift([:*, _normalize_path_re("/"), servlet, servlet_options, request_options])
64
69
  end
65
70
 
66
- def normalize_path_re(re)
71
+ ["get", "post", "put", "delete"].each do |method|
72
+ class_eval %{
73
+ def #{method}(re, servlet, *servlet_options, **request_options)
74
+ @routes ||= []
75
+ @routes << [:#{method.upcase}, _normalize_path_re(re), servlet, servlet_options, request_options]
76
+ end
77
+ }
78
+ end
79
+
80
+ def resources(re, servlet, *servlet_options, **request_options)
81
+ re = re.to_s.sub(%r#/$#, "")
82
+ get "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :index})
83
+ post "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :create})
84
+ get "#{re}/new(.:format)", servlet, *servlet_options, request_options.merge({:action => :new})
85
+ get "#{re}/:id/edit(.:format)", servlet, *servlet_options, request_options.merge({:action => :edit})
86
+ get "#{re}/:id(.:format)", servlet, *servlet_options, request_options.merge({:action => :show})
87
+ put "#{re}/:id(.:format)", servlet, *servlet_options, request_options.merge({:action => :update})
88
+ delete "#{re}/:id(.:format)", servlet, *servlet_options, request_options.merge({:action => :destroy})
89
+ end
90
+
91
+ def resource(re, servlet, *servlet_options, **request_options)
92
+ re = re.to_s.sub(%r#/$#, "")
93
+ post "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :create})
94
+ get "#{re}/new(.:format)", servlet, *servlet_options, request_options.merge({:action => :new})
95
+ get "#{re}/edit(.:format)", servlet, *servlet_options, request_options.merge({:action => :edit})
96
+ get "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :show})
97
+ put "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :update})
98
+ delete "#{re}(.:format)", servlet, *servlet_options, request_options.merge({:action => :destroy})
99
+ end
100
+
101
+ def routes
102
+ @routes ||= []
103
+ end
104
+
105
+ def _normalize_path_re(re)
67
106
  unless Regexp===re
68
107
  # normalize slash
69
108
  re = re.to_s.gsub(%r#/{2,}#, "/")
@@ -81,10 +120,7 @@ module WEBrick
81
120
  end
82
121
  re
83
122
  end
84
-
85
- def routes
86
- @routes || []
87
- end
123
+ private :_normalize_path_re
88
124
  end
89
125
 
90
126
  class << self
@@ -0,0 +1,16 @@
1
+ module WEBrick
2
+ module RouteServlet
3
+ class ActionServlet < WEBrick::HTTPServlet::AbstractServlet
4
+ def service(req, res)
5
+ action = req.params[:action] rescue ""
6
+ action ||= ""
7
+
8
+ if respond_to?(action)
9
+ send(action, req, res)
10
+ else
11
+ raise RuntimeError, "action is not implemented: #{self.class}##{action}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module WEBrick
2
2
  module RouteServlet
3
- VERSION = "1.1.1"
3
+ VERSION = "1.2.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.1.1
4
+ version: 1.2.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-17 00:00:00.000000000 Z
11
+ date: 2013-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -51,8 +51,10 @@ files:
51
51
  - README.md
52
52
  - Rakefile
53
53
  - example/multi_routes.rb
54
+ - example/simple_action_servlet.rb
54
55
  - example/simple_routes.rb
55
56
  - lib/webrick/route_servlet.rb
57
+ - lib/webrick/route_servlet/action_servlet.rb
56
58
  - lib/webrick/route_servlet/http_request.rb
57
59
  - lib/webrick/route_servlet/version.rb
58
60
  - webrick-route_servlet.gemspec
@@ -76,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
78
  version: '0'
77
79
  requirements: []
78
80
  rubyforge_project:
79
- rubygems_version: 2.0.0
81
+ rubygems_version: 2.0.2
80
82
  signing_key:
81
83
  specification_version: 4
82
84
  summary: WEBrick::RouteServlet is like a Rails routes.rb. This servlet recognizes