Class Amazon::AWS::AWSObject
In: lib/amazon/aws.rb
Parent: Object

Everything returned by AWS is an AWSObject.

Methods

[]   each   each_property   get   load   new   properties   to_h   yaml_load  

Included Modules

REXML

External Aliases

to_s -> to_str

Public Class methods

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.

[Source]

# 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

[Source]

# 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.

[Source]

# 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

Public Instance methods

Fake the appearance of an AWSObject as a hash. key should be any attribute of the object and can be a String, Symbol or anything else that can be converted to a String with to_s.

[Source]

# File lib/amazon/aws.rb, line 452
      def [](key)
        instance_variable_get( "@#{key}" )
      end

Iterator method for cycling through an object‘s properties and values.

[Source]

# 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
each_property(

Alias for each

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.

[Source]

# 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.

[Source]

# 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

Convert an AWSObject to a Hash.

[Source]

# File lib/amazon/aws.rb, line 431
      def to_h
        hash = {}

        each do |iv, value|
          if value.is_a? AWSObject
            hash[iv] = value.to_h
          elsif value.is_a?( AWSArray ) && value.size == 1
            hash[iv] = value[0]
          else
            hash[iv] = value
          end
        end

        hash
      end

[Validate]