aliyun-sms 0.1.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2247637593d8cf945a9967ad62fdc18746bdac37
4
- data.tar.gz: 0ec58248ed765666d4070bb08f7edd9dfb5aa55d
3
+ metadata.gz: 5f986f83319c6431308d094dab9a471c0902003a
4
+ data.tar.gz: 5d464bb2a11f567e03ab50e2b87b74a4b4395bd7
5
5
  SHA512:
6
- metadata.gz: 398b0bde9e5db517660b50512a6570972c3ac3678f53d96a2ca6c289e7853d890334f180281ba3d259e54383ea7a56718bfe3fa3aca0926ee8ae077fcdb64b9b
7
- data.tar.gz: d6619bee88687b7a574a094e27f70e68a943656e84c695b893c1cc37a9ca1260950d4c68dd422f223f573c4b3085b480883e4522527816fed551a0b33a7fec5c
6
+ metadata.gz: b1c10e6e10fca238b7b6004e0c882d0009bc835313c5d87a9caef8a4af5edde6eb10d944da547573d544ce6d544bba544b80d65c606f34b139d75fb82820578c
7
+ data.tar.gz: 3c05f1210454f389b744e60787ddc31182d0a86c3f0994b9d1e0e10af1d468a05d3599c9dd64d7d49f9a5eda1aa0e99b19e51d7ced0e742f571156b8bf5a5757
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at victor-luo@outlook.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aliyun-sms.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 VICTOR LUO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,195 @@
1
+ ## [中文说明](./README.zh-CN.md)
2
+
3
+ # Aliyun::Sms Ruby Gem for Aliyun Short Message Service(aliyun-sms)
4
+
5
+ **Comfortable to aliyun sms *2017-05-25 version* api**
6
+
7
+ ## Notice
8
+
9
+ Currently, aliyun has three services of short message,named:
10
+
11
+ 1. ali-dayu(阿里大于)
12
+ 2. aliyun-mq(阿里云消息队列)
13
+ 3. aliyun-sms(阿里云短信服务)
14
+
15
+ They all are usable, and will merge togather in future. But before merged, they are Independent, and every user only use one between them.
16
+
17
+ **This gem only for aliyun-sms(阿里云短信服务), and you must confirm that you just use it. If you can't confirm, please see the document [阿里云短信业务说明](https://help.aliyun.com/document_detail/63097.html?spm=a2c4g.11186623.6.542.6fZlRU) to get guidance.**
18
+
19
+ ## Before
20
+
21
+ Before use aliyun-sms, you must apply and open the aliyun-sms, and get the parameters as follows:
22
+
23
+ 1. ACCESS\_KEY\_SECRET: apply on aliyun console
24
+ 2. ACCESS\_KEY\_ID: apply on aliyun console
25
+ 3. SIGN\_NAME: get when open the aliyun-sms
26
+ 4. TEMPLATE\_CODE: get when open the aliyun-sms
27
+
28
+ ## Installation
29
+
30
+ ### Ruby Common Method
31
+
32
+
33
+ ```ruby
34
+ gem install aliyun-sms
35
+ ```
36
+
37
+ ### Rails Method
38
+
39
+ Add in Gemfile :
40
+
41
+ ```ruby
42
+ gem 'aliyun-sms' # Ruby Gems 安装源
43
+ ```
44
+
45
+ Run command:
46
+
47
+ ```ruby
48
+ bundle
49
+ ```
50
+
51
+ ### Download Method
52
+
53
+ Clone and install by rake.
54
+
55
+ ```bash
56
+ git clone https://github.com/VICTOR-LUO-F/aliyun-sms.git
57
+
58
+ cd aliyun-sms
59
+
60
+ rake build
61
+
62
+ rake install
63
+ ```
64
+
65
+ ### Problem and Resolve
66
+
67
+ ```ruby
68
+ require 'aliyun/sms'
69
+ ```
70
+
71
+ If you get a error as follows, when running above command:
72
+
73
+ > ./config/initializers/aliyun-sms.rb:1:in `<top (required)>': uninitialized constant Aliyun::Sms (NameError)
74
+
75
+ You could switch to github resource, and modify the the Rails Gemfile to:
76
+
77
+ ```ruby
78
+ gem 'aliyun-sms', '1.1.0', git: 'https://github.com/VICTOR-LUO-F/aliyun-sms.git'
79
+ ```
80
+
81
+ ## Usage
82
+
83
+ ### Ruby Common Program(irb)
84
+
85
+ #### First Step:
86
+
87
+ ```bash
88
+ $ require 'aliyun/sms'
89
+ ```
90
+
91
+ return
92
+
93
+ ```bash
94
+ => true
95
+ ```
96
+
97
+ #### Second Step:
98
+
99
+
100
+ ```ruby
101
+ $ Aliyun::Sms.configure do |config|
102
+ config.access_key_secret = ACCESS_KEY_SECRET
103
+ config.access_key_id = ACCESS_KEY_ID
104
+ config.action = 'SendSms' # default value
105
+ config.format = 'XML' # http return format, value is 'JSON' or 'XML'
106
+ config.region_id = 'cn-hangzhou' # default value
107
+ config.sign_name = SIGN_NAME
108
+ config.signature_method = 'HMAC-SHA1' # default value
109
+ config.signature_version = '1.0' # default value
110
+ config.version = '2017-05-25' # default value
111
+ end
112
+
113
+ ```
114
+ return
115
+
116
+ ```ruby
117
+ => "2017-05-25"
118
+ ```
119
+
120
+ #### Third Step:
121
+
122
+ Send message:
123
+
124
+ $ Aliyun::Sms.send(phone_numbers, template_code, template_param, out_id)
125
+
126
+ Explanation:
127
+
128
+ 1. phone_numbers: the phone number, string type, such as '1234567890'. You can use multiple phone numbers devided by comma, such as '1234567890,12388888888'.
129
+ 2. template\_code: message template code, string type, such as 'SMS_12345678'.
130
+ 3. template_param: message template params, tring type, such as '{"code":"666666", "product":"content" }'.
131
+ 4. out_id:out extension id,string type,could be null。
132
+
133
+
134
+ ### Rails Application
135
+
136
+ #### First Step:
137
+
138
+ In Rails direction 'config/initializers/', create file 'aliyun-sms.rb', and add code:
139
+
140
+
141
+ config/initializers/aliyun-sms.rb
142
+
143
+ ```ruby
144
+ Aliyun::Sms.configure do |config|
145
+ config.access_key_secret = ACCESS_KEY_SECRET
146
+ config.access_key_id = ACCESS_KEY_ID
147
+ config.action = 'SendSms' # default value
148
+ config.format = 'XML' # http return format, value is 'JSON' or 'XML'
149
+ config.region_id = 'cn-hangzhou' # default value
150
+ config.sign_name = SIGN_NAME
151
+ config.signature_method = 'HMAC-SHA1' # default value
152
+ config.signature_version = '1.0' # default value
153
+ config.version = '2017-05-25' # default value
154
+ end
155
+ ```
156
+ then, restart Rails application。
157
+
158
+ #### Second Step:
159
+
160
+ Send message in your program:
161
+
162
+ ```ruby
163
+ Aliyun::Sms.send(phone_numbers, template_code, template_param, out_id)
164
+ ```
165
+
166
+ Explanation:
167
+
168
+ 1. phone_numbers: the phone number, string type, such as '1234567890'. You can use multiple phone numbers devided by comma, such as '1234567890,12388888888'.
169
+ 2. template\_code: message template code, string type, such as 'SMS_12345678'.
170
+ 3. template_param: message template params, tring type, such as '{"code":"666666", "product":"content" }'.
171
+ 4. out_id:out extension id,string type,could be null。
172
+
173
+ Specially:
174
+
175
+ In Rails application program, you could organize your message content as a hash, and trans it bo be a param of Aliyun::Sms.send by to_json method. For instance:
176
+
177
+ ```ruby
178
+ ...
179
+ phone_number = '1234567890'
180
+ template_code = 'SMS_12345678'
181
+ template_param = {"code"=>"666666", "product"=>"conten" }.to_json
182
+ Aliyun::Sms.send(phone_numbers, template_code, template_param)
183
+ ...
184
+ ```
185
+
186
+ ## Development
187
+
188
+ According to [Aliyun Official Document](https://help.aliyun.com/document_detail/56189.html?spm=a2c4g.11186623.6.580.o8Fm0S) example, I had written spect test. You could run the test after clone as follows:
189
+
190
+ $ bundle exec rspec spec
191
+
192
+
193
+ ## License
194
+
195
+ MIT License。 [MIT License](http://opensource.org/licenses/MIT).
data/README.zh-CN.md ADDED
@@ -0,0 +1,200 @@
1
+ ## [ENGLISH README](./README.md)
2
+
3
+ # Aliyun::Sms 阿里云短信服务 Ruby Gem aliyun-sms
4
+
5
+ **适用于阿里云短信服务最新的 *2017-05-25* 版接口**
6
+
7
+ ## 特别说明
8
+
9
+ 目前阿里云通信提供三个短信产品,分别是:
10
+
11
+ 1. 原阿里大于
12
+ 2. 阿里云消息服务
13
+ 3. 阿里云短信服务
14
+
15
+ 这三个短信服务均可使用,并将最终深度融合,综合为一个产品。在合并之前,请注意以下几点:
16
+
17
+ * 各短信产品账号独立、资金独立;
18
+ * 各短信产品内使用数据独立;
19
+ * 各短信产品具有自己的 API&SDK,不可兼容使用;
20
+ * 同一账号只可使用其中一个产品,不可使用多个产品。
21
+
22
+ **这个 gem 针对的是前面所述的第 3 个短信产品,即“阿里云短信服务”。请您在使用 aliyun-sms 前,确认自己申请的阿里云短信产品是“阿里云短信服务”,如果无法确定,请参考官方说明文档[阿里云短信业务说明](https://help.aliyun.com/document_detail/63097.html?spm=a2c4g.11186623.6.542.6fZlRU)进行区分。**
23
+
24
+ ## 使用前提
25
+
26
+ 使用 aliyun-sms 前,您必须已经是阿里云注册用户,申请开通了“阿里云短信服务”,并且获得了以下几个关键参数:
27
+
28
+ 1. ACCESS\_KEY\_SECRET: 阿里云用户密钥,在阿里云控制台申请获取。
29
+ 2. ACCESS\_KEY\_ID: 阿里云用户 ID, 在阿里云控制台申请获取。
30
+ 3. SIGN\_NAME: 短信签名,在阿里云申请开通短信服务时获取。
31
+ 4. TEMPLATE\_CODE: 短信模版代码,在阿里云申请开通短信服务时申请获取。
32
+
33
+ ## Installation 安装
34
+
35
+ ### Ruby 通用安装方法
36
+ 在命令行中输入命令(电脑已经安装 gems 命令行工具)
37
+
38
+ ```ruby
39
+ gem install aliyun-sms
40
+ ```
41
+
42
+ ### Rails 应用安装方法
43
+
44
+ 在应用的 Gemfile 文件中添加 Ruby Gems 安装源:
45
+
46
+ ```ruby
47
+ gem 'aliyun-sms' # Ruby Gems 安装源
48
+ ```
49
+
50
+ 应用的根目录下运行:
51
+
52
+ ```ruby
53
+ bundle
54
+ ```
55
+
56
+ ### 下载后安装
57
+
58
+ 使用 Git 将代码克隆到本地后使用 Rake 命令安装
59
+
60
+ ```bash
61
+ git clone https://github.com/VICTOR-LUO-F/aliyun-sms.git
62
+
63
+ cd aliyun-sms
64
+
65
+ rake build
66
+
67
+ rake install
68
+ ```
69
+
70
+ ### 安装可能遇到的问题及其解决方式
71
+
72
+ 安装后,如果在 irb 命令行输入命令
73
+
74
+ ```ruby
75
+ require 'aliyun/sms'
76
+ ```
77
+
78
+ 后,无法正确获取 Gem 引用,或者在 Rails 启动时报错提示如下:
79
+
80
+ > ./config/initializers/aliyun-sms.rb:1:in `<top (required)>': uninitialized constant Aliyun::Sms (NameError)
81
+
82
+ 可以改为 Github 安装源,例如 Rails Gemfile 文件引用可以改为下面格式,即可正确安装。
83
+
84
+ ```ruby
85
+ gem 'aliyun-sms', git: 'https://github.com/VICTOR-LUO-F/aliyun-sms.git'
86
+ ```
87
+
88
+ ## Usage 使用
89
+
90
+ ### Ruby 程序通用方法(irb命令行示例)
91
+
92
+ #### 第一步:
93
+
94
+ ```bash
95
+ $ require 'aliyun/sms'
96
+ ```
97
+
98
+ ```bash
99
+ => true
100
+ ```
101
+
102
+ #### 第二步:
103
+
104
+ 参数设置:
105
+
106
+ ```ruby
107
+ $ Aliyun::Sms.configure do |config|
108
+ config.access_key_secret = ACCESS_KEY_SECRET # 阿里云接入密钥,在阿里云控制台申请
109
+ config.access_key_id = ACCESS_KEY_ID # 阿里云接入 ID, 在阿里云控制台申请
110
+ config.action = 'SendSms' # 默认设置
111
+ config.format = 'XML' # 短信推送返回信息格式,可以填写 'JSON'或者'XML'
112
+ config.region_id = 'cn-hangzhou' # 默认设置,如果没有特殊需要,可以不改
113
+ config.sign_name = SIGN_NAME # 短信签名,在阿里云申请开通短信服务时申请获取
114
+ config.signature_method = 'HMAC-SHA1' # 加密算法,默认设置
115
+ config.signature_version = '1.0' # 签名版本,默认设置,不用修改
116
+ config.version = '2017-05-25' # 服务版本,默认设置,不用修改
117
+ end
118
+
119
+ ```
120
+ 返回
121
+
122
+ ```ruby
123
+ => "2017-05-25"
124
+ ```
125
+
126
+ #### 第三步:
127
+
128
+ 发送短信:
129
+
130
+ $ Aliyun::Sms.send(phone_numbers, template_code, template_param, out_id)
131
+
132
+ 参数说明:
133
+
134
+ 1. phone_numbers: 接收短信的手机号,必须为字符型,例如 '1234567890',如果有多个号码,中间用","隔开,例如:'1234567890,12388888888';
135
+ 2. template\_code: 短信模版代码,必须为字符型,申请开通短信服务后,由阿里云提供,例如 'SMS_12345678';
136
+ 3. template_param: 请求字符串,向短信模版提供参数,必须是转为字符串的json格式对象,例如 '{"code":"666666", "product":"测试网站" }';
137
+ 4. out_id:外部流水扩展字段,必须为字符型,可以为空。
138
+
139
+
140
+ ### Rails 应用使用方法
141
+
142
+ #### 第一步:
143
+
144
+ 在 Rails 应用目录 config/initializers/ 下创建脚本文件 aliyun-sms.rb,在文件中加入以下内容:
145
+
146
+ config/initializers/aliyun-sms.rb
147
+
148
+ ```ruby
149
+ Aliyun::Sms.configure do |config|
150
+ config.access_key_secret = ACCESS_KEY_SECRET # 阿里云接入密钥,在阿里云控制台申请
151
+ config.access_key_id = ACCESS_KEY_ID # 阿里云接入 ID, 在阿里云控制台申请
152
+ config.action = 'SendSms' # 默认设置
153
+ config.format = 'XML' # 短信推送返回信息格式,可以填写 'JSON'或者'XML'
154
+ config.region_id = 'cn-hangzhou' # 默认设置,如果没有特殊需要,可以不改
155
+ config.sign_name = SIGN_NAME # 短信签名,在阿里云申请开通短信服务时申请获取
156
+ config.signature_method = 'HMAC-SHA1' # 加密算法,默认设置
157
+ config.signature_version = '1.0' # 签名版本,默认设置,不用修改
158
+ config.version = '2017-05-25' # 服务版本,默认设置,不用修改
159
+ end
160
+ ```
161
+ 之后,重新启动 Rails,加载配置。
162
+
163
+ #### 第二步:
164
+
165
+ 在 Rails 应用中调用短信发送代码:
166
+
167
+ ```ruby
168
+ Aliyun::Sms.send(phone_numbers, template_code, template_param, out_id)
169
+ ```
170
+
171
+ 参数说明:
172
+
173
+ 1. phone_numbers: 接收短信的手机号,必须为字符型,例如 '1234567890',如果有多个号码,中间用","隔开,例如:'1234567890,12388888888';
174
+ 2. template\_code: 短信模版代码,必须为字符型,申请开通短信服务后,由阿里云提供,例如 'SMS_12345678';
175
+ 3. template_param: 请求字符串,向短信模版提供参数,必须是转为字符串的json格式对象,例如 '{"code":"666666", "product":"测试网站" }';
176
+ 4. out_id:外部流水扩展字段,必须为字符型,可以为空。
177
+
178
+ 特别说明:
179
+
180
+ 在程序中可以先用 HASH 组织 template\_param 内容,再使用 to_json 方法转换为 json 格式字符串,例如:
181
+
182
+ ```ruby
183
+ ...
184
+ phone_number = '1234567890'
185
+ template_code = 'SMS_12345678'
186
+ template_param = {"code"=>"666666", "product"=>"测试网站" }.to_json
187
+ Aliyun::Sms.send(phone_numbers, template_code, template_param)
188
+ ...
189
+ ```
190
+
191
+ ## Development 开发
192
+
193
+ 按照阿里云[官方接口文档](https://help.aliyun.com/document_detail/56189.html?spm=a2c4g.11186623.6.580.o8Fm0S)提供的 SMS 样例做了 spect 测试,可以 clone 项目后,在根目录下用命令行运行以下命令测试:
194
+
195
+ $ bundle exec rspec spec
196
+
197
+
198
+ ## License 许可
199
+
200
+ MIT 协议下的开源项目。 [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aliyun/sms/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aliyun-sms"
8
+ spec.version = Aliyun::Sms::VERSION
9
+ spec.authors = ["VICTOR LUO"]
10
+ spec.email = ["victor-luo@outlook.com"]
11
+
12
+ spec.summary = %q{A Ruby Gem for using aliyun sms service.}
13
+ spec.description = %q{A Ruby Gem for using aliyun sms service.}
14
+ spec.homepage = "https://github.com/VICTOR-LUO-F/aliyun-sms"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_dependency "typhoeus"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aliyun/sms"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/aliyun/sms.rb ADDED
@@ -0,0 +1,124 @@
1
+ require "aliyun/sms/version"
2
+ require "openssl"
3
+ require "base64"
4
+ require 'typhoeus'
5
+ require "erb"
6
+ include ERB::Util
7
+
8
+ module Aliyun
9
+ module Sms
10
+ class Configuration
11
+ attr_accessor :access_key_secret, :access_key_id, :action, :format, :region_id,
12
+ :sign_name, :signature_method, :signature_version, :version
13
+
14
+ def initialize
15
+ @access_key_secret = ''
16
+ @access_key_id = ''
17
+ @action = ''
18
+ @format = ''
19
+ @region_id = ''
20
+ @sign_name = ''
21
+ @signature_method = ''
22
+ @signature_version = ''
23
+ @version = ''
24
+ end
25
+ end
26
+
27
+ class << self
28
+ attr_writer :configuration
29
+
30
+ def configuration
31
+ @configuration ||= Configuration.new
32
+ end
33
+
34
+ def configure
35
+ yield(configuration)
36
+ end
37
+
38
+ def send(phone_numbers, template_code, template_param, out_id = '')
39
+ Typhoeus.get(get_url({
40
+ 'PhoneNumbers' => phone_numbers,
41
+ 'TemplateCode' => template_code,
42
+ 'TemplateParam' => template_param,
43
+ 'OutId' => out_id,
44
+ 'SignatureNonce' => seed_signature_nonce,
45
+ 'Timestamp' => seed_timestamp
46
+ }))
47
+ end
48
+
49
+ def get_url(user_params)
50
+ params = get_params(user_params)
51
+ coded_params = canonicalized_query_string(params)
52
+ key_secret = configuration.access_key_secret
53
+ url = 'http://dysmsapi.aliyuncs.com/?' + 'Signature=' + sign(key_secret, coded_params) + '&' + coded_params
54
+ end
55
+
56
+ def get_params(user_params)
57
+ params = config_params.merge(user_params)
58
+ end
59
+
60
+ def config_params()
61
+ params ={
62
+ 'AccessKeyId' => configuration.access_key_id,
63
+ 'Action' => configuration.action,
64
+ 'Format' => configuration.format,
65
+ 'RegionId' => configuration.region_id,
66
+ 'SignName' => configuration.sign_name,
67
+ 'SignatureMethod' => configuration.signature_method,
68
+ 'SignatureVersion' => configuration.signature_version,
69
+ 'Version' => configuration.version
70
+ }
71
+ end
72
+
73
+ def canonicalized_query_string(params)
74
+ cqstring = ''
75
+ params.sort_by{|key, val| key}.each do |key, value|
76
+ if cqstring.empty?
77
+ cqstring += "#{encode(key)}=#{encode(value)}"
78
+ else
79
+ cqstring += "&#{encode(key)}=#{encode(value)}"
80
+ end
81
+ end
82
+ cqstring
83
+ end
84
+
85
+ # 生成数字签名
86
+ def sign(key_secret, coded_params)
87
+ key = key_secret + '&'
88
+ signature = 'GET' + '&' + encode('/') + '&' + encode(coded_params)
89
+ sign = Base64.encode64("#{OpenSSL::HMAC.digest('sha1',key, signature)}")
90
+ encode(sign.chomp) # 通过chomp去掉最后的换行符 LF
91
+ end
92
+
93
+ # 对字符串进行 PERCENT 编码
94
+ def encode(input)
95
+ output = url_encode(input)
96
+ end
97
+
98
+ # 生成短信时间戳
99
+ def seed_timestamp
100
+ Time.now.utc.strftime("%FT%TZ")
101
+ end
102
+
103
+ # 生成短信唯一标识码,采用到微秒的时间戳
104
+ def seed_signature_nonce
105
+ Time.now.utc.strftime("%Y%m%d%H%M%S%L")
106
+ end
107
+
108
+ # 测试参数未编码时生成的字符串是否正确(多一道保险)
109
+ def test_query_string(params)
110
+ qstring = ''
111
+ params.sort_by{|key, val| key}.each do |key, value|
112
+ if qstring.empty?
113
+ qstring += "#{key}=#{value}"
114
+ else
115
+ qstring += "&#{key}=#{value}"
116
+ end
117
+ end
118
+ qstring
119
+ end
120
+
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,5 @@
1
+ module Aliyun
2
+ module Sms
3
+ VERSION = "1.1.0"
4
+ end
5
+ end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-sms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - VICTOR LUO
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-01 00:00:00.000000000 Z
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.14'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.14'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.2'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.2'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: typhoeus
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,7 +72,19 @@ email:
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
- files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - README.zh-CN.md
82
+ - Rakefile
83
+ - aliyun-sms.gemspec
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/aliyun/sms.rb
87
+ - lib/aliyun/sms/version.rb
76
88
  homepage: https://github.com/VICTOR-LUO-F/aliyun-sms
77
89
  licenses:
78
90
  - MIT
@@ -93,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
105
  version: '0'
94
106
  requirements: []
95
107
  rubyforge_project:
96
- rubygems_version: 2.6.10
108
+ rubygems_version: 2.6.13
97
109
  signing_key:
98
110
  specification_version: 4
99
111
  summary: A Ruby Gem for using aliyun sms service.