Class: TxNlp::Base
- Inherits:
-
Object
- Object
- TxNlp::Base
- Defined in:
- lib/tx_nlp/base.rb
Direct Known Subclasses
Constant Summary collapse
- HOST =
'nlp.tencentcloudapi.com'
Instance Method Summary collapse
- #encode_parameters ⇒ Object
- #fixed_paramaters ⇒ Object
- #full_request_url ⇒ Object
- #indent_result ⇒ Object
- #json ⇒ Object
- #orginal_parameters ⇒ Object
- #raw_result ⇒ Object
- #region ⇒ Object
- #request_object ⇒ Object
- #request_params ⇒ Object
- #secret_id ⇒ Object
- #secret_key ⇒ Object
- #signature ⇒ Object
- #string_for_sign ⇒ Object
- #to_params(original_hash) ⇒ Object
- #url ⇒ Object
Instance Method Details
#encode_parameters ⇒ Object
42 43 44 45 46 47 |
# File 'lib/tx_nlp/base.rb', line 42 def encode_parameters new_hash = fixed_paramaters new_hash['Action'] = @action_type new_hash['Text'] = @escape_text to_params(new_hash) end |
#fixed_paramaters ⇒ Object
49 50 51 |
# File 'lib/tx_nlp/base.rb', line 49 def fixed_paramaters { 'Nonce' => '2373', 'Region' => region, 'SecretId' => secret_id, 'Timestamp' => Time.now.to_i.to_s, 'Version' => '2019-04-08' } end |
#full_request_url ⇒ Object
73 74 75 |
# File 'lib/tx_nlp/base.rb', line 73 def full_request_url url + request_params end |
#indent_result ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/tx_nlp/base.rb', line 93 def indent_result if json['Response']['Error'] json else new_content = raw_result.join(',') new_content end end |
#json ⇒ Object
81 82 83 |
# File 'lib/tx_nlp/base.rb', line 81 def json JSON.parse(request_object.body) end |
#orginal_parameters ⇒ Object
35 36 37 38 39 40 |
# File 'lib/tx_nlp/base.rb', line 35 def orginal_parameters new_hash = fixed_paramaters new_hash['Action'] = @action_type new_hash['Text'] = @encode_text to_params(new_hash) end |
#raw_result ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/tx_nlp/base.rb', line 85 def raw_result if json['Response']['Error'] json else original_data = json['Response']["SimilarWords"] end end |
#region ⇒ Object
19 20 21 |
# File 'lib/tx_nlp/base.rb', line 19 def region TxNlp.config[:region] end |
#request_object ⇒ Object
77 78 79 |
# File 'lib/tx_nlp/base.rb', line 77 def request_object HTTParty.get(url + request_params) end |
#request_params ⇒ Object
69 70 71 |
# File 'lib/tx_nlp/base.rb', line 69 def request_params "/?#{encode_parameters}&Signature=#{signature}" end |
#secret_id ⇒ Object
27 28 29 |
# File 'lib/tx_nlp/base.rb', line 27 def secret_id TxNlp.config[:secret_id] end |
#secret_key ⇒ Object
31 32 33 |
# File 'lib/tx_nlp/base.rb', line 31 def secret_key TxNlp.config[:secret_key] end |
#signature ⇒ Object
62 63 64 65 66 67 |
# File 'lib/tx_nlp/base.rb', line 62 def signature digest = OpenSSL::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, secret_key, string_for_sign) encrypt = Base64.encode64(hmac).delete("\n") encrypt.gsub('+', '%2B') # 处理 + 号 end |
#string_for_sign ⇒ Object
58 59 60 |
# File 'lib/tx_nlp/base.rb', line 58 def string_for_sign "GET#{HOST}/?" + orginal_parameters end |
#to_params(original_hash) ⇒ Object
53 54 55 56 |
# File 'lib/tx_nlp/base.rb', line 53 def to_params(original_hash) sorted_by_key = Hash[original_hash.sort] sorted_by_key.map { |x| "#{x[0]}=#{x[1]}" }.join('&') end |
#url ⇒ Object
23 24 25 |
# File 'lib/tx_nlp/base.rb', line 23 def url "https://#{HOST}" end |