Class: TxTranslate::TencentFy

Inherits:
Object
  • Object
show all
Defined in:
lib/tx_translate/tencent_fy.rb

Instance Method Summary collapse

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

#codeObject



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

#dataObject



57
58
59
# File 'lib/tx_translate/tencent_fy.rb', line 57

def data
  sign_str + orginal_parameters
end

#encode_parametersObject



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=' + timestamp + '&Version=2018-03-21'
end

#full_request_urlObject



72
73
74
# File 'lib/tx_translate/tencent_fy.rb', line 72

def full_request_url
  url + request_params
end

#hostObject



17
18
19
# File 'lib/tx_translate/tencent_fy.rb', line 17

def host
  "tmt.tencentcloudapi.com"
end

#jsonObject



80
81
82
# File 'lib/tx_translate/tencent_fy.rb', line 80

def json
  JSON.parse(request_object.body)
end

#nonceObject



25
26
27
# File 'lib/tx_translate/tencent_fy.rb', line 25

def nonce
  rand(2323)
end

#orginal_parametersObject



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=' + timestamp + '&Version=2018-03-21'
end

#regionObject



13
14
15
# File 'lib/tx_translate/tencent_fy.rb', line 13

def region
  TxTranslate.config[:region]
end

#request_objectObject



76
77
78
# File 'lib/tx_translate/tencent_fy.rb', line 76

def request_object
  HTTParty.get(url + request_params)
end

#request_paramsObject



68
69
70
# File 'lib/tx_translate/tencent_fy.rb', line 68

def request_params
  "/?#{encode_parameters}&Signature=#{code}"
end

#resultObject



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_idObject



29
30
31
# File 'lib/tx_translate/tencent_fy.rb', line 29

def secret_id
  TxTranslate.config[:secret_id]
end

#secret_keyObject



33
34
35
# File 'lib/tx_translate/tencent_fy.rb', line 33

def secret_key
  TxTranslate.config[:secret_key]
end

#sign_strObject



53
54
55
# File 'lib/tx_translate/tencent_fy.rb', line 53

def sign_str
  "GET#{host}/?"
end

#timestampObject



49
50
51
# File 'lib/tx_translate/tencent_fy.rb', line 49

def timestamp
  timestamp = Time.now.to_i.to_s
end

#urlObject



21
22
23
# File 'lib/tx_translate/tencent_fy.rb', line 21

def url
  "https://#{host}"
end