smart_sms 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +97 -24
- data/README.md +245 -3
- data/Rakefile +1 -1
- data/lib/generators/smart_sms/install_generator.rb +7 -2
- data/lib/generators/smart_sms/templates/smart_sms_config.rb +1 -1
- data/lib/smart_sms.rb +2 -2
- data/lib/smart_sms/account.rb +6 -4
- data/lib/smart_sms/config.rb +4 -3
- data/lib/smart_sms/has_sms_verification.rb +39 -34
- data/lib/smart_sms/helpers/fake_sms.rb +14 -13
- data/lib/smart_sms/helpers/verification_code.rb +15 -7
- data/lib/smart_sms/message_service.rb +15 -13
- data/lib/smart_sms/model/message.rb +2 -0
- data/lib/smart_sms/request.rb +9 -8
- data/lib/smart_sms/template.rb +8 -7
- data/lib/smart_sms/version.rb +2 -2
- data/smart_sms.gemspec +11 -8
- data/spec/account_spec.rb +80 -0
- data/spec/config/config_spec.rb +172 -0
- data/spec/fake_app/active_record/config.rb +9 -0
- data/spec/fake_app/active_record/models.rb +42 -0
- data/spec/fake_app/initializers/smart_sms.rb +15 -0
- data/spec/fake_app/rails_app.rb +23 -0
- data/spec/has_sms_verificaton_spec.rb +275 -0
- data/spec/helpers/fake_sms_spec.rb +15 -0
- data/spec/helpers/verification_code_spec.rb +62 -0
- data/spec/smart_sms_spec.rb +260 -0
- data/spec/spec_helper.rb +15 -2
- data/spec/support/database_cleaner.rb +13 -0
- data/spec/template_spec.rb +168 -0
- metadata +69 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae8bc61edbbcbc8ae6fdb4e185d1ec9b96d49dd7
|
4
|
+
data.tar.gz: 5cf87b214f8541f14a99ad9a6a6e7ad5249c7432
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 929a55ac72a38ff5cbfab959ca8c3fcbefa75ef6f7cd2094c0601810c3d909b57afb7eb6755644bc7d1b80f8e4b8d87cc29beaa6d7585281f1d7783fe6b67e8b
|
7
|
+
data.tar.gz: 56190a364c5fa08f8c938ae206a4d62e64243e827e608963ce0a07d8b6a7a52d2b14d1fdd4784e9519a4f9a7436dfa5db5a197a22d5aed72ca3b172d84781857
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,44 +1,114 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smart_sms (0.0
|
4
|
+
smart_sms (0.1.0)
|
5
5
|
activerecord (>= 3.0, < 5.0)
|
6
6
|
activesupport (>= 3.0, < 5.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
11
|
+
actionmailer (4.1.1)
|
12
|
+
actionpack (= 4.1.1)
|
13
|
+
actionview (= 4.1.1)
|
14
|
+
mail (~> 2.5.4)
|
15
|
+
actionpack (4.1.1)
|
16
|
+
actionview (= 4.1.1)
|
17
|
+
activesupport (= 4.1.1)
|
18
|
+
rack (~> 1.5.2)
|
19
|
+
rack-test (~> 0.6.2)
|
20
|
+
actionview (4.1.1)
|
21
|
+
activesupport (= 4.1.1)
|
22
|
+
builder (~> 3.1)
|
23
|
+
erubis (~> 2.7.0)
|
24
|
+
activemodel (4.1.1)
|
25
|
+
activesupport (= 4.1.1)
|
26
|
+
builder (~> 3.1)
|
27
|
+
activerecord (4.1.1)
|
28
|
+
activemodel (= 4.1.1)
|
29
|
+
activesupport (= 4.1.1)
|
30
|
+
arel (~> 5.0.0)
|
31
|
+
activesupport (4.1.1)
|
32
|
+
i18n (~> 0.6, >= 0.6.9)
|
33
|
+
json (~> 1.7, >= 1.7.7)
|
34
|
+
minitest (~> 5.1)
|
35
|
+
thread_safe (~> 0.1)
|
36
|
+
tzinfo (~> 1.1)
|
22
37
|
addressable (2.3.6)
|
23
|
-
arel (
|
24
|
-
builder (3.
|
38
|
+
arel (5.0.1.20140414130214)
|
39
|
+
builder (3.2.2)
|
40
|
+
coderay (1.1.0)
|
25
41
|
crack (0.4.2)
|
26
42
|
safe_yaml (~> 1.0.0)
|
27
43
|
database_cleaner (1.2.0)
|
28
44
|
diff-lcs (1.2.5)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
45
|
+
erubis (2.7.0)
|
46
|
+
hike (1.2.3)
|
47
|
+
i18n (0.6.9)
|
48
|
+
json (1.8.1)
|
49
|
+
mail (2.5.4)
|
50
|
+
mime-types (~> 1.16)
|
51
|
+
treetop (~> 1.4.8)
|
52
|
+
method_source (0.8.2)
|
53
|
+
mime-types (1.25.1)
|
54
|
+
minitest (5.3.3)
|
55
|
+
multi_json (1.10.0)
|
56
|
+
polyglot (0.3.4)
|
57
|
+
pry (0.9.12.6)
|
58
|
+
coderay (~> 1.0)
|
59
|
+
method_source (~> 0.8)
|
60
|
+
slop (~> 3.4)
|
61
|
+
rack (1.5.2)
|
62
|
+
rack-test (0.6.2)
|
63
|
+
rack (>= 1.0)
|
64
|
+
rails (4.1.1)
|
65
|
+
actionmailer (= 4.1.1)
|
66
|
+
actionpack (= 4.1.1)
|
67
|
+
actionview (= 4.1.1)
|
68
|
+
activemodel (= 4.1.1)
|
69
|
+
activerecord (= 4.1.1)
|
70
|
+
activesupport (= 4.1.1)
|
71
|
+
bundler (>= 1.3.0, < 2.0)
|
72
|
+
railties (= 4.1.1)
|
73
|
+
sprockets-rails (~> 2.0)
|
74
|
+
railties (4.1.1)
|
75
|
+
actionpack (= 4.1.1)
|
76
|
+
activesupport (= 4.1.1)
|
77
|
+
rake (>= 0.8.7)
|
78
|
+
thor (>= 0.18.1, < 2.0)
|
79
|
+
rake (10.3.1)
|
36
80
|
rspec-core (2.14.8)
|
37
81
|
rspec-expectations (2.14.5)
|
38
82
|
diff-lcs (>= 1.1.3, < 2.0)
|
39
83
|
rspec-mocks (2.14.6)
|
40
|
-
|
41
|
-
|
84
|
+
rspec-rails (2.14.2)
|
85
|
+
actionpack (>= 3.0)
|
86
|
+
activemodel (>= 3.0)
|
87
|
+
activesupport (>= 3.0)
|
88
|
+
railties (>= 3.0)
|
89
|
+
rspec-core (~> 2.14.0)
|
90
|
+
rspec-expectations (~> 2.14.0)
|
91
|
+
rspec-mocks (~> 2.14.0)
|
92
|
+
safe_yaml (1.0.3)
|
93
|
+
slop (3.5.0)
|
94
|
+
sprockets (2.12.1)
|
95
|
+
hike (~> 1.2)
|
96
|
+
multi_json (~> 1.0)
|
97
|
+
rack (~> 1.0)
|
98
|
+
tilt (~> 1.1, != 1.3.0)
|
99
|
+
sprockets-rails (2.1.3)
|
100
|
+
actionpack (>= 3.0)
|
101
|
+
activesupport (>= 3.0)
|
102
|
+
sprockets (~> 2.8)
|
103
|
+
sqlite3 (1.3.9)
|
104
|
+
thor (0.19.1)
|
105
|
+
thread_safe (0.3.3)
|
106
|
+
tilt (1.4.1)
|
107
|
+
treetop (1.4.15)
|
108
|
+
polyglot
|
109
|
+
polyglot (>= 0.3.1)
|
110
|
+
tzinfo (1.1.0)
|
111
|
+
thread_safe (~> 0.1)
|
42
112
|
webmock (1.17.4)
|
43
113
|
addressable (>= 2.2.7)
|
44
114
|
crack (>= 0.3.2)
|
@@ -49,7 +119,10 @@ PLATFORMS
|
|
49
119
|
DEPENDENCIES
|
50
120
|
bundler (>= 1.0.0)
|
51
121
|
database_cleaner (~> 1.2.0)
|
122
|
+
pry
|
123
|
+
rails (>= 3.1.0)
|
52
124
|
rake
|
53
|
-
rspec
|
125
|
+
rspec-rails
|
54
126
|
smart_sms!
|
127
|
+
sqlite3
|
55
128
|
webmock (~> 1.17.0)
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
SmartSMS [![Build Status](https://travis-ci.org/lyfeyaj/smart_sms.png?branch=master)](https://travis-ci.org/lyfeyaj/smart_sms) [![Code Climate](https://codeclimate.com/github/lyfeyaj/smart_sms.png)](https://codeclimate.com/github/lyfeyaj/smart_sms)
|
2
2
|
===================================
|
3
3
|
|
4
|
-
|
4
|
+
提供在中国境内发送短信([云片网络](http://www.yunpian.com)), 校验, 集成 ActiveRecord
|
5
5
|
|
6
6
|
功能特点
|
7
7
|
--------
|
@@ -30,9 +30,251 @@ SmartSMS [![Build Status](https://travis-ci.org/lyfeyaj/smart_sms.png?branch=mas
|
|
30
30
|
|
31
31
|
### 结合 Rails 使用
|
32
32
|
|
33
|
-
|
33
|
+
##### 安装
|
34
34
|
|
35
|
-
|
35
|
+
在您的 `Gemfile` 里面添加:
|
36
36
|
|
37
|
+
```
|
38
|
+
gem 'smart_sms'
|
39
|
+
```
|
37
40
|
|
41
|
+
然后, 在 console 中执行下面的代码:
|
38
42
|
|
43
|
+
``` bash
|
44
|
+
# 安装gem
|
45
|
+
bundle
|
46
|
+
|
47
|
+
rails g smart_sms:config # 将会拷贝配置文件至 `config/initializers/smart_sms_config.rb`
|
48
|
+
|
49
|
+
# 如果, 需要将所有短信都在本地存储, 则需要将配置文件中的 `store_sms_in_local` 设置为 true, 然后运行
|
50
|
+
rails g smart_sms:install # 程序将会拷贝相应的 migration 文件到 `db/migrate` 目录下
|
51
|
+
rake db:migrate
|
52
|
+
```
|
53
|
+
|
54
|
+
##### 配置
|
55
|
+
|
56
|
+
``` ruby
|
57
|
+
SmartSMS.configure do |config|
|
58
|
+
config.api_key = nil # 授权 API KEY
|
59
|
+
config.api_version = :v1 # API 的版本, 当前仅有v1
|
60
|
+
config.template_id = '2' # 指定发送信息时使用的模板
|
61
|
+
config.template_value = [:code, :company] # 用于指定信息文本中的可替换内容, 数组形势: [:code, :company]
|
62
|
+
config.page_num = 1 # 获取信息时, 指定默认的页数
|
63
|
+
config.page_size = 20 # 获取信息时, 一页包含信息数量
|
64
|
+
config.company = '云片网' # 默认公司名称
|
65
|
+
config.expires_in = 1.hour # 短信验证过期时间
|
66
|
+
config.default_interval = 1.day # 查询短信时的默认时间段: end_time - start_time
|
67
|
+
config.store_sms_in_local = false # 是否存储SMS信息在本地: true or false
|
68
|
+
config.verification_code_algorithm = :simple # 提供三种形式的验证码: `:simple, :middle, :complex`
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
##### API 汇总(ActiveRecord)
|
73
|
+
|
74
|
+
当在您现有的 model 中声明 `has_sms_verification` 的时候, 您将获得以下方法:
|
75
|
+
``` ruby
|
76
|
+
class User < ActiveRecord::Base
|
77
|
+
# 在您的Model里面声明这个方法, 以添加SMS短信验证功能
|
78
|
+
# moible_column: mobile 绑定的字段, 用于获取发送短信所需的手机号码, 默认是 :phone
|
79
|
+
# verification_column: 验证绑定的字段, 用于判断是否已验证,
|
80
|
+
#
|
81
|
+
# Options:
|
82
|
+
# :class_name 自定义的Message类名称. 默认是 `SmartSMS::Message`
|
83
|
+
# :messages 自定义的Message关联名称. 默认是 `:messages`.
|
84
|
+
#
|
85
|
+
has_sms_verification
|
86
|
+
end
|
87
|
+
|
88
|
+
# 发送短信验证码
|
89
|
+
user.deliver # 将会生成一个随机的验证码发送至手机, 并保存在messages表中
|
90
|
+
user.deliver '内容' # 可以发送指定内容至手机
|
91
|
+
|
92
|
+
# 查询历史短信记录
|
93
|
+
user.messages
|
94
|
+
|
95
|
+
# 查询是否已经验证
|
96
|
+
user.verified? # ture : false
|
97
|
+
|
98
|
+
# 校验验证码
|
99
|
+
user.verify '123456' # 返回 true 或者 false, 不修改数据库的 verified_at 关联字段
|
100
|
+
user.verify! '123456' # 返回 true 或者 false, 同时修改数据库的 verified_at 关联字段为当前时间
|
101
|
+
|
102
|
+
# 查询验证日期
|
103
|
+
user.verified_at
|
104
|
+
|
105
|
+
# 查询最新的一条有效短信记录
|
106
|
+
user.latest_message # 返回有效期内的最近一条短信, 若无则返回nil
|
107
|
+
|
108
|
+
# 发送假信息, 方便非国内用户测试用
|
109
|
+
user.deliver_fake_sms # messages中会保存一条新的短信记录, 但是不会发送短信到手机
|
110
|
+
```
|
111
|
+
|
112
|
+
### 基本用法(不依赖Rails)
|
113
|
+
|
114
|
+
##### 设置api_key
|
115
|
+
``` ruby
|
116
|
+
SmartSMS.configure { |c| c.api_key = 'fure8423n4324uoj432n4324' }
|
117
|
+
```
|
118
|
+
|
119
|
+
##### 短信
|
120
|
+
|
121
|
+
``` ruby
|
122
|
+
|
123
|
+
# 发送短信到手机, 默认使用模板发送, 提供通用接口支持
|
124
|
+
# phone: 需要接受短信的手机号码
|
125
|
+
# content: 短信验证内容
|
126
|
+
#
|
127
|
+
# Options:
|
128
|
+
# :method 如若要使用通用短信接口, 需要 method: :general
|
129
|
+
# :tpl_id 选择发送短信的模板, 默认是2
|
130
|
+
SmartSMS.deliver 13522948742, 'SmartSMS WOW!'
|
131
|
+
SmartSMS.deliver 13522948742, 'SmartSMS WOW!', tpl_id: 1
|
132
|
+
SmartSMS.deliver 13522948742, 'SmartSMS WOW!', method: :general # 使用通用短信发送方式, 需申请
|
133
|
+
|
134
|
+
# 根据sid来查询短信记录
|
135
|
+
SmartSMS.find_by_sid 13232
|
136
|
+
# => {"code"=>0,
|
137
|
+
# "msg"=>"OK",
|
138
|
+
# "sms"=>
|
139
|
+
# {"sid"=>13232,
|
140
|
+
# "mobile"=>"13522948742",
|
141
|
+
# "send_time"=>"2014-04-06 13:29:33",
|
142
|
+
# "text"=>"您的验证码是668965。如非本人操作,请忽略本短信【SmartSMS】",
|
143
|
+
# "send_status"=>"SUCCESS",
|
144
|
+
# "report_status"=>"SUCCESS",
|
145
|
+
# "fee"=>1,
|
146
|
+
# "user_receive_time"=>"2014-04-06 13:29:49",
|
147
|
+
# "error_msg"=>nil}}
|
148
|
+
|
149
|
+
# 批量查短信, 参数:
|
150
|
+
# start_time: 短信提交开始时间
|
151
|
+
# end_time: 短信提交结束时间
|
152
|
+
# page_num: 页码,从1开始
|
153
|
+
# page_size: 每页个数,最大100个
|
154
|
+
# mobile: 接收短信的手机号
|
155
|
+
SmartSMS.find
|
156
|
+
SmartSMS.find start_time: Time.now.yesterday, end_time: Time.now
|
157
|
+
SmartSMS.find start_time: Time.now.yesterday, end_time: Time.now, mobile: 13522948742
|
158
|
+
|
159
|
+
# 查询屏蔽词
|
160
|
+
SmartSMS.get_black_word '这是一条测试短信'
|
161
|
+
# => {"code"=>0, "msg"=>"OK", "result"=>{"black_word"=>"测试"}}
|
162
|
+
|
163
|
+
# 查回复的短信, 参数与批量查短信一致, 可以查询用户回复的短信
|
164
|
+
SmartSMS.get_reply
|
165
|
+
SmartSMS.get_reply start_time: Time.now.yesterday, end_time: Time.now
|
166
|
+
SmartSMS.get_reply start_time: Time.now.yesterday, end_time: Time.now, mobile: 13522948742
|
167
|
+
|
168
|
+
```
|
169
|
+
|
170
|
+
##### 账户
|
171
|
+
|
172
|
+
``` ruby
|
173
|
+
|
174
|
+
# 获取用户信息
|
175
|
+
SmartSMS::Account.info
|
176
|
+
# =>
|
177
|
+
#{
|
178
|
+
# "code" => 0,
|
179
|
+
# "msg" => "OK",
|
180
|
+
# "user" => {
|
181
|
+
# "nick" => "Jacky",
|
182
|
+
# "gmt_created" => "2012-09-11 15:14:00",
|
183
|
+
# "mobile" => "13764071479",
|
184
|
+
# "email" => "jacky@taovip.com",
|
185
|
+
# "ip_whitelist" => null, //IP白名单,推荐使用
|
186
|
+
# "api_version" => "v1", //api版本号
|
187
|
+
# "send_count" => 0, //当天已发送的短信数
|
188
|
+
# "balance" => 0, //短信剩余条数
|
189
|
+
# "alarm_balance" => 0, //剩余条数低于该值时提醒
|
190
|
+
# "emergency_contact" => "张三", //紧急联系人
|
191
|
+
# "emergency_mobile" => "13812341234" //紧急联系人电话
|
192
|
+
# }
|
193
|
+
#}
|
194
|
+
|
195
|
+
# 设置用户信息
|
196
|
+
# emergency_contact: 紧急联系人
|
197
|
+
# emergency_mobile: 紧急联系人手机号
|
198
|
+
# alarm_balance: 短信余额提醒阈值。一天只提示一次
|
199
|
+
SmartSMS::Account.set emergency_contact: 13764071479
|
200
|
+
# =>
|
201
|
+
#{
|
202
|
+
# "code" => 0,
|
203
|
+
# "msg" => "OK",
|
204
|
+
# "detail" => null
|
205
|
+
#}
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
##### 模板
|
210
|
+
|
211
|
+
``` ruby
|
212
|
+
|
213
|
+
# 获取系统默认模板
|
214
|
+
# Options:
|
215
|
+
# tpl_id: 指定tpl_id时返回tpl_id对应的默认模板. 未指定时返回所有默认模板
|
216
|
+
#
|
217
|
+
SmartSMS::Template.find_default
|
218
|
+
SmartSMS::Template.find_default 2
|
219
|
+
|
220
|
+
# 获取自定义模板
|
221
|
+
# Options:
|
222
|
+
# tpl_id: 指定tpl_id时返回tpl_id对应的自定义模板. 未指定时返回所有自定义模板
|
223
|
+
#
|
224
|
+
SmartSMS::Template.find
|
225
|
+
SmartSMS::Template.find 3252
|
226
|
+
|
227
|
+
# 创建新模板
|
228
|
+
# 规则请参见: <http://www.yunpian.com/api/tpl.html>
|
229
|
+
#
|
230
|
+
SmartSMS::Template.create '您的验证码是: #code#'
|
231
|
+
|
232
|
+
# 更新模板, 需指定id和content
|
233
|
+
#
|
234
|
+
SmartSMS::Template.update 3252, '您的验证码是: #code#, 【SmartSMS】'
|
235
|
+
|
236
|
+
# 删除模板, 需指定id
|
237
|
+
#
|
238
|
+
SmartSMS::Template.destroy 3252
|
239
|
+
|
240
|
+
```
|
241
|
+
|
242
|
+
##### 校验码
|
243
|
+
|
244
|
+
``` ruby
|
245
|
+
|
246
|
+
# 生成随机校验码
|
247
|
+
# 三个选项:
|
248
|
+
# simple: 6位随机数字, 默认
|
249
|
+
# middle: 6位随机字母, 数字组合
|
250
|
+
# complex: 8位随机字母, 数字, 特殊字符组合
|
251
|
+
|
252
|
+
SmartSMS::VerificationCode.random # => "141068"
|
253
|
+
SmartSMS::VerificationCode.random :middle # => "xey7id"
|
254
|
+
SmartSMS::VerificationCode.random :complex # => "x+rkag6a"
|
255
|
+
|
256
|
+
SmartSMS::VerificationCode.simple # => "141068"
|
257
|
+
SmartSMS::VerificationCode.middle # => "xey7id"
|
258
|
+
SmartSMS::VerificationCode.complex # => "x+rkag6a"
|
259
|
+
|
260
|
+
```
|
261
|
+
|
262
|
+
## 贡献
|
263
|
+
|
264
|
+
+ Fork
|
265
|
+
+ 创建分支 (git checkout -b my-new-feature)
|
266
|
+
+ 保存代码 (git commit -am 'Added some feature')
|
267
|
+
+ 上传到分支 (git push origin my-new-feature)
|
268
|
+
+ 创建一个新的合并请求
|
269
|
+
|
270
|
+
## LICENCE
|
271
|
+
|
272
|
+
MIT
|
273
|
+
|
274
|
+
## 作者
|
275
|
+
|
276
|
+
[Felix Liu](https://github.com/lyfeyaj)
|
277
|
+
|
278
|
+
## TODO
|
279
|
+
|
280
|
+
+ 增加测试用例覆盖率
|
data/Rakefile
CHANGED