webrick-route_servlet 1.2.1 → 1.2.2

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: 1a1b0f4ab6a7f0d6c068df163c11cd01434e9511
4
- data.tar.gz: dd6622a22e81c2a4d99c99519af1e080491e6e3d
3
+ metadata.gz: ea2b3e3f91704a6122a1c5a3aa35e46c912142a3
4
+ data.tar.gz: 8f6b55aecfcf2a0ba62a0f40a5a5c96e2bb2fecc
5
5
  SHA512:
6
- metadata.gz: eb231b422ae59a9f130f6795d0868f0258c63bbcbed6660100f66acbd24e22e7a7264834db5b8e7637027a88a8a78fff60b64346bec8569723d735760170ea64
7
- data.tar.gz: 7c225b2f52c7aac4d728091a87348c392d1066bf50f30d52b22a56db91badb9439183b084cfc2ceb3402b200d5ae3d28a4062ef993838ca9c1af36dd8a21dcf5
6
+ metadata.gz: fe7b63371a13fc1a686a8aba3ac48c5d4dd6cb84bdab1a1cf3f28e0e58eabb5deea6d4bc5222ea3b37bd406d32e6a73a2a2f5db61ca17ff2efba2d05fca30bca
7
+ data.tar.gz: 65167911e10308ab4bbfa05f32dfa4fda801b7725b6bf523859c77d3537bbe4208d38602b32bb03727d13e209008838ccb5910c6d5f1edf4b1834e3fe3e29e00
data/README.md CHANGED
@@ -31,6 +31,49 @@ Or install it yourself as:
31
31
 
32
32
  https://github.com/yoshida-eth0/ruby-webrick-route_servlet/tree/master/example
33
33
 
34
+ ## Supported methods
35
+
36
+ match
37
+
38
+ s.match "/:controller(/:action(/:id))(.:format)", ActionServlet
39
+ s.match "/*path", NotFoundServlet
40
+
41
+ root
42
+
43
+ s.root IndexServlet
44
+
45
+ resources
46
+
47
+ s.resources "/photos", PhotoServlet
48
+
49
+ resource
50
+
51
+ s.resource "/profile", ProfileServlet
52
+
53
+ get / post / put / delete
54
+
55
+ s.get "/photos", PhotoServlet
56
+ s.post "/photos", PhotoServlet
57
+ s.put "/photos/123", PhotoServlet
58
+ s.delete "/photos/123", PhotoServlet
59
+
60
+ ## Supported options
61
+
62
+ via
63
+
64
+ s.match "/photos/show", PhotoServlet, :via => :get
65
+ s.match "/photos/show", PhotoServlet, :via => [:get, :post]
66
+
67
+ constraints
68
+
69
+ s.match "/photos/show", PhotoServlet, :constraints => { :id => /[A-Z]\d{5}/ }
70
+ s.match "/photos/show", PhotoServlet, :id => /[A-Z]\d{5}/
71
+
72
+ only / except
73
+
74
+ s.resources "/photo", PhotoServlet, :only => [:index]
75
+ s.resources "/photo", PhotoServlet, :except => [:index, :show]
76
+
34
77
  ## Contributing
35
78
 
36
79
  1. Fork it
@@ -47,7 +47,8 @@ module WEBrick
47
47
  if md
48
48
  params = Hash[md.names.map(&:to_sym).zip(md.captures)]
49
49
  params.delete_if{|k,v| v.nil?}
50
- req.params = request_options.merge(params)
50
+ req.action = params[:action] || request_options[:action]
51
+ req.params = params
51
52
  return [servlet, servlet_options]
52
53
  end
53
54
  end
@@ -60,19 +61,22 @@ module WEBrick
60
61
 
61
62
  def match(re, servlet, *servlet_options, **request_options)
62
63
  @routes ||= []
63
- @routes << [:*, _normalize_path_re(re), servlet, servlet_options, request_options]
64
+ re = _normalize_path_re(re, request_options)
65
+ _select_via(request_options).each do |via|
66
+ @routes << [via, re, servlet, servlet_options, request_options]
67
+ end
64
68
  end
65
69
 
66
70
  def root(servlet, *servlet_options, **request_options)
67
71
  @routes ||= []
68
- @routes.unshift([:*, _normalize_path_re("/"), servlet, servlet_options, request_options])
72
+ @routes.unshift([:*, _normalize_path_re("/", request_options), servlet, servlet_options, request_options])
69
73
  end
70
74
 
71
75
  ["get", "post", "put", "delete"].each do |method|
72
76
  class_eval %{
73
77
  def #{method}(re, servlet, *servlet_options, **request_options)
74
78
  @routes ||= []
75
- @routes << [:#{method.upcase}, _normalize_path_re(re), servlet, servlet_options, request_options]
79
+ @routes << [:#{method.upcase}, _normalize_path_re(re, request_options), servlet, servlet_options, request_options]
76
80
  end
77
81
  }
78
82
  end
@@ -118,7 +122,7 @@ module WEBrick
118
122
  @routes ||= []
119
123
  end
120
124
 
121
- def _normalize_path_re(re)
125
+ def _normalize_path_re(re, request_options)
122
126
  unless Regexp===re
123
127
  # normalize slash
124
128
  re = re.to_s.gsub(%r#/{2,}#, "/")
@@ -128,16 +132,28 @@ module WEBrick
128
132
  re = re.sub(%r#^/?#, "^/").sub(%r#/?$#, '/?$')
129
133
  # normalize parentheses
130
134
  re = re.gsub(")", ")?")
131
- # named capture
132
- re = re.gsub(%r#/:([^/()\.]+)#, '/(?<\1>[^/]+?)')
133
- re = re.gsub(%r#\.:([^/()\.]+)#, '.(?<\1>[^/]+?)')
134
- re = re.gsub(%r#/\\\*([^/]+)#, '/(?<\1>.+?)')
135
+ # constrain named capture
136
+ constraints = request_options[:constraints] || request_options
137
+ keys = re.scan(%r#:([^/()\.]+)#).map(&:first).sort{|a,b| b.length <=> a.length}
138
+ keys.each do |key|
139
+ value_re = Regexp.new("(?<#{key}>[^/]+?)")
140
+ if Regexp===constraints[key.to_sym]
141
+ value_re = /(?<#{key}>#{constraints[key.to_sym]})/
142
+ end
143
+ re = re.gsub(":#{key}", value_re.to_s)
144
+ end
135
145
  re = Regexp.new(re)
136
146
  end
137
147
  re
138
148
  end
139
149
  private :_normalize_path_re
140
150
 
151
+ def _select_via(request_options)
152
+ via = request_options[:via] || :*
153
+ via = [via] unless Array===via
154
+ a = via.map(&:to_sym).map(&:upcase)
155
+ end
156
+
141
157
  def _select_rest_actions(actions, request_options)
142
158
  actions
143
159
 
@@ -148,11 +164,11 @@ module WEBrick
148
164
  actions.select!{|k,v| only.include?(k)}
149
165
  end
150
166
 
151
- # expect
152
- if request_options[:expect]
153
- expect = request_options[:expect]
154
- expect = [expect] unless Array===expect
155
- actions.delete_if{|k,v| expect.include?(k)}
167
+ # except
168
+ if request_options[:except]
169
+ except = request_options[:except]
170
+ except = [except] unless Array===except
171
+ actions.delete_if{|k,v| except.include?(k)}
156
172
  end
157
173
 
158
174
  actions
@@ -2,13 +2,10 @@ module WEBrick
2
2
  module RouteServlet
3
3
  class ActionServlet < WEBrick::HTTPServlet::AbstractServlet
4
4
  def service(req, res)
5
- action = req.params[:action] rescue ""
6
- action ||= ""
7
-
8
- if respond_to?(action)
9
- send(action, req, res)
5
+ if respond_to?(req.action)
6
+ send(req.action, req, res)
10
7
  else
11
- raise RuntimeError, "action is not implemented: #{self.class}##{action}"
8
+ raise RuntimeError, "action is not implemented: #{self.class}##{req.action}"
12
9
  end
13
10
  end
14
11
  end
@@ -1,6 +1,7 @@
1
1
  module WEBrick
2
2
  module RouteServlet
3
3
  module HTTPRequest
4
+ attr_accessor :action
4
5
  attr_accessor :params
5
6
  end
6
7
  end
@@ -1,5 +1,5 @@
1
1
  module WEBrick
2
2
  module RouteServlet
3
- VERSION = "1.2.1"
3
+ VERSION = "1.2.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrick-route_servlet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshida Tetsuya
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  requirements: []
80
80
  rubyforge_project:
81
- rubygems_version: 2.0.2
81
+ rubygems_version: 2.0.0
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: WEBrick::RouteServlet is like a Rails routes.rb. This servlet recognizes