module Alidayu::Helper

Public Instance Methods

get_response(params) click to toggle source

获得响应结果

# File lib/alidayu/helper.rb, line 7
def get_response(params)
  uri = URI(Alidayu.config.server)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request = Net::HTTP::Post.new(uri.path)
  request_params = build_params(params)
  request.add_field('Content-Type', 'application/json')
  request.set_form_data(request_params)
  response = http.request(request)
  Alidayu.logger.info response.body
  return JSON.parse(response.body)
end

Private Instance Methods

build_params(params) click to toggle source

构建请求参数

# File lib/alidayu/helper.rb, line 25
def build_params(params)
  params[:app_key] = params[:app_key] || Alidayu.config.app_key
  params[:timestamp] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  params[:format] = 'json'
  params[:v] = '2.0'
  params[:sign_method] = 'hmac'

  signature = generate_signature(params)

  params[:sign] = signature
  return params
end
generate_signature(params) click to toggle source

生成签名 hmac

# File lib/alidayu/helper.rb, line 39
def generate_signature(params)
  params.delete(:sign)
  data = params.sort.compact.join
  digest = OpenSSL::Digest.new('md5')
  secret = params[:app_secret] || Alidayu.config.app_secret
  hmac_sign = OpenSSL::HMAC.hexdigest(digest, secret, data).upcase
  return hmac_sign
end