Class: Baidu::Translate

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/baidu/translate.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey, from = nil, to = nil) ⇒ Translate

Returns a new instance of Translate.



8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#translate(query, from = nil, to = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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