Class: Aliyun::Sms::PhoneCode

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/sms/phone_code.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePhoneCode

Returns a new instance of PhoneCode.



21
22
23
24
25
26
27
28
29
30
31
# 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

Class Attribute Details

.configurationObject



36
37
38
# File 'lib/aliyun/sms/phone_code.rb', line 36

def configuration
	@configuration ||= PhoneCode.new
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def access_key_id
  @access_key_id
end

#access_key_secretObject

Returns the value of attribute access_key_secret.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def access_key_secret
  @access_key_secret
end

#actionObject

Returns the value of attribute action.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def action
  @action
end

#formatObject

Returns the value of attribute format.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def format
  @format
end

#region_idObject

Returns the value of attribute region_id.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def region_id
  @region_id
end

#sign_nameObject

Returns the value of attribute sign_name.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def sign_name
  @sign_name
end

#signature_methodObject

Returns the value of attribute signature_method.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def signature_method
  @signature_method
end

#signature_versionObject

Returns the value of attribute signature_version.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def signature_version
  @signature_version
end

#sms_versionObject

Returns the value of attribute sms_version.



12
13
14
# File 'lib/aliyun/sms/phone_code.rb', line 12

def sms_version
  @sms_version
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



40
41
42
# File 'lib/aliyun/sms/phone_code.rb', line 40

def configure
	yield(configuration)
end

.create_params(template_code, phone_number, content) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

.send(template_code, phone_number, params) ⇒ Object

TODO: 错误处理



63
64
65
66
67
68
69
# 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) ⇒ Object



75
76
77
78
79
# 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) ⇒ Object



71
72
73
# 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