incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962272 - in /incubator/deltacloud/trunk/server: lib/deltacloud/helpers/driver_helper.rb lib/deltacloud/models/base_model.rb server.rb
Date Thu, 08 Jul 2010 23:42:34 GMT
Author: lutter
Date: Thu Jul  8 23:42:33 2010
New Revision: 962272

URL: http://svn.apache.org/viewvc?rev=962272&view=rev
Log:
JSON output added

Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/helpers/driver_helper.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/models/base_model.rb
    incubator/deltacloud/trunk/server/server.rb

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/helpers/driver_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/helpers/driver_helper.rb?rev=962272&r1=962271&r2=962272&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/helpers/driver_helper.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/helpers/driver_helper.rb Thu Jul  8 23:42:33
2010
@@ -26,4 +26,20 @@ module DriverHelper
       Converters::XMLConverter.new( self, type ).convert(obj)
     end
   end
+
+  def convert_to_json(type, obj)
+    if ( [ :flavor, :account, :image, :realm, :instance, :storage_volume, :storage_snapshot
].include?( type ) )
+      if Array.eql?(obj.class)
+        data = obj.collect do |o|
+          o.to_hash.merge({ :href => self.send(:"#{type}_url", o.id ) })
+        end
+        type = type.to_s.pluralize
+      else
+        data = obj.to_hash
+        data.merge!({ :href => self.send(:"#{type}_url", data[:id]) })
+      end
+      return { :"#{type}" => data }.to_json
+    end
+  end
+
 end

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/models/base_model.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/models/base_model.rb?rev=962272&r1=962271&r2=962272&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/models/base_model.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/models/base_model.rb Thu Jul  8 23:42:33
2010
@@ -27,8 +27,32 @@ class BaseModel
     end
   end
 
+  def self.attr_accessor(*vars)
+    @attributes ||= [:id]
+    @attributes.concat vars
+    super
+  end
+
+  def self.attributes
+    @attributes
+  end
+
+  def attributes
+    self.class.attributes
+  end
+
   def id
     @id
   end
 
+  def to_hash
+    out = {}
+    self.attributes.each { |attribute| out.merge!({ attribute => self.send(:"#{attribute}")
} ) }
+    out
+  end
+
+  def to_json
+    self.to_hash.to_json
+  end
+
 end

Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=962272&r1=962271&r2=962272&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Thu Jul  8 23:42:33 2010
@@ -1,5 +1,6 @@
 require 'rubygems'
 require 'deltacloud'
+require 'json'
 require 'sinatra'
 require 'sinatra/respond_to'
 require 'erb'
@@ -47,7 +48,8 @@ def filter_all(model)
     @elements = driver.send(model.to_sym, credentials, filter)
     instance_variable_set(:"@#{model}", @elements)
     respond_to do |format|
-      format.xml  { return convert_to_xml(singular, @elements) }
+      format.xml  { convert_to_xml(singular, @elements) }
+      format.json { convert_to_json(singular, @elements) }
       format.html { haml :"#{model}/index" }
     end
 end
@@ -56,7 +58,8 @@ def show(model)
   @element = driver.send(model, credentials, { :id => params[:id]} )
   instance_variable_set("@#{model}", @element)
   respond_to do |format|
-    format.xml { return convert_to_xml(model, @element) }
+    format.xml { convert_to_xml(model, @element) }
+    format.json { convert_to_json(model, @element) }
     format.html { haml :"#{model.to_s.pluralize}/show" }
   end
 end
@@ -93,6 +96,14 @@ get '/api\/?' do
     @version = 1.0
     respond_to do |format|
         format.xml { haml :"api/show" }
+        format.json do
+          { :api => {
+            :version => @version,
+            :driver => DRIVER,
+            :links => entry_points.collect { |l| { :rel => l[0], :href => l[1]}
}
+            }
+          }.to_json
+        end
         format.html { haml :"api/show" }
     end
 end
@@ -212,7 +223,8 @@ end
 def instance_action(name)
   @instance = driver.send(:"#{name}_instance", credentials, params[:id])
   respond_to do |format|
-    format.xml { return convert_to_xml(:instance, @instance) }
+    format.xml { convert_to_xml(:instance, @instance) }
+    format.json {convert_to_json(:instance, @instance) }
     format.html { haml :"instances/show" }
   end
 end



Mime
View raw message