Module: AliyunSms

Defined in:
lib/aliyun_sms.rb,
lib/aliyun_sms/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

API_URL =
'https://sms.aliyuncs.com/'
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.build_params(args = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aliyun_sms.rb', line 58

def build_params(args={})
  {
    "Format":configuration.format,
    "SignatureMethod":configuration.signature_method,
    "SignatureVersion":configuration.signature_version,
    "Version":configuration.version,
    "AccessKeyId":configuration.access_key_id,
    "Action":"SingleSendSms",
    "ParamString":args[:params].to_json,
    "SignName":args[:sign_name],
    "RegionId":configuration.region_id,
    "SignatureNonce":args[:nonce]||SecureRandom.uuid,
    "TemplateCode":args[:tpl_id],
    "Timestamp":args[:timestamp]||timestamp,
    "RecNum":args[:phone],
  }.sort.to_h
end

.configurationObject



32
33
34
# File 'lib/aliyun_sms.rb', line 32

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



28
29
30
# File 'lib/aliyun_sms.rb', line 28

def configure
  yield(configuration)
end

.signature(params = {}) ⇒ Object



52
53
54
55
56
# File 'lib/aliyun_sms.rb', line 52

def signature(params={})
  canonicalized_query_string = URI.encode_www_form_component URI.encode_www_form(params)
  hmac_result = OpenSSL::HMAC.digest("sha1","#{configuration.access_key_secret}&","POST&%2F&#{canonicalized_query_string}")
  URI.encode_www_form_component Base64.encode64(hmac_result).chomp
end

.to(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aliyun_sms.rb', line 36

def to(opts={})
  params = build_params(opts)
  params = params.delete_if { |k,v| v.nil? or v.to_s.empty? }
  begin 
    res = RestClient.post(API_URL, generate_query_body_str(params), :headers=>{"Content-Type":"application/x-www-form-urlencoded"})
    result = JSON.parse(res)
    if res.code==200 and result["Model"] and result["RequestId"]
      return {code:0, msg: "发送成功"}
    else
      return {code:res.code, msg: "发送失败"}
    end
  rescue=>e
    return {code:-1, msg:e.message}
  end
end