class YoudaoDict

Constants

VERSION

Public Instance Methods

parse(src) click to toggle source
# File lib/youdao-dict.rb, line 16
def parse(src)
  xml = REXML::Document.new(src)
  parse_phonetic_symbol xml

  __ "辞典翻译: #{@word}", "="
  parse_dict_trans      xml

  __ "网络释义", "="
  parse_web_trans       xml
end
translate(word) click to toggle source
# File lib/youdao-dict.rb, line 9
def translate(word)
  if word && !word.empty?
    @word = word
    parse Net::HTTP.get( "dict.youdao.com", "/fsearch?q=#{word}" )
  end
end

Protected Instance Methods

parse_dict_trans( xml ) click to toggle source

辞典

# File lib/youdao-dict.rb, line 40
def parse_dict_trans( xml )
  xml.each_node('//translation/content') do |node|
    indent
    puts node.text.green
  end
end
parse_phonetic_symbol( xml ) click to toggle source

音标

# File lib/youdao-dict.rb, line 29
def parse_phonetic_symbol( xml )
  xml.each_node('//phonetic-symbol') do |node|
    if node.text
      indent
      print @word
      puts " [#{node.text}]".cyan
    end
  end
end
parse_web_trans( xml ) click to toggle source

网络释义

# File lib/youdao-dict.rb, line 48
def parse_web_trans( xml )
  xml.each_node('//yodao-web-dict/web-translation') do |node|
    __ node.first_node('key/').text
    node.each_node('trans/value/') do |val|
      indent
      puts val.text
    end
  end
end

Private Instance Methods

__(t, pad='-', len=30 ) click to toggle source
# File lib/youdao-dict.rb, line 59
def __(t, pad='-', len=30 )
  puts " #{t} ".center(len, pad)
end
indent(idt=2) click to toggle source
# File lib/youdao-dict.rb, line 63
def indent(idt=2)
  print " " * idt
end