module BaiduTranslate::Translation
Attributes
app_id[RW]
key[RW]
Public Instance Methods
setup(app_id, key)
click to toggle source
# File lib/baidu_translate/translation.rb, line 12 def setup(app_id, key) @app_id = app_id @key = key end
translate(text, from, to)
click to toggle source
# File lib/baidu_translate/translation.rb, line 17 def translate(text, from, to) raise Exception.new("AppID and/or Key missing. Use BaiduTranslate.setup() to initialize.") unless @app_id && @key salt = rand(32768..65536) api_params = { q: text, from: from, to: to, appid: @app_id, salt: salt, sign: sign(text, salt) } response = Faraday.get(API_URL, api_params).body parsed_response = JSON.parse(response) raise Exception.new(parsed_response.to_s) if parsed_response.key? "error_code" parsed_response['trans_result'][0]['dst'] end
Private Instance Methods
sign(text, salt)
click to toggle source
# File lib/baidu_translate/translation.rb, line 36 def sign(text, salt) undigested_sign = @app_id.to_s + text + salt.to_s + @key md5 = Digest::MD5.new md5 << undigested_sign md5.hexdigest end