You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2011/06/07 12:21:14 UTC

svn commit: r1132943 - in /incubator/deltacloud/trunk/server/lib/sinatra: rack_matrix_params.rb url_for.rb

Author: mfojtik
Date: Tue Jun  7 10:21:14 2011
New Revision: 1132943

URL: http://svn.apache.org/viewvc?rev=1132943&view=rev
Log:
Added 'api_url_for' helper to deal with prefix and matrix parameters
as 'driver' and 'provider'.
Every link should now include those parameters when they are present.

This patch also introduces DEFAULT_URI_PREFIX constant to be use
as a default for all routes instead of '/api

Modified:
    incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb
    incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb

Modified: incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb?rev=1132943&r1=1132942&r2=1132943&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb Tue Jun  7 10:21:14 2011
@@ -57,7 +57,7 @@ module Rack
 	    value=nil
 	    next
 	  else
-	    value = param
+	    value = param.gsub(/\?.*$/, '')
 	  end
 	end
       end

Modified: incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb?rev=1132943&r1=1132942&r2=1132943&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb Tue Jun  7 10:21:14 2011
@@ -28,6 +28,19 @@ require 'uri'
 
 module Sinatra
   module UrlForHelper
+
+    DEFAULT_URI_PREFIX = "/api"
+
+    def api_url_for(url_fragment, mode=:path_only)
+      matrix_params = ''
+      if request.params['api']
+        matrix_params += ";provider=%s" % request.params['api']['provider'] if request.params['api']['provider']
+        matrix_params += ";driver=%s" % request.params['api']['driver'] if request.params['api']['driver']
+      end
+      url_fragment = "/#{url_fragment}" unless url_fragment =~ /^\// # There is no need to prefix URI with '/'
+      url_for "#{DEFAULT_URI_PREFIX}#{matrix_params}#{url_fragment}", mode
+    end
+
     # Construct a link to +url_fragment+, which should be given relative to
     # the base of this Sinatra app.  The mode should be either
     # <code>:path_only</code>, which will generate an absolute path within
@@ -70,7 +83,7 @@ module Sinatra
     end
 
     def root_url
-      url_for '/'
+      DEFAULT_URI_PREFIX
     end
   end