ali_sms 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c292bcf4cd2088721b5b94ab37efcad8e95314c
4
+ data.tar.gz: e2c01601bc5f3496825687eff0228c8888481bf8
5
+ SHA512:
6
+ metadata.gz: cfb62ed7a965483b248e0e3364335955fcc39ff87a739f9bb2cdd274cb385cc5040c5421bdebab371953bd16fc6f124c7d93bbea61c1de0d580083430ecb11b7
7
+ data.tar.gz: e5e1d9c5fc34256d37b4a9075984bcf1415ab5de5b3bbdd2f15773686da1c33008c99a2d88b60ee234836cb09f6e5a8d8d095592ffa252f7d9459167c20f34ca
@@ -0,0 +1,4 @@
1
+ require 'ali_sms/config'
2
+ require 'ali_sms/curl'
3
+ require 'ali_sms/sms'
4
+ require 'ali_sms/util'
@@ -0,0 +1,24 @@
1
+ require 'yaml'
2
+ require 'singleton'
3
+
4
+ module AliSms
5
+
6
+ class Config
7
+ include Singleton
8
+
9
+ def initialize()
10
+ configHash = YAML.load_file(Rails.root.to_s + '/config/ali_sms.yml')
11
+
12
+ if configHash.nil?
13
+ raise "ali_sms.yml为空"
14
+ end
15
+
16
+ configHash.each do |k, v|
17
+ Object.send :define_method, k do
18
+ v
19
+ end
20
+ end
21
+ end # initialize .. end
22
+
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require 'net/http'
2
+
3
+ module AliSms
4
+ module Curl
5
+ def self.request_url(url)
6
+ uri = URI(url)
7
+ request = Net::HTTP::Get.new(uri.request_uri)
8
+
9
+ http = Net::HTTP.new(uri.host, uri.port)
10
+ http.read_timeout = 10
11
+ http.open_timeout = 10
12
+
13
+
14
+ res = http.request(request)
15
+
16
+ if res.is_a? Net::HTTPSuccess
17
+ result = res.body
18
+ else
19
+ result = {}
20
+ end
21
+
22
+ return result
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,95 @@
1
+ require 'json'
2
+ require 'singleton'
3
+
4
+ module AliSms
5
+
6
+ class Sms
7
+ include Singleton
8
+
9
+ def initialize()
10
+ @config = AliSms::Config.instance
11
+ end
12
+
13
+ # option_hash
14
+ # 需提供参数PhoneNumbers, 它可以是字符串和数组
15
+ # TemplateCode 模板code
16
+ # 可选:TemplateParam 传递hash,填充模板的参数值
17
+ # 返回值:{ Message: , RequestId: , BizId: , Code: }
18
+ def send_sms(option_hash = {})
19
+ option = option_hash.deep_stringify_keys
20
+
21
+ option["AccessKeyId"] = @config.AccessKeyId
22
+ option["Timestamp"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
23
+ option["SignatureMethod"] = "HMAC-SHA1"
24
+ option["SignatureVersion"] = "1.0"
25
+ option["SignatureNonce"] = SecureRandom.hex(16)
26
+ option["SignName"] = "易百事"
27
+ option["Format"] = "JSON"
28
+
29
+ if option["PhoneNumbers"].present?
30
+ option["PhoneNumbers"] = Array.wrap(option["PhoneNumbers"]).join(",")
31
+ end
32
+ option["Action"] = "SendSms"
33
+ option["Version"] = "2017-05-25"
34
+ option["RegionId"] = "cn-hangzhou"
35
+ option["TemplateParam"] = option["TemplateParam"].to_json
36
+
37
+ query_str = AliSms::Util.make_query_str(option)
38
+ sign = AliSms::Util.make_sign(query_str, "GET", @config.AccessKeySecret + "&")
39
+
40
+ url = "http://dysmsapi.aliyuncs.com/?Signature=" + sign + "&" + query_str
41
+
42
+ response_body = AliSms::Curl.request_url(url)
43
+ result = JSON.parse(response_body).with_indifferent_access
44
+
45
+ # if result.empty?
46
+ # result = { Message: "请求错误", RequestId: "request_error", BizId: 0 }
47
+ # end
48
+
49
+ return result
50
+ end
51
+
52
+ # option_hash
53
+ # 需提供参数PhoneNumber, 手机号
54
+ # SendDate 年月日字符串,如20171116
55
+ # PageSize 结果数量
56
+ # CurrentPage 当前页数 以1开始
57
+ # 可选参数BizId,发送流水号
58
+ #
59
+ # 返回值:Hash,
60
+ # 详见https://help.aliyun.com/document_detail/55452.html?spm=5176.doc59840.6.563.lxmKXd
61
+ # 此方法暂未使用
62
+ def query_sms(option_hash = {})
63
+ option = option_hash.stringify_keys
64
+
65
+ option["AccessKeyId"] = @config.AccessKeyId
66
+ option["Timestamp"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
67
+ option["Format"] = "JSON"
68
+ option["SignatureMethod"] = "HMAC-SHA1"
69
+ option["SignatureVersion"] = "1.0"
70
+ option["SignatureNonce"] = SecureRandom.hex(16)
71
+ option["Action"] = "QuerySendDetails"
72
+ option["Version"] = "2017-05-25"
73
+ option["RegionId"] = "cn-hangzhou"
74
+
75
+ # 业务参数
76
+ #option["PhoneNumber"] = "18733848323"
77
+ #option["SendDate"] = "20171116"
78
+ option["PageSize"] = option["PageSize"] || "10"
79
+ option["CurrentPage"] = option["CurrentPage"] || "1"
80
+
81
+ query_str = AliSms::Util.make_query_str(option)
82
+ sign = AliSms::Util.make_sign(query_str, "GET", @config.AccessKeySecret + "&")
83
+
84
+ url = "http://dysmsapi.aliyuncs.com/?Signature=" + sign + "&" + query_str
85
+ response_body = AliSms::Curl.request_url(url)
86
+ result = JSON.parse(response_body).with_indifferent_access
87
+
88
+ # if result.empty?
89
+ # result = { Message: "请求错误", RequestId: "request_error", BizId: 0 }
90
+ # end
91
+
92
+ return result
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,44 @@
1
+ require "uri"
2
+ require 'openssl'
3
+ require "base64"
4
+
5
+ module AliSms
6
+
7
+ module Util
8
+
9
+ def self.make_query_str(dataHash)
10
+ sortHash = Hash[dataHash.sort_by { |k, v| k }]
11
+ if sortHash.has_key?("sign")
12
+ sortHash.delete("sign")
13
+ end
14
+
15
+ sortQueryStr = ""
16
+ sortHash.each do |k, v|
17
+ sortQueryStr = sortQueryStr + "&" + specialUrlEncode(k) + "=" + specialUrlEncode(v)
18
+ end
19
+ sortQueryStr = sortQueryStr[1..-1]
20
+
21
+ return sortQueryStr
22
+ end
23
+
24
+ def self.make_sign(sortQueryStr, httpMethod, key)
25
+ str = httpMethod + "&" + specialUrlEncode("/") + "&" + specialUrlEncode(sortQueryStr)
26
+
27
+ digest = OpenSSL::Digest.new("sha1")
28
+ str_8 = str.encode("UTF-8")
29
+ hash = OpenSSL::HMAC.digest(digest, key, str_8)
30
+ base64_str = Base64.encode64(hash).chomp
31
+
32
+ sign = specialUrlEncode(base64_str)
33
+
34
+ return sign
35
+ end
36
+
37
+ # 此方法没有问题
38
+ def self.specialUrlEncode(value)
39
+ str = CGI.escape(value)
40
+ return str.gsub("+", "%20").gsub("*", "%2A").gsub("%7E", "~")
41
+ end
42
+
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ali_sms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "袁帅"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "阿里云短信接口"
14
+ email: bysxrokr@qq.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ali_sms.rb
20
+ - lib/ali_sms/config.rb
21
+ - lib/ali_sms/curl.rb
22
+ - lib/ali_sms/sms.rb
23
+ - lib/ali_sms/util.rb
24
+ homepage: http://rubygems.org/gems/ali_sms
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: "阿里云短信接口"
48
+ test_files: []