@@ -72,12 +72,16 @@ def prefix(prefix = nil)
7272 # end
7373 # end
7474 #
75- def version ( *new_versions , &block )
76- if new_versions . any?
77- @versions = versions | new_versions
78- nest ( block ) { set ( :version , new_versions ) }
79- else
80- settings [ :version ]
75+ def version ( *args , &block )
76+ if args . any?
77+ options = args . pop if args . last . is_a? Hash
78+ options ||= { }
79+ options = { :using => :header } . merge! ( options )
80+ @versions = versions | args
81+ nest ( block ) do
82+ set ( :version , args )
83+ set ( :version_options , options )
84+ end
8185 end
8286 end
8387
@@ -248,15 +252,15 @@ def route(methods, paths = ['/'], route_options = {}, &block)
248252 request_method = ( method . to_s . upcase unless method == :any )
249253
250254 routes << Route . new ( route_options . merge ( {
251- :prefix => prefix ,
252- :version => version ? version . join ( '|' ) : nil ,
253- :namespace => namespace ,
254- :method => request_method ,
255+ :prefix => prefix ,
256+ :version => settings [ : version] ? settings [ : version] . join ( '|' ) : nil ,
257+ :namespace => namespace ,
258+ :method => request_method ,
255259 :path => prepared_path ,
256260 :params => path_params } ) )
257261
258- route_set . add_route ( endpoint ,
259- :path_info => path ,
262+ route_set . add_route ( endpoint ,
263+ :path_info => path ,
260264 :request_method => request_method
261265 )
262266 end
@@ -357,10 +361,18 @@ def build_endpoint(&block)
357361 :format => settings [ :error_format ] || :txt ,
358362 :rescue_options => settings [ :rescue_options ] ,
359363 :rescue_handlers => settings [ :rescue_handlers ] || { }
360- b . use Rack ::Auth ::Basic , settings [ :auth ] [ :realm ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_basic
364+
365+ b . use Rack ::Auth ::Basic , settings [ :auth ] [ :realm ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_basic
361366 b . use Rack ::Auth ::Digest ::MD5 , settings [ :auth ] [ :realm ] , settings [ :auth ] [ :opaque ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_digest
362- b . use Grape ::Middleware ::Prefixer , :prefix => prefix if prefix
363- b . use Grape ::Middleware ::Versioner , :versions => ( version if version . is_a? ( Array ) ) if version
367+ b . use Grape ::Middleware ::Prefixer , :prefix => prefix if prefix
368+
369+ if settings [ :version ]
370+ b . use Grape ::Middleware ::Versioner . using ( settings [ :version_options ] [ :using ] ) , {
371+ :versions => settings [ :version ] ,
372+ :version_options => settings [ :version_options ]
373+ }
374+ end
375+
364376 b . use Grape ::Middleware ::Formatter , :default_format => default_format || :json
365377 middleware . each { |m | b . use *m }
366378
@@ -375,7 +387,6 @@ def build_endpoint(&block)
375387 } , &block )
376388 endpoint . send :include , helpers
377389 b . run endpoint
378-
379390 b . to_app
380391 end
381392
@@ -396,7 +407,7 @@ def route_set
396407 def prepare_path ( path )
397408 parts = [ ]
398409 parts << prefix if prefix
399- parts << ':version' if version
410+ parts << ':version' if settings [ : version] && settings [ :version_options ] [ :using ] == :path
400411 parts << namespace . to_s if namespace
401412 parts << path . to_s if path && '/' != path
402413 parts . last << '(.:format)'
@@ -405,12 +416,12 @@ def prepare_path(path)
405416
406417 def compile_path ( path , anchor = true )
407418 endpoint_options = { }
408- endpoint_options [ :version ] = /#{ version . join ( '|' ) } / if version
419+ endpoint_options [ :version ] = /#{ settings [ : version] . join ( '|' ) } / if settings [ : version]
409420
410421 Rack ::Mount ::Strexp . compile ( prepare_path ( path ) , endpoint_options , %w( / . ? ) , anchor )
411422 end
412- end
413-
414- reset!
423+ end
424+
425+ reset!
415426 end
416427end
0 commit comments