incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962229 - in /incubator/deltacloud/trunk/server/libexec: ./ lib/deltacloud/ lib/sinatra/ views/ views/docs/
Date Thu, 08 Jul 2010 23:38:10 GMT
Author: lutter
Date: Thu Jul  8 23:38:10 2010
New Revision: 962229

URL: http://svn.apache.org/viewvc?rev=962229&view=rev
Log:
Factor validation and parameters into a separate module

Also fixes a bug where parameters with a fixed set of values were never
validated against those values. The issue was a naming problem in
Operation.control in rabbit.rb

Added:
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/validation.rb
    incubator/deltacloud/trunk/server/libexec/views/error.xml.haml
      - copied, changed from r962228, incubator/deltacloud/trunk/server/libexec/views/error.html.haml
Removed:
    incubator/deltacloud/trunk/server/libexec/lib/sinatra/validation.rb
    incubator/deltacloud/trunk/server/libexec/views/error.html.haml
Modified:
    incubator/deltacloud/trunk/server/libexec/lib/sinatra/rabbit.rb
    incubator/deltacloud/trunk/server/libexec/server.rb
    incubator/deltacloud/trunk/server/libexec/views/docs/collection.xml.haml
    incubator/deltacloud/trunk/server/libexec/views/docs/operation.html.haml
    incubator/deltacloud/trunk/server/libexec/views/docs/operation.xml.haml

Added: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/validation.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/validation.rb?rev=962229&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/validation.rb (added)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/validation.rb Thu Jul  8 23:38:10
2010
@@ -0,0 +1,61 @@
+module Deltacloud::Validation
+
+  class Failure < StandardError
+    attr_reader :param
+    def initialize(param, msg='')
+      super(msg)
+      @param = param
+    end
+
+    def name
+      param.name
+    end
+  end
+
+  class Param
+    attr_reader :name, :klass, :type, :options, :description
+
+    def initialize(args)
+      @name = args[0]
+      @klass = args[1] || :string
+      @type = args[2] || :optional
+      @options = args[3] || []
+      @description = args[4] || ''
+    end
+
+    def required?
+      type.eql?(:required)
+    end
+
+    def optional?
+      type.eql?(:optional)
+    end
+  end
+
+  def param(*args)
+    raise DuplicateParamException if params[args[0]]
+    p = Param.new(args)
+    params[p.name] = p
+  end
+
+  def params
+    @params ||= {}
+    @params
+  end
+
+  def each_param(&block)
+    params.each_value { |p| yield p }
+  end
+
+  def validate(values)
+    each_param do |p|
+      if p.required? and not values[p.name]
+        raise Failure.new(p, "Required parameter #{p.name} not found")
+      end
+      if values[p.name] and not p.options.empty? and
+          not p.options.include?(values[p.name])
+        raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not
in #{p.options.join(", ")}")
+      end
+    end
+  end
+end

Modified: incubator/deltacloud/trunk/server/libexec/lib/sinatra/rabbit.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/sinatra/rabbit.rb?rev=962229&r1=962228&r2=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/sinatra/rabbit.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/sinatra/rabbit.rb Thu Jul  8 23:38:10 2010
@@ -1,5 +1,6 @@
 require 'sinatra/base'
 require 'sinatra/url_for'
+require 'deltacloud/validation'
 
 module Sinatra
 
@@ -8,11 +9,12 @@ module Sinatra
     class DuplicateParamException < Exception; end
     class DuplicateOperationException < Exception; end
     class DuplicateCollectionException < Exception; end
-    class ValidationFailure < Exception; end
 
     class Operation
       attr_reader :name, :method
 
+      include ::Deltacloud::Validation
+
       STANDARD = {
         :index => { :method => :get, :member => false },
         :show =>  { :method => :get, :member => true },
@@ -29,7 +31,6 @@ module Sinatra
         @method = opts[:method].to_sym
         @member = opts[:member]
         @description = ""
-        @params = {}
         instance_eval(&block) if block_given?
         generate_documentation
       end
@@ -54,23 +55,10 @@ module Sinatra
         end
       end
 
-      def param(*args)
-        raise DuplicateParamException if @params[args[0]]
-        spec = {
-          :class => args[1] || :string,
-          :type => args[2] || :optional,
-          :options => args[3] || [],
-          :description => args[4] || '' }
-        @params[args[0]] = spec
-      end
-
-      def params
-        @params
-      end
-
       def control(&block)
+        op = self
         @control = Proc.new do
-          validate_parameters(params, @params)
+          op.validate(params)
           instance_eval(&block)
         end
       end

Modified: incubator/deltacloud/trunk/server/libexec/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/server.rb?rev=962229&r1=962228&r2=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/server.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/server.rb Thu Jul  8 23:38:10 2010
@@ -9,8 +9,8 @@ require 'builder'
 require 'drivers'
 require 'sinatra/static_assets'
 require 'sinatra/rabbit'
-require 'sinatra/validation'
 require 'sinatra/lazy_auth'
+require 'deltacloud/validation'
 
 configure do
   set :raise_errors => false
@@ -64,6 +64,19 @@ def show(model)
   end
 end
 
+
+#
+# Error handlers
+#
+error Deltacloud::Validation::Failure do
+  @error = request.env['sinatra.error']
+  $stdout.flush
+  response.status = 400
+  respond_to do |format|
+    format.xml { haml :error, :layout => false }
+  end
+end
+
 # Redirect to /api
 get '/' do redirect '/api'; end
 

Modified: incubator/deltacloud/trunk/server/libexec/views/docs/collection.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/docs/collection.xml.haml?rev=962229&r1=962228&r2=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/docs/collection.xml.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/docs/collection.xml.haml Thu Jul  8 23:38:10
2010
@@ -5,10 +5,10 @@
       - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
         %operation{:url => "/api/#{@collection.name.to_s}", :name => "#{operation}",
:href => "#{@operations[operation].path}", :method => "#{@operations[operation].method}"}
           %description #{@operations[operation].description}
-          - @operations[operation].params.each_key do |p|
-            %parameter{:name => "#{p}", :type => "#{@operations[operation].params[p][:type]}"}
-              %class #{@operations[operation].params[p][:class]}
-              - unless @operations[operation].params[p][:options].empty?
+          - @operations[operation].each_param do |param|
+            %parameter{:name => "#{param.name}", :type => "#{param.type}"}
+              %class #{param.klass}
+              - unless param.options.empty?
                 %values
-                  - @operations[operation].params[p][:options].each do |v|
+                  - param.options.each do |v|
                     %value #{v}

Modified: incubator/deltacloud/trunk/server/libexec/views/docs/operation.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/docs/operation.html.haml?rev=962229&r1=962228&r2=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/docs/operation.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/docs/operation.html.haml Thu Jul  8 23:38:10
2010
@@ -22,10 +22,10 @@
       %th Class
       %th Valid values
   %tbody
-    - @operation.params.each_key do |p|
+    - @operation.each_param do |p|
       %tr
         %td{:style => "width:15em"}
-          %em #{p}
-        %td{:style => "width:10em"} #{@operation.params[p][:type]}
-        %td #{@operation.params[p][:class]}
-        %td{:style => "width:10em"} #{@operation.params[p][:options].join(',')}
+          %em #{p.name}
+        %td{:style => "width:10em"} #{p.type}
+        %td #{p.klass}
+        %td{:style => "width:10em"} #{p.options.join(',')}

Modified: incubator/deltacloud/trunk/server/libexec/views/docs/operation.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/docs/operation.xml.haml?rev=962229&r1=962228&r2=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/docs/operation.xml.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/docs/operation.xml.haml Thu Jul  8 23:38:10
2010
@@ -1,10 +1,10 @@
 %docs{:status => "unsupported"}
   %operation{:url => "/api/docs/#{@collection.name.to_s}", :name => "#{@operation.name.to_s}",
:href => "#{@operation.path}", :method => "#{@operation.method}"}
     %description #{@operation.description}
-    - @operation.params.each_key do |p|
-      %parameter{:name => "#{p}", :type => "#{@operation.params[p][:type]}"}
-        %class #{@operation.params[p][:class]}
-        - unless @operation.params[p][:options].empty?
+    - @operation.each_param do |param|
+      %parameter{:name => "#{param.name}", :type => "#{param.type}"}
+        %class #{param.klass}
+        - unless param.options.empty?
           %values
-            - @operation.params[p][:options].each do |v|
+            - param.options.each do |v|
               %value #{v}

Copied: incubator/deltacloud/trunk/server/libexec/views/error.xml.haml (from r962228, incubator/deltacloud/trunk/server/libexec/views/error.html.haml)
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/views/error.xml.haml?p2=incubator/deltacloud/trunk/server/libexec/views/error.xml.haml&p1=incubator/deltacloud/trunk/server/libexec/views/error.html.haml&r1=962228&r2=962229&rev=962229&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/views/error.html.haml (original)
+++ incubator/deltacloud/trunk/server/libexec/views/error.xml.haml Thu Jul  8 23:38:10 2010
@@ -1,7 +1,7 @@
 %error{:url => "#{request.env['REQUEST_URI']}"}
   %parameter #{@error.name}
-  %msg #{@error.msg}
-  - unless @error.spec[:options].empty?
+  %message #{@error.message}
+  - unless @error.param.options.empty?
     %valid_options
-      - @error.spec[:options].each do |v|
+      - @error.param.options.each do |v|
         %value #{v}



Mime
View raw message