class Blade::Translate

Constants

VERSION

Public Class Methods

new(input) click to toggle source
# File lib/blade/translate.rb, line 10
def initialize(input)
  @words = input
  query_for_hash
end

Public Instance Methods

dict_explains() click to toggle source
# File lib/blade/translate.rb, line 36
def dict_explains
  dict_explains = @result_hash["basic"]["explains"] if @result_hash["basic"]
  lines         = dict_explains.collect do |explain|
    " - " + explain.color(:green)
  end
  lines << ""
end
query_for_hash() click to toggle source
# File lib/blade/translate.rb, line 15
def query_for_hash
  query_url    = API_URL + URI.escape(@words.gsub(/ /, '+'))
  result_json  = Net::HTTP.get(URI(query_url))
  @result_hash = JSON.parse(result_json)
end
result() click to toggle source
# File lib/blade/translate.rb, line 64
def result
  output = []
  output << word_and_phonetic << yd_result << web_results
  output.flatten
end
translations() click to toggle source
# File lib/blade/translate.rb, line 21
def translations
  translations = @result_hash["translation"]
  lines        = translations.collect do |translation|
    "  " + translation.color(:green)
  end
  lines << ""
end
web_results() click to toggle source
# File lib/blade/translate.rb, line 44
def web_results
  return [] unless @result_hash["web"]
  lines       = []
  web_results = @result_hash["web"]
  web_results.each_with_index do |web_result, i|
    web_result_key   = web_result["key"].gsub(/#{@words}/i, @words.color(:yellow))
    web_result_value = web_result["value"].join(', ').color(:cyan)
    lines << " #{i+1}. #{web_result_key}"
    lines << "    #{web_result_value}"
  end
  lines << ""
end
word_and_phonetic() click to toggle source
# File lib/blade/translate.rb, line 29
def word_and_phonetic
  line     = " " + @words
  phonetic = @result_hash["basic"]["phonetic"] if @result_hash["basic"]
  line     += " [ #{phonetic} ]".color(:magenta) if phonetic
  [line, ""]
end
yd_result() click to toggle source

一般来说,有道词典解释的比较好 但是长一点的句子有道词典没有结果,需要用有道翻译 所以如果有道词典有结果就只用词典的结果,否则用有道翻译

# File lib/blade/translate.rb, line 60
def yd_result
  @result_hash["basic"].nil? ? translations : dict_explains
end