Class: VersiononeSdk::ParserXmlAssets

Inherits:
Object
  • Object
show all
Defined in:
lib/versionone_sdk/parser_xml_assets.rb

Instance Method Summary collapse

Constructor Details

#initialize(dOptions = {}) ⇒ ParserXmlAssets

Returns a new instance of ParserXmlAssets.



7
8
9
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 7

def initialize(dOptions={})
  @sUrl = dOptions.key?(:url) ? dOptions[:url] : ''
end

Instance Method Details

#getAssetNodeChildPropVal(oNodeChild = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 75

def getAssetNodeChildPropVal(oNodeChild=nil)
  xxPropVal = nil
  aPropVals = []
  if oNodeChild.children.length > 0
    oNodeChild.children.each do |oNodeChildGrand|
      dAttributes = oNodeChildGrand.attributes
      xxPropVal   = dAttributes.key?('idref')    \
        ? oNodeChildGrand.attribute('idref').value \
        : oNodeChildGrand.text
      xxPropVal = nil if xxPropVal == ''
      aPropVals.push(xxPropVal)
    end
    if aPropVals.length > 1
      xxPropVal = aPropVals
    elsif aPropVals.length == 1
      xxPropVal == aPropVals[0]
    else
      xxPropVal = nil
    end
  else
    xxPropVal = oNodeChild.text
  end
  xxPropVal = nil if xxPropVal == ''
  return xxPropVal
end

#getDocForAssetXml(xAsset = nil) ⇒ Object



28
29
30
31
32
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 28

def getDocForAssetXml(xAsset=nil)
  oXml = Nokogiri::XML::Document.parse(xAsset)
  oAsset = self.getJsondocForXmlAssetNode( oXml.root )
  return oAsset
end

#getDocsForAssetsXml(xAssets = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 16

def getDocsForAssetsXml(xAssets=nil)
  oXml = Nokogiri::XML::Document.parse(xAssets)
  oBlock = oXml.xpath("//Assets/Asset")
  aDocs = []
  oBlock.map do |oNodeAsset|
    oAsset = self.getJsondocForXmlAssetNode( oNodeAsset )
    unless oAsset.nil?
      aDocs.push(oAsset)
    end
  end
  return aDocs
end

#getDocsForAssetsXmlPath(sPath = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 10

def getDocsForAssetsXmlPath(sPath=nil)
  if File.exists?(sPath)
    return self.getDocsForAssetsXml(IO.read(sPath))
  end
  return []
end

#getJsondocForXmlAssetNode(oNodeAsset = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 33

def getJsondocForXmlAssetNode(oNodeAsset=nil)
  if oNodeAsset.nil?
    raise RuntimeError, 'E_NIL_NODE'
  end
  oAsset = VersiononeSdk::Asset.new
  oAsset.bIsStrict = false
  sOidtokSrc       = nil
  sObjectType      = nil
  iObjectId        = nil
  if oNodeAsset.attribute('id')
    sOidtokSrc     =  oNodeAsset.attribute('id').value
    if sOidtokSrc  =~ /^(.+):([0-9]+)$/
      sObjectType  =  $1
      iObjectId    =  $2.to_i
      oAsset.setProp(:_sObjectDomain__id,'Versionone')
      oAsset.setProp(:_sObjectType__id,sObjectType)
      oAsset.setProp(:_iObjectId__id,  iObjectId)
    end
  end
  if oNodeAsset.attribute('href').value
    sUrl = @sUrl \
      ? @sUrl + oNodeAsset.attribute('href').value
      : oNodeAsset.attribute('href').value
    oAsset.setProp(:_sObjectUrl__id,sUrl)
  end
  oNodeAsset.children.each do |oNodeChild|
    if oNodeChild.name == 'Attribute' || oNodeChild.name == 'Relation'
      if oNodeChild.attribute('name')
        yPropKey = oNodeChild.attribute('name').to_s.to_sym
        xxPropVal = getAssetNodeChildPropVal( oNodeChild )
        oAsset.setProp(yPropKey,xxPropVal)
      end
    elsif oNodeChild.name == 'Message' && oNodeChild.text == 'Not Found'
      raise RuntimeError, "E_ASSET_NOT_FOUND #{oNodeAsset.attribute('href').value}"
    else
      raise RuntimeError, "E_UNKNOWN_ASSET_NODE_NAME #{oNodeChild.name}"
    end
  end
  oAsset.inflate
  return oAsset
end