Class: Baidu::Translate
- Inherits:
-
Object
- Object
- Baidu::Translate
- Includes:
- HTTParty
- Defined in:
- lib/baidu/translate.rb
Instance Method Summary collapse
-
#initialize(apikey, from = nil, to = nil) ⇒ Translate
constructor
A new instance of Translate.
- #translate(query, from = nil, to = nil) ⇒ Object
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) = { :query =>{ :client_id => @apikey, :q=>query, :from=>from||@from, :to=>to||@to } } response = self.class.get('/public/2.0/bmt/translate',) result = [] response['trans_result'].each do |trans| result << trans['dst'] end result end |