Module: BaiduTranslate::Translation
- Included in:
- BaiduTranslate
- Defined in:
- lib/baidu_translate/translation.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
10 11 12 |
# File 'lib/baidu_translate/translation.rb', line 10 def app_id @app_id end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/baidu_translate/translation.rb', line 10 def key @key end |
Instance Method Details
#setup(app_id, key) ⇒ Object
12 13 14 15 |
# File 'lib/baidu_translate/translation.rb', line 12 def setup(app_id, key) @app_id = app_id @key = key end |
#translate(text, from, to) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |