Module: Aliyun::Sms
- Defined in:
- lib/aliyun/sms.rb,
lib/aliyun/sms/version.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"1.1.5"
Class Attribute Summary collapse
Class Method Summary collapse
- .canonicalized_query_string(params) ⇒ Object
- .config_params ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.encode(input) ⇒ Object
对字符串进行 PERCENT 编码.
- .get_params(user_params) ⇒ Object
- .get_url(user_params) ⇒ Object
-
.seed_signature_nonce ⇒ Object
生成短信唯一标识码,采用到微秒的时间戳.
-
.seed_timestamp ⇒ Object
生成短信时间戳.
- .send(phone_numbers, template_code, template_param, out_id = '') ⇒ Object
- .send_globle(mobile, text, from, type = 'OTP') ⇒ Object
-
.sign(key_secret, coded_params) ⇒ Object
生成数字签名.
-
.test_query_string(params) ⇒ Object
测试参数未编码时生成的字符串是否正确(多一道保险).
Class Attribute Details
.configuration ⇒ Object
30 31 32 |
# File 'lib/aliyun/sms.rb', line 30 def configuration @configuration ||= Configuration.new end |
Class Method Details
.canonicalized_query_string(params) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/aliyun/sms.rb', line 85 def canonicalized_query_string(params) cqstring = '' params.sort_by{|key, val| key}.each do |key, value| if cqstring.empty? cqstring += "#{encode(key)}=#{encode(value)}" else cqstring += "&#{encode(key)}=#{encode(value)}" end end cqstring end |
.config_params ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/aliyun/sms.rb', line 72 def config_params() params ={ 'AccessKeyId' => configuration.access_key_id, 'Action' => configuration.action, 'Format' => configuration.format, 'RegionId' => configuration.region_id, 'SignName' => configuration.sign_name, 'SignatureMethod' => configuration.signature_method, 'SignatureVersion' => configuration.signature_version, 'Version' => configuration.version } end |
.configure {|configuration| ... } ⇒ Object
34 35 36 |
# File 'lib/aliyun/sms.rb', line 34 def configure yield(configuration) end |
.encode(input) ⇒ Object
对字符串进行 PERCENT 编码
106 107 108 |
# File 'lib/aliyun/sms.rb', line 106 def encode(input) output = url_encode(input) end |
.get_params(user_params) ⇒ Object
68 69 70 |
# File 'lib/aliyun/sms.rb', line 68 def get_params(user_params) params = config_params.merge(user_params) end |
.get_url(user_params) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/aliyun/sms.rb', line 61 def get_url(user_params) params = get_params(user_params) coded_params = canonicalized_query_string(params) key_secret = configuration.access_key_secret url = 'http://dysmsapi.aliyuncs.com/?' + 'Signature=' + sign(key_secret, coded_params) + '&' + coded_params end |
.seed_signature_nonce ⇒ Object
生成短信唯一标识码,采用到微秒的时间戳
116 117 118 |
# File 'lib/aliyun/sms.rb', line 116 def seed_signature_nonce Time.now.utc.strftime("%Y%m%d%H%M%S%L") end |
.seed_timestamp ⇒ Object
生成短信时间戳
111 112 113 |
# File 'lib/aliyun/sms.rb', line 111 def Time.now.utc.strftime("%FT%TZ") end |
.send(phone_numbers, template_code, template_param, out_id = '') ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/aliyun/sms.rb', line 38 def send(phone_numbers, template_code, template_param, out_id = '') Typhoeus.get(get_url({ 'PhoneNumbers' => phone_numbers, 'TemplateCode' => template_code, 'TemplateParam' => template_param, 'OutId' => out_id, 'SignatureNonce' => seed_signature_nonce, 'Timestamp' => })) end |
.send_globle(mobile, text, from, type = 'OTP') ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aliyun/sms.rb', line 49 def send_globle(mobile, text, from, type = 'OTP') Typhoeus.get(get_url({ 'Action' => 'SendMessageToGlobe', 'Message' => text, 'To' => mobile, 'From' => from, 'Type' => type, 'SignatureNonce' => seed_signature_nonce, 'Timestamp' => })) end |
.sign(key_secret, coded_params) ⇒ Object
生成数字签名
98 99 100 101 102 103 |
# File 'lib/aliyun/sms.rb', line 98 def sign(key_secret, coded_params) key = key_secret + '&' signature = 'GET' + '&' + encode('/') + '&' + encode(coded_params) sign = Base64.encode64("#{OpenSSL::HMAC.digest('sha1',key, signature)}") encode(sign.chomp) # 通过chomp去掉最后的换行符 LF end |
.test_query_string(params) ⇒ Object
测试参数未编码时生成的字符串是否正确(多一道保险)
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/aliyun/sms.rb', line 121 def test_query_string(params) qstring = '' params.sort_by{|key, val| key}.each do |key, value| if qstring.empty? qstring += "#{key}=#{value}" else qstring += "&#{key}=#{value}" end end qstring end |