class TxNlp::Base

Constants

HOST

Public Instance Methods

encode_parameters() click to toggle source
# File lib/tx_nlp/base.rb, line 34
def encode_parameters
  new_hash = fixed_paramaters
  new_hash['Action'] = @action_type
  new_hash['Text'] = @escape_text
  to_params(new_hash)
end
fixed_paramaters() click to toggle source
# File lib/tx_nlp/base.rb, line 41
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() click to toggle source
# File lib/tx_nlp/base.rb, line 65
def full_request_url
  url + request_params
end
indent_result() click to toggle source
# File lib/tx_nlp/base.rb, line 85
def indent_result
  if json['Response']['Error']
    json
  else
    new_content = raw_result.join(',')

    new_content
  end
end
json() click to toggle source
# File lib/tx_nlp/base.rb, line 73
def json
  JSON.parse(request_object.body)
end
orginal_parameters() click to toggle source
# File lib/tx_nlp/base.rb, line 27
def orginal_parameters
  new_hash = fixed_paramaters
  new_hash['Action'] = @action_type
  new_hash['Text'] = @encode_text
  to_params(new_hash)
end
raw_result() click to toggle source
# File lib/tx_nlp/base.rb, line 77
def raw_result
  if json['Response']['Error']
    json
  else
    original_data = json['Response']["SimilarWords"]
  end
end
region() click to toggle source
# File lib/tx_nlp/base.rb, line 11
def region
  TxNlp.config[:region]
end
request_object() click to toggle source
# File lib/tx_nlp/base.rb, line 69
def request_object
  HTTParty.get(url + request_params)
end
request_params() click to toggle source
# File lib/tx_nlp/base.rb, line 61
def request_params
  "/?#{encode_parameters}&Signature=#{signature}"
end
secret_id() click to toggle source
# File lib/tx_nlp/base.rb, line 19
def secret_id
  TxNlp.config[:secret_id]
end
secret_key() click to toggle source
# File lib/tx_nlp/base.rb, line 23
def secret_key
  TxNlp.config[:secret_key]
end
signature() click to toggle source
# File lib/tx_nlp/base.rb, line 54
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() click to toggle source
# File lib/tx_nlp/base.rb, line 50
def string_for_sign
  "GET#{HOST}/?" + orginal_parameters
end
to_params(original_hash) click to toggle source
# File lib/tx_nlp/base.rb, line 45
def to_params(original_hash)
  sorted_by_key = Hash[original_hash.sort]
  sorted_by_key.map { |x| "#{x[0]}=#{x[1]}" }.join('&')
end
url() click to toggle source
# File lib/tx_nlp/base.rb, line 15
def url
  "https://#{HOST}"
end