module Qcloud::Sms

Constants

VERSION

Attributes

configuration[W]

Public Class Methods

configuration() click to toggle source
# File lib/qcloud/sms.rb, line 21
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/qcloud/sms.rb, line 25
def configure
  yield(configuration)
end
create_params(mobile, templId, params, random_number) click to toggle source
# File lib/qcloud/sms.rb, line 29
def create_params(mobile, templId, params, random_number)
  sms_params = {
    "ext" => "",
    "extend" => "",
    "params" => params,
    "sig" => sig(mobile, random_number),
    "sign" => configuration.sign,
    "tel" => {
      "mobile" => mobile,
      "nationcode" => "86"
    },
    "time" => timestamp,
    "tpl_id" => templId
  }
  sms_params.to_json
end
random() click to toggle source

生成随机数

# File lib/qcloud/sms.rb, line 61
def random
  SecureRandom.random_number.to_s.slice(-10..-1)
end
sig(mobile, random_number) click to toggle source

生成数字签名

# File lib/qcloud/sms.rb, line 55
def sig(mobile, random_number)
  signature = "appkey=" + configuration.app_key + "&random=" + random_number + "&time=" + timestamp.to_s + "&mobile=" + mobile
  Digest::SHA256.hexdigest signature
end
single_sender(mobile, templId, params) click to toggle source
# File lib/qcloud/sms.rb, line 46
def single_sender(mobile, templId, params)
  random_number = random
  Typhoeus.post("https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=" + configuration.app_id + "&random=" + random_number,
    headers: { "Content-Type": "application/json" },
    body: create_params(mobile, templId, params, random_number)
  )
end
timestamp() click to toggle source

生成短信时间戳

# File lib/qcloud/sms.rb, line 66
def timestamp
  Time.now.to_i
end