class Aliyun::Sms::PhoneCode

Attributes

configuration[W]
access_key_id[RW]
access_key_secret[RW]
action[RW]
format[RW]
region_id[RW]
sign_name[RW]
signature_method[RW]
signature_version[RW]
sms_version[RW]

Public Class Methods

configuration() click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 36
def configuration
        @configuration ||= PhoneCode.new
end
configure() { |configuration| ... } click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 40
def configure
        yield(configuration)
end
create_params(template_code, phone_number, content) click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 44
                def create_params(template_code, phone_number, content)
                        sms_params ={
  'AccessKeyId'                        => configuration.access_key_id,
  'Action'                                             => configuration.action,
  'Format'                                             => configuration.format,
  'ParamString'                        => message_param,
  'RecNum'                                             => phone_number,
  'RegionId'                                   => configuration.region_id,
  'SignName'                                   => configuration.sign_name,
  'SignatureMethod'    => configuration.signature_method,
  'SignatureNonce'             => seed_signature_nonce,
  'SignatureVersion'   => configuration.signature_version,
  'TemplateCode'                       => template_code,
  'Timestamp'                          => seed_timestamp,
  'Version'                                    => configuration.sms_version,
}
                end
new() click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 21
                def initialize
                        @access_key_secret = ''
@access_key_id = ''
@sign_name = ''
@action ||= 'SingleSendSms'
@format ||= 'JSON' 
@region_id ||= 'cn-hangzhou' 
@signature_method ||= 'HMAC-SHA1'
@signature_version ||= '1.0'
@sms_version ||= '2016-09-27' 
                end
send(template_code, phone_number, params) click to toggle source

TODO: 错误处理

# File lib/aliyun/sms/phone_code.rb, line 63
def send(template_code, phone_number, params)
        Net::HTTP.post( 
                        URI("https://sms.aliyuncs.com"), 
                        headers: {"Content-Type": "application/x-www-form-urlencoded"},
                        body: sign_result(configuration.access_key_secret, params)
                )
end
sign(key_secret, params) click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 75
def sign(key_secret, params)
        signature = 'POST' + '&' + encode('/') + '&' + encode(URI.encode_www_form(params))
        sign = Base64.encode64(OpenSSL::HMAC.digest('sha1', "#{key_secret}&", signature))
        encode(sign.chomp)
end
sign_result(key_secret, params) click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 71
def sign_result(key_secret, params)
        body_data = "Signature=" + sign(key_secret, params) + '&' + query_string(params)
end

Private Class Methods

encode(input) click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 82
def encode(input)
        ERB::Util.url_encode(input)
end
query_string(params) click to toggle source

原生参数拼接成请求字符串

# File lib/aliyun/sms/phone_code.rb, line 87
def query_string(params)
  qstring = ''
  params.each do |key, value|
    if qstring.empty?
      qstring += "#{key}=#{value}"
    else
      qstring += "&#{key}=#{value}"
    end
  end
  return qstring
end
seed_signature_nonce() click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 103
                def seed_signature_nonce
Time.now.utc.strftime("%Y%m%d%H%M%S%L")
                end
seed_timestamp() click to toggle source
# File lib/aliyun/sms/phone_code.rb, line 99
def seed_timestamp
        Time.now.utc.strftime("%FT%TZ")
end