client/lib/deltacloud.rb
# Convert XML response to defined Ruby Class
Convert XML response to defined Ruby Class
Convert XML response to defined Ruby Class
def xml_to_class(base_object, item)
      return nil unless item
      params = {
          :id => item['id'],
          :url => item['href'],
          :name => item.name,
          :client => self
      }
      params.merge!({ :initial_state => (item/'state').text.sanitize }) if (item/'state').length > 0
      obj = base_object.new(params)
      item.xpath('./*').each do |attribute|
        if self.entry_points.keys.include?(:"#{attribute.name}s")
          obj.add_link!(attribute.name, attribute['id']) && next unless (attribute.name == 'bucket' && item.name == 'blob')
        end
        if attribute.name == 'property'
          if attribute['value'] =~ /^(\d+)\.(\d+)$/
            obj.add_hwp_property!(attribute['name'], attribute, :float) && next
          else
            obj.add_hwp_property!(attribute['name'], attribute, :integer) && next
          end
        end
        if attribute.name == 'actions'
          (attribute/'link').each do |link|
            (obj.add_run_action!(item['id'], link) && next) if link[:rel] == 'run'
            obj.add_action_link!(item['id'], link)
          end && next
        end
        if attribute.name == 'mount'
          obj.add_link!("instance", (attribute/"./instance/@id").first.value)
          obj.add_text!("device", (attribute/"./device/@name").first.value)
          next
        end
        if (attribute.name == 'user_metadata')
          meta = {}
          attribute.children.select {|x| x.name=="entry" }.each  do |element|
            value = element.content.gsub!(/(\n) +/,'')
            meta[element['key']] = value
          end
          obj.add_collection!(attribute.name, meta.inspect) && next
        end
        if (['public_addresses', 'private_addresses'].include? attribute.name)
          obj.add_addresses!(attribute.name, (attribute/'*')) && next
        end
        if ('authentication'.include? attribute.name)
          obj.add_authentication!(attribute[:type], (attribute/'*')) && next
        end
        if (attribute/'./*').length > 0
          obj.add_collection!(attribute.name, (attribute/'*').collect { |value| value.text }) && next
        end
        if(attribute.name == 'blob')
          obj.add_blob!(attribute.attributes['id'].value) && next
        end
        obj.add_text!(attribute.name, attribute.text.convert)
      end
      return obj
    end
def xml_to_class ( base_object , item ) return nil unless item params = { :id => item [ 'id' ] , :url => item [ 'href' ] , :name => item . name , :client => self } params . merge! ( { :initial_state => ( item / 'state' ) . text . sanitize } ) if ( item / 'state' ) . length > 0 obj = base_object . new ( params ) item . xpath ( './*' ) . each do | attribute | if self . entry_points . keys . include? ( :" #{ attribute . name } s " ) obj . add_link! ( attribute . name , attribute [ 'id' ] ) && next unless ( attribute . name == 'bucket' && item . name == 'blob' ) end if attribute . name == 'property' if attribute [ 'value' ] =~ / ^( \d +) \. ( \d +)$ / obj . add_hwp_property! ( attribute [ 'name' ] , attribute , :float ) && next else obj . add_hwp_property! ( attribute [ 'name' ] , attribute , :integer ) && next end end if attribute . name == 'actions' ( attribute / 'link' ) . each do | link | ( obj . add_run_action! ( item [ 'id' ] , link ) && next ) if link [ :rel ] == 'run' obj . add_action_link! ( item [ 'id' ] , link ) end && next end if attribute . name == 'mount' obj . add_link! ( "instance" , ( attribute / "./instance/@id" ) . first . value ) obj . add_text! ( "device" , ( attribute / "./device/@name" ) . first . value ) next end if ( attribute . name == 'user_metadata' ) meta = { } attribute . children . select { | x | x . name == "entry" } . each do | element | value = element . content . gsub! ( / ( \n ) + / , '' ) meta [ element [ 'key' ] ] = value end obj . add_collection! ( attribute . name , meta . inspect ) && next end if ( [ 'public_addresses' , 'private_addresses' ] . include? attribute . name ) obj . add_addresses! ( attribute . name , ( attribute / '*' ) ) && next end if ( 'authentication' . include? attribute . name ) obj . add_authentication! ( attribute [ :type ] , ( attribute / '*' ) ) && next end if ( attribute / './*' ) . length > 0 obj . add_collection! ( attribute . name , ( attribute / '*' ) . collect { | value | value . text } ) && next end if ( attribute . name == 'blob' ) obj . add_blob! ( attribute . attributes [ 'id' ] . value ) && next end obj . add_text! ( attribute . name , attribute . text . convert ) end return obj end
Convert XML response to defined Ruby Class
Convert XML response to defined Ruby Class
# Traverse across XML document and deal with elements # Do a link for elements which are links to other REST models # Do a HWP property for hardware profile properties # If there are actions, add they to ActionObject/StateFullObject #deal with blob metadata # Deal with collections like public-addresses, private-addresses #deal with blobs for buckets # Anything else is treaten as text object
base_object
item