Class | Amazon::AWS::AWSObject |
In: |
lib/amazon/aws.rb
|
Parent: | Object |
to_s | -> | to_str |
This method can be used to load AWSObject data previously serialised by Marshal.dump.
Example:
File.open( 'aws.dat' ) { |f| Amazon::AWS::AWSObject.load( f ) }
Marshal.load cannot be used directly, because subclasses of AWSObject are dynamically defined as needed when AWS XML responses are parsed.
Later attempts to load objects instantiated from these classes cause a problem for Marshal, because it knows nothing of classes that were dynamically defined by a separate process.
# File lib/amazon/aws.rb, line 235 def AWSObject.load(io) begin Marshal.load( io ) rescue ArgumentError => ex m = ex.to_s.match( /Amazon::AWS::AWSObject::([^ ]+)/ ) const_set( m[1], Class.new( AWSObject ) ) io.rewind retry end end
# File lib/amazon/aws.rb, line 295 def initialize(op=nil) # The name of this instance variable must never clash with the # uncamelised name of an Amazon tag. # # This is used to store the REXML::Text value of an element, which # exists only when the element contains no children. # @__val__ = nil @__op__ = op if op end
This method can be used to load AWSObject data previously serialised by YAML.dump.
Example:
File.open( 'aws.yaml' ) { |f| Amazon::AWS::AWSObject.yaml_load( f ) }
The standard YAML.load cannot be used directly, because subclasses of AWSObject are dynamically defined as needed when AWS XML responses are parsed.
Later attempts to load objects instantiated from these classes cause a problem for YAML, because it knows nothing of classes that were dynamically defined by a separate process.
# File lib/amazon/aws.rb, line 263 def AWSObject.yaml_load(io) io.each do |line| # File data is external, so it's deemed unsafe when $SAFE > 0, which # is the case with mod_ruby, for example, where $SAFE == 1. # # YAML data isn't eval'ed or anything dangerous like that, so we # consider it safe to untaint it. If we don't, mod_ruby will complain # when Module#const_defined? is invoked a few lines down from here. # line.untaint m = line.match( /Amazon::AWS::AWSObject::([^ ]+)/ ) if m cl_name = [ m[1] ] # Module#const_defined? takes 2 parameters in Ruby 1.9. # cl_name << false if RUBY_VERSION >= '1.9.0' unless AWSObject.const_defined?( *cl_name ) AWSObject.const_set( m[1], Class.new( AWSObject ) ) end end end io.rewind YAML.load( io ) end
Iterator method for cycling through an object‘s properties and values.
# File lib/amazon/aws.rb, line 342 def each # :yields: property, value self.properties.each do |iv| yield iv, instance_variable_get( "@#{iv}" ) end end
For objects of class AWSObject::.*Image, fetch the image in question, optionally overlaying a discount icon for the percentage amount of discount to the image.
# File lib/amazon/aws.rb, line 517 def get(discount=nil) if self.class.to_s =~ /Image$/ && @url url = URI.parse( @url[0] ) url.path.sub!( /(\.\d\d\._)/, "\\1PE#{discount}" ) if discount # FIXME: All HTTP in Ruby/AWS should go through the same method. # Net::HTTP.start( url.host, url.port ) do |http| http.get( url.path ) end.body else nil end end
This alias makes the ability to determine an AWSObject‘s properties a little more intuitive. It‘s pretty much just an alias for the inherited Object#instance_variables method, with a little tidying.
# File lib/amazon/aws.rb, line 391 def properties # Make sure we remove the leading @. # iv = instance_variables.collect { |v| v = v[1..-1] } iv.delete( '__val__' ) iv end