class TxTranslate::TencentFy
Public Class Methods
new(text)
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 8 def initialize(text) @encode_text = text.tr("\n", ' ') @escape_text = CGI.escape(@encode_text) end
Public Instance Methods
code()
click to toggle source
# 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()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 57 def data sign_str + orginal_parameters end
encode_parameters()
click to toggle source
# 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=' + timestamp + '&Version=2018-03-21' end
full_request_url()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 72 def full_request_url url + request_params end
host()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 17 def host "tmt.tencentcloudapi.com" end
json()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 80 def json JSON.parse(request_object.body) end
nonce()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 25 def nonce rand(2323) end
orginal_parameters()
click to toggle source
# 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=' + timestamp + '&Version=2018-03-21' end
region()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 13 def region TxTranslate.config[:region] end
request_object()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 76 def request_object HTTParty.get(url + request_params) end
request_params()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 68 def request_params "/?#{encode_parameters}&Signature=#{code}" end
result()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 84 def result if json['Response']['Error'] json else json['Response']['TargetText'] end end
secret_id()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 29 def secret_id TxTranslate.config[:secret_id] end
secret_key()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 33 def secret_key TxTranslate.config[:secret_key] end
sign_str()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 53 def sign_str "GET#{host}/?" end
timestamp()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 49 def timestamp timestamp = Time.now.to_i.to_s end
url()
click to toggle source
# File lib/tx_translate/tencent_fy.rb, line 21 def url "https://#{host}" end