class Baidu::Translate

Public Class Methods

new(apikey,from=nil,to=nil) click to toggle source
# File lib/baidu/translate.rb, line 8
def initialize(apikey,from=nil,to=nil)
  @from_to = %w(zh_en zh_jp en_zh jp_zh)
  @apikey = apikey
  unless from.nil? and to.nil?
    if @from_to.include?"#{from}_#{to}"
      @from,@to = from,to
    else
      warn "invalid options,only allow:#{@from_to}"
    end
  end
end

Public Instance Methods

translate(query,from=nil,to=nil) click to toggle source
# File lib/baidu/translate.rb, line 19
def translate(query,from=nil,to=nil)
  options = {
    :query =>{
      :client_id => @apikey,
      :q=>query,
      :from=>from||@from,
      :to=>to||@to
    }
  }
  response = self.class.get('/public/2.0/bmt/translate',options)
  result = []
  response['trans_result'].each do |trans|
    result << trans['dst']
  end
  result
end