incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject svn commit: r1067893 - /incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb
Date Mon, 07 Feb 2011 09:46:20 GMT
Author: mfojtik
Date: Mon Feb  7 09:46:20 2011
New Revision: 1067893

URL: http://svn.apache.org/viewvc?rev=1067893&view=rev
Log:
Added support for HEAD and OPTIONS for Rabbit

Modified:
    incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb

Modified: incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb?rev=1067893&r1=1067892&r2=1067893&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb Mon Feb  7 09:46:20 2011
@@ -36,6 +36,11 @@ module Sinatra
         @description = ""
         instance_eval(&block) if block_given?
         generate_documentation
+        generate_options
+      end
+
+      def http_method
+        @method
       end
 
       def standard?
@@ -58,6 +63,21 @@ module Sinatra
         end
       end
 
+      def generate_options
+        current_operation = self
+        ::Sinatra::Application.options("/api/#{current_operation.collection.name}/#{current_operation.name}")
do
+          required_params = current_operation.effective_params(driver).collect do |name,
validation| 
+            name.to_s if validation.type.eql?(:required)
+          end.compact.join(',')
+          optional_params = current_operation.effective_params(driver).collect do |name,
validation| 
+            name.to_s if validation.type.eql?(:optional)
+          end.compact.join(',')
+          headers 'X-Required-Parameters' => required_params
+          headers 'X-Optional-Parameters' => optional_params
+          [200, '']
+        end
+      end
+
       def control(&block)
         op = self
         @control = Proc.new do
@@ -142,6 +162,8 @@ module Sinatra
         @operations = {}
         instance_eval(&block) if block_given?
         generate_documentation
+        generate_head
+        generate_options
       end
 
       # Set/Return description for collection
@@ -152,6 +174,24 @@ module Sinatra
         @description = text
       end
 
+      def generate_head
+        current_collection = self
+        ::Sinatra::Application.head("/api/#{name}") do
+          methods_allowed = current_collection.operations.collect { |o| o[1].method.to_s.upcase
}.uniq.join(',')
+          headers 'Allow' => "HEAD,OPTIONS,#{methods_allowed}"
+          [200, '']
+        end
+      end
+
+      def generate_options
+        current_collection = self
+        ::Sinatra::Application.options("/api/#{name}") do
+          operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',')
+          headers 'X-Operations-Allowed' => operations_allowed
+          [200, '']
+        end
+      end
+
       def generate_documentation
         coll = self
         ::Sinatra::Application.get("/api/docs/#{@name}") do
@@ -288,3 +328,13 @@ class String
           downcase
   end
 end
+
+configure do
+  class << Sinatra::Base
+    def options(path, opts={}, &block)
+      route 'OPTIONS', path, opts, &block
+    end
+  end
+  Sinatra::Delegator.delegate :options
+end
+



Mime
View raw message