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/03/11 12:44:32 UTC
svn commit: r1080535 - in /incubator/deltacloud/trunk/server:
lib/sinatra/rack_matrix_params.rb server.rb views/instances/show.xml.haml
Author: mfojtik
Date: Fri Mar 11 11:44:32 2011
New Revision: 1080535
URL: http://svn.apache.org/viewvc?rev=1080535&view=rev
Log:
Rack::MatrixParams now support all HTTP methods and are parsed
as real matrix params (params can be defined for every URL component)
Added:
incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb
Modified:
incubator/deltacloud/trunk/server/server.rb
incubator/deltacloud/trunk/server/views/instances/show.xml.haml
Added: 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=1080535&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb (added)
+++ incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb Fri Mar 11 11:44:32 2011
@@ -0,0 +1,86 @@
+# Copyright (C) 2011 Red Hat, Inc.
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# Methods added to this helper will be available to all templates in the application.
+
+module Rack
+
+ require 'cgi'
+
+ class MatrixParams
+ def initialize(app)
+ @app = app
+ end
+
+ # This will allow to use 'matrix' params in requests, like:
+ #
+ # http://example.com/library;section=nw/books;topic=money;binding=hardcover
+ #
+ # Will result in this params matrix:
+ #
+ # => params['library']['section'] = 'nw'
+ # => params['books']['topic'] = 'money'
+ # => params['books']['binding'] = 'hardcover'
+ #
+ # All HTTP methods are supported, in case of POST they will be passed as a
+ # regular <form> parameters.
+
+ def call(env)
+ # Return if this header is not set properly (Rack::Test case...)
+ return @app.call(env) unless env['REQUEST_URI']
+
+ # Split URI to components and then extract ;var=value pairs
+ uri_components = env['REQUEST_URI'].split('/')
+ matrix_params = {}
+ uri_components.each do |component|
+ sub_components, value = component.split(/\;(\w+)\=/), nil
+ next unless sub_components.first # Skip subcomponent if it's empty (usually /)
+ while param=sub_components.pop do
+ if value
+ matrix_params[sub_components.first] ||= {}
+ matrix_params[sub_components.first].merge!(
+ param => value
+ )
+ value=nil
+ next
+ else
+ value = param
+ end
+ end
+ end
+
+ # If request method is POST, simply include matrix params in form_hash
+ env['rack.request.form_hash'].merge!(matrix_params) if env['rack.request.form_hash']
+
+ # For other methods it's a way complicated ;-)
+ if env['REQUEST_METHOD']!='POST' and not matrix_params.keys.empty?
+ # Rewrite current path and query string and strip all matrix params from it
+ env['REQUEST_PATH'], env['PATH_INFO'] = env['REQUEST_URI'].gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
+ env['PATH_INFO'] = env['REQUEST_PATH']
+ env['QUERY_STRING'].gsub!(/;([^\/]*)/, '')
+ new_params = matrix_params.collect do |component, params|
+ params.collect { |k,v| "#{component}[#{k}]=#{CGI::escape(v.to_s)}" }
+ end.flatten
+ # Add matrix params as a regular GET params
+ env['QUERY_STRING'] += '&' if not env['QUERY_STRING'].empty?
+ env['QUERY_STRING'] += "#{new_params.join('&')}"
+ end
+ @app.call(env)
+ end
+ end
+
+end
Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=1080535&r1=1080534&r2=1080535&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Fri Mar 11 11:44:32 2011
@@ -30,15 +30,17 @@ require 'lib/deltacloud/helpers/blob_str
require 'sinatra/rack_driver_select'
require 'sinatra/rack_runtime'
require 'sinatra/rack_etag'
+require 'sinatra/rack_matrix_params'
set :version, '0.2.0'
include Deltacloud::Drivers
set :drivers, Proc.new { driver_config }
-use Rack::DriverSelect
use Rack::ETag
use Rack::Runtime
+use Rack::MatrixParams
+use Rack::DriverSelect
configure do
set :raise_errors => false
Modified: incubator/deltacloud/trunk/server/views/instances/show.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instances/show.xml.haml?rev=1080535&r1=1080534&r2=1080535&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/instances/show.xml.haml (original)
+++ incubator/deltacloud/trunk/server/views/instances/show.xml.haml Fri Mar 11 11:44:32 2011
@@ -22,7 +22,7 @@
- @instance.actions.compact.each do |instance_action|
%link{:rel => instance_action, :method => instance_action_method(instance_action), :href => self.send("#{instance_action}_instance_url", @instance.id)}
- if driver.respond_to?(:run_on_instance)
- %link{:rel => 'run', :method => :post, :href => run_instance_url(@instance.id)}
+ %link{:rel => 'run', :method => :post, :href => "#{run_instance_url(@instance.id)};id=#{@instance.id}"}
- if @instance.can_create_image?
%link{:rel => 'create_image', :method => :post, :href => "#{create_image_url};instance_id=#{@instance.id}"}
- if @instance.instance_variables.include?("@launch_time")