Permalink
Browse files

Refactored: used ActiveSupport to generalize many methods

  • Loading branch information...
1 parent a45092b commit 738a27107daab04237d0130cc3d4de4f9dbc3bec @joeyates committed Nov 12, 2010
Showing with 30 additions and 43 deletions.
  1. +2 −13 lib/rightstuff/array_instance.rb
  2. +20 −0 lib/rightstuff/base.rb
  3. +4 −16 lib/rightstuff/server.rb
  4. +3 −14 lib/rightstuff/server_array.rb
  5. +1 −0 rightstuff.gemspec
View
15 lib/rightstuff/array_instance.rb
@@ -9,19 +9,8 @@ def initialize( client, item )
super
end
- def self.collection_xpath
- '/instances/instance'
- end
-
- def method_missing( name , *args, &block )
- result = super
- return result unless result.nil?
- return nil if @attributes[ :state ] == 'stopped'
- return @attributes[ name ]
- end
-
- def id
- @attributes[ :href ].split( '/' ).last
+ def active?
+ @attributes[ :state ] != 'stopped'
end
def inputs
View
20 lib/rightstuff/base.rb
@@ -1,10 +1,19 @@
require 'rubygems' if RUBY_VERSION < '1.9'
+require 'active_support/all'
require 'nokogiri'
module Rightstuff
class Base
+ def self.type
+ name.demodulize.underscore
+ end
+
+ def self.collection_xpath
+ "/#{ type.pluralize }/#{ type }"
+ end
+
def self.load_collection( client, doc )
doc.xpath( self.collection_xpath ).collect do | item |
self.new( client, item )
@@ -31,6 +40,17 @@ def initialize( client, item )
@attributes = Base.extract_attributes( item )
end
+ def id
+ @attributes[ :href ].split( '/' ).last
+ end
+
+ def method_missing( name , *args, &block )
+ result = super
+ return result unless result.nil?
+ return nil if ! active?
+ return @attributes[ name ]
+ end
+
def method_missing( name, *args, &block )
return @attributes[ name ]
end
View
20 lib/rightstuff/server.rb
@@ -1,5 +1,5 @@
module Rightstuff
-
+ # http://support.rightscale.com/15-References/RightScale_API_Reference_Guide/02-Management/02-Servers
class Server < Base
attr_reader :attributes
@@ -10,20 +10,8 @@ def initialize( client, item )
super
end
- def self.collection_xpath
- '/servers/server'
- end
-
- def method_missing( name , *args, &block )
- result = super
- return result unless result.nil?
- return nil unless @attributes[ :state ] == 'operational'
- settings unless @settings
- return @attributes[ name ]
- end
-
- def id
- @attributes[ :href ].split( '/' ).last
+ def active?
+ @attributes[ :state ] == 'operational'
end
def inputs
@@ -34,7 +22,7 @@ def inputs
def settings
return @settings if @settings
- doc = @client.get_rest( 'servers/' + id + '/settings' )
+ doc = @client.get_rest( "#{ type.pluralize }/#{ id }/settings" )
xml = Nokogiri::XML( doc )
@settings = Base.extract_attributes( xml.children )
@attributes.merge!( @settings )
View
17 lib/rightstuff/server_array.rb
@@ -10,19 +10,8 @@ def initialize( client, item )
super
end
- def self.collection_xpath
- '/server-arrays/server-array'
- end
-
- def method_missing( name , *args, &block )
- result = super
- return result unless result.nil?
- return nil if @attributes[ :state ] == 'stopped'
- return @attributes[ name ]
- end
-
- def id
- @attributes[ :href ].split( '/' ).last
+ def active?
+ @attributes[ :state ] != 'stopped'
end
def inputs
@@ -33,7 +22,7 @@ def inputs
def instances
return @instances if @instances
- body = Nokogiri::XML( @client.get_rest( "server_arrays/#{id}/instances" ) )
+ body = Nokogiri::XML( @client.get_rest( "#{ type.pluralize }/#{ id }/instances" ) )
@instances = Rightstuff::ArrayInstance.load_collection( @client, body )
end
end
View
1 rightstuff.gemspec
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.executables += EXECUTABLE_FILENAMES
s.require_paths = [ 'lib' ]
s.add_dependency( 'nokogiri', '>= 1.4.3.1' )
+ s.add_dependency( 'activesupport', '>= 2.3.2' )
s.has_rdoc = true
s.rdoc_options += RDOC_OPTS

0 comments on commit 738a271

Please sign in to comment.