public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/vic/sinatra.git
Search Repo:
Specs, documentation and fixes for splat'n routes
vic (author)
Thu Apr 24 19:24:31 -0700 2008
commit  2bad23f4d7fcabe2c598d3f8ee78dd592bf1bc87
tree    8d12b01df17b22da233b1ccc72e33558e7ab21c2
parent  9c875ffda0d9d12bd54f83606b30601b1b9b6bf7
...
57
58
59
60
61
 
 
 
 
 
 
 
 
62
63
64
...
57
58
59
 
 
60
61
62
63
64
65
66
67
68
69
70
0
@@ -57,8 +57,14 @@
0
   
0
 Splat'n
0
 
0
- get '/message/*' do
0
- # matches /message/1/2/3/4/5
0
+ get '/say/*/to/*' do
0
+ # matches /say/hello/to/world
0
+ params["splat"] # => ["hello", "world"]
0
+ end
0
+
0
+ get '/download/*.*' do
0
+ # matches /download/path/to/file.xml
0
+ params["splat"] # => ["path/to/file", "xml"]
0
   end
0
   
0
 Get an agent!
...
164
165
166
167
 
168
169
170
171
...
173
174
175
176
177
178
 
 
 
 
 
 
 
 
 
 
179
180
181
182
 
183
184
185
...
194
195
196
 
 
 
 
 
197
198
199
...
164
165
166
 
167
168
169
170
171
...
173
174
175
 
 
 
176
177
178
179
180
181
182
183
184
185
186
 
 
 
187
188
189
190
...
199
200
201
202
203
204
205
206
207
208
209
0
@@ -164,7 +164,7 @@
0
   class Event
0
 
0
     URI_CHAR = '[^/?:,&#\.]'.freeze unless defined?(URI_CHAR)
0
- PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
0
+ PARAM = /(:(#{URI_CHAR}+)|\*)/.freeze unless defined?(PARAM)
0
     SPLAT = /(.*?)/
0
     attr_reader :path, :block, :param_keys, :pattern, :options
0
     
0
0
@@ -173,13 +173,18 @@
0
       @block = b
0
       @param_keys = []
0
       @options = options
0
- regex = @path.to_s.gsub(PARAM) do
0
- @param_keys << $1
0
- "(#{URI_CHAR}+)"
0
+ splats = 0
0
+ regex = @path.to_s.gsub(PARAM) do |match|
0
+ if match == "*"
0
+ @param_keys << "_splat_#{splats}"
0
+ splats += 1
0
+ SPLAT.to_s
0
+ else
0
+ @param_keys << $2
0
+ "(#{URI_CHAR}+)"
0
+ end
0
       end
0
-
0
- regex.gsub!('*', SPLAT.to_s)
0
-
0
+
0
       @pattern = /^#{regex}$/
0
     end
0
         
0
@@ -194,6 +199,11 @@
0
       end
0
       return unless pattern =~ request.path_info.squeeze('/')
0
       params.merge!(param_keys.zip($~.captures.map(&:from_param)).to_hash)
0
+ splats = params.select { |k, v| k =~ /^_splat_\d+$/ }.sort.map(&:last)
0
+ unless splats.empty?
0
+ params.delete_if { |k, v| k =~ /^_splat_\d+$/ }
0
+ params["splat"] = splats
0
+ end
0
       Result.new(block, params, 200)
0
     end
0
     
...
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
0
@@ -27,6 +27,46 @@
0
     body.should.equal 'Hello Blake'
0
   end
0
 
0
+
0
+ specify "handles splats" do
0
+ get '/hi/*' do
0
+ params["splat"].kind_of?(Array).should.equal true
0
+ params["splat"].first
0
+ end
0
+
0
+ get_it '/hi/Blake'
0
+
0
+ should.be.ok
0
+ body.should.equal 'Blake'
0
+ end
0
+
0
+ specify "handles multiple splats" do
0
+ get '/say/*/to/*' do
0
+ params["splat"].join(' ')
0
+ end
0
+
0
+ get_it '/say/hello/to/world'
0
+
0
+ should.be.ok
0
+ body.should.equal 'hello world'
0
+ end
0
+
0
+ specify "allow empty splats" do
0
+ get '/say/*/to*/*' do
0
+ params["splat"].join(' ')
0
+ end
0
+
0
+ get_it '/say/hello/to/world'
0
+
0
+ should.be.ok
0
+ body.should.equal 'hello world' # second splat is empty
0
+
0
+ get_it '/say/hello/tomy/world'
0
+
0
+ should.be.ok
0
+ body.should.equal 'hello my world'
0
+ end
0
+
0
   specify "gives access to underlying response header Hash" do
0
     get '/' do
0
       header['X-Test'] = 'Is this thing on?'

Comments

  • bmizerany Sun Apr 27 19:53:47 -0700 2008

    Vic, is the ready for a pull? I love it.

  • vic Sun Apr 27 19:59:05 -0700 2008

    Oh sure!, please pull.

  • bmizerany Sun Apr 27 20:30:59 -0700 2008

    done. thx!