Class: TxTranslate::TencentFy
- Inherits:
-
Object
- Object
- TxTranslate::TencentFy
- Defined in:
- lib/tx_translate/tencent_fy.rb
Instance Method Summary collapse
- #code ⇒ Object
- #data ⇒ Object
- #encode_parameters ⇒ Object
- #full_request_url ⇒ Object
- #host ⇒ Object
-
#initialize(text) ⇒ TencentFy
constructor
A new instance of TencentFy.
- #json ⇒ Object
- #nonce ⇒ Object
- #orginal_parameters ⇒ Object
- #region ⇒ Object
- #request_object ⇒ Object
- #request_params ⇒ Object
- #result ⇒ Object
- #secret_id ⇒ Object
- #secret_key ⇒ Object
- #sign_str ⇒ Object
- #timestamp ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(text) ⇒ TencentFy
Returns a new instance of TencentFy.
8 9 10 11 |
# File 'lib/tx_translate/tencent_fy.rb', line 8 def initialize(text) @encode_text = text.tr("\n", ' ') @escape_text = CGI.escape(@encode_text) end |
Instance Method Details
#code ⇒ Object
61 62 63 64 65 66 |
# File 'lib/tx_translate/tencent_fy.rb', line 61 def code digest = OpenSSL::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, secret_key, data) encrypt = Base64.encode64(hmac).delete("\n") encrypt.gsub('+', '%2B') # 处理 + 号 end |
#data ⇒ Object
57 58 59 |
# File 'lib/tx_translate/tencent_fy.rb', line 57 def data sign_str + orginal_parameters end |
#encode_parameters ⇒ Object
43 44 45 46 47 |
# File 'lib/tx_translate/tencent_fy.rb', line 43 def encode_parameters 'Action=TextTranslate' + '&Nonce=1234' + '&ProjectId=1257710951' + "&Region=#{region}" \ "&SecretId=#{secret_id}" + '&Source=en' + '&SourceText=' + @escape_text + '&Target=zh' + '&Timestamp=' + + '&Version=2018-03-21' end |
#full_request_url ⇒ Object
72 73 74 |
# File 'lib/tx_translate/tencent_fy.rb', line 72 def full_request_url url + request_params end |
#host ⇒ Object
17 18 19 |
# File 'lib/tx_translate/tencent_fy.rb', line 17 def host "tmt.tencentcloudapi.com" end |
#json ⇒ Object
80 81 82 |
# File 'lib/tx_translate/tencent_fy.rb', line 80 def json JSON.parse(request_object.body) end |
#nonce ⇒ Object
25 26 27 |
# File 'lib/tx_translate/tencent_fy.rb', line 25 def nonce rand(2323) end |
#orginal_parameters ⇒ Object
37 38 39 40 41 |
# File 'lib/tx_translate/tencent_fy.rb', line 37 def orginal_parameters 'Action=TextTranslate' + '&Nonce=1234' + '&ProjectId=1257710951' + "&Region=#{region}" \ "&SecretId=#{secret_id}" + '&Source=en' + '&SourceText=' + @encode_text + '&Target=zh' + '&Timestamp=' + + '&Version=2018-03-21' end |
#region ⇒ Object
13 14 15 |
# File 'lib/tx_translate/tencent_fy.rb', line 13 def region TxTranslate.config[:region] end |
#request_object ⇒ Object
76 77 78 |
# File 'lib/tx_translate/tencent_fy.rb', line 76 def request_object HTTParty.get(url + request_params) end |
#request_params ⇒ Object
68 69 70 |
# File 'lib/tx_translate/tencent_fy.rb', line 68 def request_params "/?#{encode_parameters}&Signature=#{code}" end |
#result ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/tx_translate/tencent_fy.rb', line 84 def result if json['Response']['Error'] json else json['Response']['TargetText'] end end |
#secret_id ⇒ Object
29 30 31 |
# File 'lib/tx_translate/tencent_fy.rb', line 29 def secret_id TxTranslate.config[:secret_id] end |
#secret_key ⇒ Object
33 34 35 |
# File 'lib/tx_translate/tencent_fy.rb', line 33 def secret_key TxTranslate.config[:secret_key] end |
#sign_str ⇒ Object
53 54 55 |
# File 'lib/tx_translate/tencent_fy.rb', line 53 def sign_str "GET#{host}/?" end |
#timestamp ⇒ Object
49 50 51 |
# File 'lib/tx_translate/tencent_fy.rb', line 49 def = Time.now.to_i.to_s end |
#url ⇒ Object
21 22 23 |
# File 'lib/tx_translate/tencent_fy.rb', line 21 def url "https://#{host}" end |