pancake 0.1.22 → 0.1.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -116,6 +116,7 @@ module Pancake
116
116
  # @api private
117
117
  def self.reset_mime_types!
118
118
  # Setup the mime types based on the rack mime types
119
+ Type.new("any")
119
120
  Rack::Mime::MIME_TYPES.each do |ext, type|
120
121
  ext =~ /\.(.*)$/
121
122
  e = $1
@@ -166,9 +167,22 @@ module Pancake
166
167
  accepted_type = nil
167
168
 
168
169
  if accepted_types.include?("*/*")
169
- name = provided.first
170
+ if provided.include?(:any)
171
+ name = :any
172
+ if at = accepted_types.detect{|t| t != "*/*"}
173
+ at
174
+ elsif t = provided.detect{|a| a != :any}
175
+ at = group(t).first.type_strings.first
176
+ else
177
+ at = "text/html"
178
+ end
179
+ else
180
+ name = provided.first
181
+ accepted_type = group(name).first
182
+ at = accepted_type.type_strings.first
183
+ end
170
184
  accepted_type = group(name).first
171
- negotiated_accept_types[key] = [name, accepted_type.type_strings.first, accepted_type]
185
+ negotiated_accept_types[key] = [name, at, accepted_type]
172
186
  return negotiated_accept_types[key]
173
187
  end
174
188
 
@@ -177,14 +191,23 @@ module Pancake
177
191
  provided.flatten.each do |name|
178
192
  accepted_type = match_content_type(at, name)
179
193
  if accepted_type
194
+ if name == :any
195
+ if at = accepted_types.detect{|a| a != "*/*"}
196
+ return [name, at, accepted_type]
197
+ elsif at = provided.flatten.detect{|p| p != :any}
198
+ return [name, at.type_strings.first, accepted_type]
199
+ else
200
+ return [name, "text/html", accept_type]
201
+ end
202
+ end
180
203
  at = accepted_type.type_strings.first if at == "*/*"
181
204
  if accepted_types.join.size > 4096
182
- # Don't save the key if it's larger than 4 k.
183
- # This could hit a dos attack if it's repeatedly hit
184
- # with anything large
185
- negotiated_accept_types[key] = [name, at, accepted_type]
186
- end
187
- return [name, at, accepted_type]
205
+ # Don't save the key if it's larger than 4 k.
206
+ # This could hit a dos attack if it's repeatedly hit
207
+ # with anything large
208
+ negotiated_accept_types[key] = [name, at, accepted_type]
209
+ end
210
+ return [name, at, accepted_type]
188
211
  end
189
212
  end
190
213
  end
@@ -213,6 +236,13 @@ module Pancake
213
236
 
214
237
  result = nil
215
238
  provided.each do |name|
239
+ if name == :any
240
+ at = group(ext.to_sym).first
241
+ return nil if at.nil?
242
+ result = [name, at.type_strings.first, at]
243
+ negotiated_accept_types[key] = result
244
+ return result
245
+ end
216
246
  group(name).each do |type|
217
247
  if type.extension == ext
218
248
  result = [name, type.type_strings.first, type]
@@ -249,7 +279,11 @@ module Pancake
249
279
  # @api private
250
280
  def self.match_content_type(accept_type, key)
251
281
  group(key).detect do |t|
252
- t.type_strings.include?(accept_type) || accept_type == "*/*"
282
+ if key == :any
283
+ true
284
+ else
285
+ t.type_strings.include?(accept_type) || accept_type == "*/*"
286
+ end
253
287
  end
254
288
  end
255
289
 
@@ -115,6 +115,12 @@ module Pancake
115
115
  define_published_action(:delete, path, opts, block)
116
116
  end
117
117
 
118
+ # Matches any method to the route
119
+ # @api public
120
+ def self.any(path, opts={}, &block)
121
+ define_published_action(:any, path, opts, block)
122
+ end
123
+
118
124
  private
119
125
  # Defines an action on the inner Controller class of this stack.
120
126
  # Also sets it as published if it's not already published.
@@ -129,7 +135,6 @@ module Pancake
129
135
 
130
136
  action_name = controller_method_name(method,path)
131
137
  attach_action(action_name, block)
132
-
133
138
  attach_route(method, path, action_name, opts)
134
139
  end
135
140
 
@@ -156,7 +161,7 @@ module Pancake
156
161
  def self.attach_route(method, path, action_name, options)
157
162
  name = options.delete(:_name)
158
163
  options[:conditions] ||= {}
159
- options[:conditions][:request_method] = method.to_s.upcase
164
+ options[:conditions][:request_method] = method.to_s.upcase unless method == :any
160
165
  options[:default_values] ||= {}
161
166
  options[:default_values][:action] = action_name
162
167
  r = router.add(path, options)
@@ -166,7 +166,6 @@ describe Pancake::MimeTypes do
166
166
  end
167
167
 
168
168
  it "should use */* if avaiable" do
169
- # safari uses application/xml first
170
169
  accept_type = "application/xml,*/*"
171
170
  group, at, r = Pancake::MimeTypes.negotiate_accept_type(accept_type, :html, :xml)
172
171
  at.should == "text/html"
@@ -174,6 +173,39 @@ describe Pancake::MimeTypes do
174
173
  r.should_not be_nil
175
174
  end
176
175
 
176
+ describe "matching on :any" do
177
+ it "should use match :any for a request" do
178
+ accept_type = "text/xml,*/*"
179
+ group, at, r = Pancake::MimeTypes.negotiate_accept_type(accept_type, :any)
180
+ group.should == :any
181
+ at.should == "text/xml"
182
+ r.should_not be_nil
183
+ end
184
+
185
+ it "should match :any for a request with a defualt given" do
186
+ accept_types = "*/*"
187
+ group, at, r = Pancake::MimeTypes.negotiate_accept_type(accept_types, :any, :json)
188
+ group.should == :any
189
+ at.should == "application/json"
190
+ r.should_not be_nil
191
+ end
192
+
193
+ it "should allow a specified mime type when a default is set" do
194
+ accept_types = "text/plain"
195
+ group, at, r = Pancake::MimeTypes.negotiate_accept_type(accept_types, :any, :json)
196
+ group.should == :any
197
+ at.should == "text/plain"
198
+ r.should_not be_nil
199
+ end
200
+
201
+ it "should match text/html for a request with only */* with no default" do
202
+ accept_types = "*/*"
203
+ group, at, r = Pancake::MimeTypes.negotiate_accept_type(accept_types, :any)
204
+ at.should == "text/html"
205
+ group.should == :any
206
+ end
207
+ end
208
+
177
209
  it "should return nil if there is no matching class" do
178
210
  accept_type = "text/xml"
179
211
  group, r = Pancake::MimeTypes.negotiate_accept_type(accept_type, :text, :html)
@@ -229,6 +261,13 @@ describe Pancake::MimeTypes do
229
261
  r.should be_nil
230
262
  end
231
263
 
264
+ it "should negotiate when any by extension" do
265
+ group, at, r = Pancake::MimeTypes.negotiate_by_extension("xml", :any)
266
+ group.should == :any
267
+ at.should == "application/xml"
268
+ r.should_not be_nil
269
+ end
270
+
232
271
  end
233
272
  end
234
273
 
@@ -41,6 +41,10 @@ describe Pancake::Stacks::Short, "routes" do
41
41
  "delete - bar"
42
42
  end
43
43
 
44
+ any "/any/foo" do
45
+ "any - foo"
46
+ end
47
+
44
48
  get "/baz/:var(/:date)" do
45
49
  "done: var == #{params[:var]} : date == #{params[:date]}"
46
50
  end.name(:baz)
@@ -83,6 +87,14 @@ describe Pancake::Stacks::Short, "routes" do
83
87
  result.body.should == "done: var == hassox : date == 2009-08-21"
84
88
  end
85
89
 
90
+ it "should handle the any request method" do
91
+ [:get, :post, :put, :delete].each do |meth|
92
+ result = self.send(meth, "/any/foo")
93
+ result.status.should == 200
94
+ result.body.should == "any - foo"
95
+ end
96
+ end
97
+
86
98
  describe "url generation" do
87
99
  it "should generate a simple named url" do
88
100
  Pancake.url(RoutedShortStack, :foo).should == "/foo"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pancake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.22
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Neighman
@@ -9,7 +9,7 @@ autorequire: pancake
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-21 00:00:00 +11:00
12
+ date: 2009-11-22 00:00:00 +11:00
13
13
  default_executable: pancake-gen
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency