Class: BaiduTranslateApi
- Inherits:
-
Object
- Object
- BaiduTranslateApi
- Defined in:
- lib/baidu-translate-api.rb
Constant Summary collapse
- VERSION =
'0.1.0'- SALT_MAXNUM =
10000000- HTTP_ENDPOINT =
'http://api.fanyi.baidu.com/api/trans/vip/translate'- HTTPS_ENDPOINT =
'https://fanyi-api.baidu.com/api/trans/vip/translate'
Instance Attribute Summary collapse
-
#appid ⇒ Object
Returns the value of attribute appid.
-
#default_from ⇒ Object
Returns the value of attribute default_from.
-
#default_to ⇒ Object
Returns the value of attribute default_to.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#use_https ⇒ Object
Returns the value of attribute use_https.
Instance Method Summary collapse
- #endpoint ⇒ Object
-
#initialize(appid:, secret_key:, default_from: :auto, default_to: :zh, use_https: true) ⇒ BaiduTranslateApi
constructor
A new instance of BaiduTranslateApi.
- #request(word, from: self.default_from, to: self.default_to) ⇒ Object
- #translate(word, from: self.default_from, to: self.default_to) ⇒ Object
Constructor Details
#initialize(appid:, secret_key:, default_from: :auto, default_to: :zh, use_https: true) ⇒ BaiduTranslateApi
Returns a new instance of BaiduTranslateApi.
15 16 17 18 19 20 21 |
# File 'lib/baidu-translate-api.rb', line 15 def initialize(appid:, secret_key:, default_from: :auto, default_to: :zh, use_https: true) @appid = appid @secret_key = secret_key @default_from = default_from @default_to = default_to @use_https = use_https end |
Instance Attribute Details
#appid ⇒ Object
Returns the value of attribute appid.
14 15 16 |
# File 'lib/baidu-translate-api.rb', line 14 def appid @appid end |
#default_from ⇒ Object
Returns the value of attribute default_from.
14 15 16 |
# File 'lib/baidu-translate-api.rb', line 14 def default_from @default_from end |
#default_to ⇒ Object
Returns the value of attribute default_to.
14 15 16 |
# File 'lib/baidu-translate-api.rb', line 14 def default_to @default_to end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
14 15 16 |
# File 'lib/baidu-translate-api.rb', line 14 def secret_key @secret_key end |
#use_https ⇒ Object
Returns the value of attribute use_https.
14 15 16 |
# File 'lib/baidu-translate-api.rb', line 14 def use_https @use_https end |
Instance Method Details
#endpoint ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/baidu-translate-api.rb', line 47 def endpoint if self.use_https HTTPS_ENDPOINT else HTTP_ENDPOINT end end |
#request(word, from: self.default_from, to: self.default_to) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/baidu-translate-api.rb', line 23 def request(word, from: self.default_from, to: self.default_to) if word.bytesize > 6000 raise AugumentsError.new("word bytesize over 6000, https://api.fanyi.baidu.com/api/trans/product/apidoc") end salt = rand(SALT_MAXNUM).to_s q = word sign = Digest::MD5.hexdigest "#{self.appid}#{q}#{salt}#{self.secret_key}" params = { salt: salt, q: q, to: to, from: from, appid: self.appid, sign: sign, } open(endpoint + '?' + URI.encode_www_form(params)) end |
#translate(word, from: self.default_from, to: self.default_to) ⇒ Object
41 42 43 44 45 |
# File 'lib/baidu-translate-api.rb', line 41 def translate(word, from: self.default_from, to: self.default_to) res = request(word, from: from, to: to) data = JSON.parse(res.read) data['trans_result'].map {|d| d['dst'] }.join("\n") end |