Class: YandexTranslator::XMLParser

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Notifications, REXML
Defined in:
lib/parsers/xml_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(text, index) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/parsers/xml_parser.rb', line 9

def parse(text, index)
  xmldoc = REXML::Document.new(text)
  response = Hash.new

  if error = xmldoc.elements["Error"]
    response["code"] = error.attributes['code'].to_i
    response["message"] = error.attributes['message']
  end

  unless (element = xmldoc.elements["Langs/dirs"]).nil?
    response["dirs"] = element.collect { |e|
      e.text
    }
  end

  unless (element = xmldoc.root.attributes["lang"]).nil?
    response["lang"] = element
  end
  unless (element = xmldoc.elements["Langs/langs"]).nil?
    response["langs"] = Hash.new 
    element.each { |e|
      response["langs"][e.attributes["key"]] = e.attributes["value"]
    }
  end
  unless (element = xmldoc.elements["Translation/text"]).nil?
    response["text"] = element.text
  end

  publish(:parsing_done, response, index)
end