module Aliyun::Vms
Constants
- VERSION
Attributes
configuration[W]
Public Class Methods
canonicalized_query_string(params)
click to toggle source
# File lib/aliyun/vms.rb, line 74 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()
click to toggle source
# File lib/aliyun/vms.rb, line 62 def config_params() params ={ 'AccessKeyId' => configuration.access_key_id, 'Action' => configuration.action, 'Format' => configuration.format, 'RegionId' => configuration.region_id, 'SignatureMethod' => configuration.signature_method, 'SignatureVersion' => configuration.signature_version, 'Version' => configuration.version } end
configuration()
click to toggle source
# File lib/aliyun/vms.rb, line 31 def configuration @configuration ||= Configuration.new end
configure() { |configuration| ... }
click to toggle source
# File lib/aliyun/vms.rb, line 35 def configure yield(configuration) end
encode(input)
click to toggle source
对字符串进行 PERCENT 编码
# File lib/aliyun/vms.rb, line 95 def encode(input) output = url_encode(input) end
get_params(user_params)
click to toggle source
# File lib/aliyun/vms.rb, line 58 def get_params(user_params) params = config_params.merge(user_params) end
get_url(user_params)
click to toggle source
# File lib/aliyun/vms.rb, line 51 def get_url(user_params) params = get_params(user_params) coded_params = canonicalized_query_string(params) key_secret = configuration.access_key_secret url = 'http://dyvmsapi.aliyuncs.com/?' + 'Signature=' + sign(key_secret, coded_params) + '&' + coded_params end
seed_signature_nonce()
click to toggle source
生成每次请求唯一的防重放攻击,
# File lib/aliyun/vms.rb, line 105 def seed_signature_nonce # Time.now.utc.strftime("%Y%m%d%H%M%S%L") SecureRandom.uuid end
seed_timestamp()
click to toggle source
生成语音时间戳
# File lib/aliyun/vms.rb, line 100 def seed_timestamp Time.now.utc.strftime("%FT%TZ") end
send(called_show_number, called_number, tts_code, tts_param, out_id = '')
click to toggle source
# File lib/aliyun/vms.rb, line 39 def send(called_show_number, called_number, tts_code, tts_param, out_id = '') Typhoeus.get(get_url({ 'CalledShowNumber' => called_show_number, 'CalledNumber' => called_number, 'TtsCode' => tts_code, 'TtsParam' => tts_param, 'OutId' => out_id, 'SignatureNonce' => seed_signature_nonce, 'Timestamp' => seed_timestamp })) end
sign(key_secret, coded_params)
click to toggle source
生成数字签名
# File lib/aliyun/vms.rb, line 87 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)
click to toggle source
测试参数未编码时生成的字符串是否正确(多一道保险)
# File lib/aliyun/vms.rb, line 111 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