Class: Entityjs::ParseXML

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/parsers/parse_xml.rb

Class Method Summary collapse

Class Method Details

.parse(contents) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/entityjs/parsers/parse_xml.rb', line 27

def self.parse(contents)
  if contents.nil? || contents.empty?
    return '{}'
  end
  
  if contents.is_a? String
    contents = self.parse_to_hash(contents)
  end
  
  #to string
  contents = contents.to_json
  
  #remove @
  contents = contents.gsub('"@','"')
  
  
  #transform string-numbers into numbers
  
  return contents.gsub(/"[0-9\.]*"/){|s| s[1..-2] }
end

.parse_to_hash(contents) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/entityjs/parsers/parse_xml.rb', line 5

def self.parse_to_hash(contents)
  if contents.nil? || contents.empty?
    return '{}'
  end
  #might need different transfomration
  #remove header
  
  contents = contents.gsub(/<\?xml.*\?>/, '')
  #convert to hash
  contents = CobraVsMongoose.xml_to_hash(contents)
  
  #remove root
  contents.each do |i,v|
    if !v.nil?
      contents = v
      break
    end
  end
  
  return contents
end