yx-smart_sms 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.rdoc_options +21 -0
  4. data/.travis.yml +9 -0
  5. data/CONTRIBUTING.md +32 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +145 -0
  8. data/LICENSE +21 -0
  9. data/README.md +283 -0
  10. data/Rakefile +11 -0
  11. data/lib/generators/smart_sms/config_generator.rb +16 -0
  12. data/lib/generators/smart_sms/install_generator.rb +36 -0
  13. data/lib/generators/smart_sms/templates/add_uid_to_smart_sms_messages.rb +6 -0
  14. data/lib/generators/smart_sms/templates/create_smart_sms_messages.rb +19 -0
  15. data/lib/generators/smart_sms/templates/smart_sms_config.rb +15 -0
  16. data/lib/smart_sms.rb +35 -0
  17. data/lib/smart_sms/account.rb +25 -0
  18. data/lib/smart_sms/config.rb +49 -0
  19. data/lib/smart_sms/has_sms_verification.rb +149 -0
  20. data/lib/smart_sms/helpers/fake_sms.rb +31 -0
  21. data/lib/smart_sms/helpers/verification_code.rb +42 -0
  22. data/lib/smart_sms/message_service.rb +98 -0
  23. data/lib/smart_sms/model/message.rb +9 -0
  24. data/lib/smart_sms/request.rb +48 -0
  25. data/lib/smart_sms/template.rb +44 -0
  26. data/lib/smart_sms/version.rb +3 -0
  27. data/smart_sms.gemspec +35 -0
  28. data/spec/account_spec.rb +80 -0
  29. data/spec/config/config_spec.rb +172 -0
  30. data/spec/fake_app/active_record/config.rb +9 -0
  31. data/spec/fake_app/active_record/models.rb +44 -0
  32. data/spec/fake_app/initializers/smart_sms.rb +15 -0
  33. data/spec/fake_app/rails_app.rb +23 -0
  34. data/spec/has_sms_verificaton_spec.rb +275 -0
  35. data/spec/helpers/fake_sms_spec.rb +15 -0
  36. data/spec/helpers/verification_code_spec.rb +62 -0
  37. data/spec/smart_sms_spec.rb +261 -0
  38. data/spec/spec_helper.rb +26 -0
  39. data/spec/support/database_cleaner.rb +13 -0
  40. data/spec/template_spec.rb +168 -0
  41. metadata +264 -0
@@ -0,0 +1,261 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS do
5
+ let(:phone) { '13394738283' }
6
+ let(:content) { '您的验证码是: 432432, 如果此操作与您无关, 请忽略这条短信' }
7
+
8
+ describe '#deliver' do
9
+ context 'template send api' do
10
+ let(:url) { 'http://yunpian.com/v1/sms/tpl_send.json' }
11
+ subject { SmartSMS.deliver phone, content }
12
+
13
+ before do
14
+ stub_request(:post, url).with(
15
+ body: {
16
+ 'apikey' => SmartSMS.config.api_key,
17
+ 'mobile' => phone,
18
+ 'tpl_id' => '2',
19
+ 'tpl_value' => "#code#=#{content}&#company#=Smart SMS"
20
+ },
21
+ headers: {
22
+ 'Content-Type' => 'application/x-www-form-urlencoded'
23
+ }
24
+ ).to_return(
25
+ body: {
26
+ 'code' => 0,
27
+ 'msg' => 'OK',
28
+ 'result' => {
29
+ 'count' => '1',
30
+ 'fee' => '1',
31
+ 'sid' => '592762800'
32
+ }
33
+ }.to_json
34
+ )
35
+ end
36
+
37
+ its(['code']) { should eq 0 }
38
+ its(['msg']) { should eq 'OK' }
39
+ end
40
+
41
+ context 'general send api' do
42
+ let(:url) { 'http://yunpian.com/v1/sms/send.json' }
43
+ subject { SmartSMS.deliver phone, content, method: :general, extend: true }
44
+
45
+ before do
46
+ stub_request(:post, url).with(
47
+ body: {
48
+ 'apikey' => SmartSMS.config.api_key,
49
+ 'mobile' => phone,
50
+ 'extend' => 'true',
51
+ 'text' => content
52
+ },
53
+ headers: {
54
+ 'Content-Type' => 'application/x-www-form-urlencoded'
55
+ }
56
+ ).to_return(
57
+ body: {
58
+ 'code' => 0,
59
+ 'msg' => 'OK',
60
+ 'result' => {
61
+ 'count' => '1',
62
+ 'fee' => '1',
63
+ 'sid' => '592762800'
64
+ }
65
+ }.to_json
66
+ )
67
+ end
68
+
69
+ its(['code']) { should eq 0 }
70
+ its(['msg']) { should eq 'OK' }
71
+ end
72
+ end
73
+
74
+ describe '#find_by_sid' do
75
+ context 'with a valid sid' do
76
+ let(:valid_sid) { '1234' }
77
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
78
+ subject { SmartSMS.find_by_sid valid_sid }
79
+
80
+ before do
81
+ stub_request(:post, url).with(
82
+ body: {
83
+ 'apikey' => SmartSMS.config.api_key,
84
+ 'sid' => valid_sid
85
+ },
86
+ headers: {
87
+ 'Content-Type' => 'application/x-www-form-urlencoded'
88
+ }
89
+ ).to_return(
90
+ body: {
91
+ 'code' => 0,
92
+ 'msg' => 'OK',
93
+ 'sms' => {
94
+ 'sid' => valid_sid,
95
+ 'mobile' => phone,
96
+ 'send_time' => '2014-05-08 09:24:08',
97
+ 'text' => content,
98
+ 'send_status' => 'SUCCESS',
99
+ 'report_status' => 'SUCCESS',
100
+ 'fee' => 1,
101
+ 'user_receive_time' => '2014-05-08 09:26:23',
102
+ 'error_msg' => nil
103
+ }
104
+ }.to_json
105
+ )
106
+ end
107
+
108
+ its(['code']) { should eq 0 }
109
+ its(['msg']) { should eq 'OK' }
110
+ its(:keys) { should include 'sms' }
111
+ end
112
+
113
+ context 'with a invalid sid' do
114
+ let(:invalid_sid) { '4362847632874' }
115
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
116
+ subject { SmartSMS.find_by_sid invalid_sid }
117
+
118
+ before do
119
+ stub_request(:post, url).with(
120
+ body: {
121
+ 'apikey' => SmartSMS.config.api_key,
122
+ 'sid' => invalid_sid
123
+ },
124
+ headers: {
125
+ 'Content-Type' => 'application/x-www-form-urlencoded'
126
+ }
127
+ ).to_return(
128
+ body: {
129
+ 'code' => 0,
130
+ 'msg' => 'OK',
131
+ 'sms' => nil
132
+ }.to_json
133
+ )
134
+ end
135
+
136
+ its(['code']) { should eq 0 }
137
+ its(['msg']) { should eq 'OK' }
138
+ its(['sms']) { should eq nil }
139
+ end
140
+ end
141
+
142
+ describe '#find' do
143
+ let(:url) { 'http://yunpian.com/v1/sms/get.json' }
144
+ subject { SmartSMS.find start_time: '2014-05-07 11:33:36', end_time: '2014-05-08 11:33:36' }
145
+
146
+ before do
147
+ stub_request(:post, url).with(
148
+ body: {
149
+ 'apikey' => SmartSMS.config.api_key,
150
+ 'start_time' => '2014-05-07 11:33:36',
151
+ 'end_time' => '2014-05-08 11:33:36',
152
+ 'page_num' => '1',
153
+ 'page_size' => '20'
154
+ },
155
+ headers: {
156
+ 'Content-Type' => 'application/x-www-form-urlencoded'
157
+ }
158
+ ).to_return(
159
+ body: {
160
+ 'code' => 0,
161
+ 'msg' => 'OK',
162
+ 'sms' => [
163
+ {
164
+ 'sid' => 654805009,
165
+ 'mobile' => phone,
166
+ 'send_time' => '2014-05-08 09:25:58',
167
+ 'text' => content,
168
+ 'send_status' => 'SUCCESS',
169
+ 'report_status' => 'SUCCESS',
170
+ 'fee' => 1,
171
+ 'user_receive_time' => '2014-05-08 09:25:58',
172
+ 'error_msg' => nil
173
+ },
174
+ {
175
+ 'sid' => 654804990,
176
+ 'mobile' => phone,
177
+ 'send_time' => '2014-05-08 09:25:56',
178
+ 'text' => content,
179
+ 'send_status' => 'SUCCESS',
180
+ 'report_status' => 'SUCCESS',
181
+ 'fee' => 1,
182
+ 'user_receive_time' => '2014-05-08 09:25:57',
183
+ 'error_msg' => nil
184
+ }
185
+ ]
186
+ }.to_json
187
+ )
188
+ end
189
+
190
+ its(['code']) { should eq 0 }
191
+ its(['msg']) { should eq 'OK' }
192
+ its(:keys) { should include 'sms' }
193
+ its(['sms']) { should_not be_empty }
194
+ end
195
+
196
+ describe '#get_black_word' do
197
+ let(:url) { 'http://yunpian.com/v1/sms/get_black_word.json' }
198
+ let(:text) { '这是一条测试短信' }
199
+ subject { SmartSMS.get_black_word text }
200
+
201
+ before do
202
+ stub_request(:post, url).with(
203
+ body: {
204
+ 'apikey' => SmartSMS.config.api_key,
205
+ 'text' => text
206
+ },
207
+ headers: {
208
+ 'Content-Type' => 'application/x-www-form-urlencoded'
209
+ }
210
+ ).to_return(
211
+ body: {
212
+ 'code' => 0, 'msg' => 'OK', 'result' => { 'black_word' => '测试' }
213
+ }.to_json
214
+ )
215
+ end
216
+
217
+ its(['code']) { should eq 0 }
218
+ its(['msg']) { should eq 'OK' }
219
+ its(:keys) { should include 'result' }
220
+ end
221
+
222
+ describe '#get_reply' do
223
+ let(:url) { 'http://yunpian.com/v1/sms/get_reply.json' }
224
+ subject { SmartSMS.get_reply start_time: '2014-05-07 11:33:36', end_time: '2014-05-08 11:33:36' }
225
+
226
+ before do
227
+ stub_request(:post, url).with(
228
+ body: {
229
+ 'apikey' => SmartSMS.config.api_key,
230
+ 'start_time' => '2014-05-07 11:33:36',
231
+ 'end_time' => '2014-05-08 11:33:36',
232
+ 'page_num' => '1',
233
+ 'page_size' => '20'
234
+ },
235
+ headers: {
236
+ 'Content-Type' => 'application/x-www-form-urlencoded'
237
+ }
238
+ ).to_return(
239
+ body: {
240
+ 'code' => 0,
241
+ 'msg' => 'OK',
242
+ 'sms_reply' => [
243
+ {
244
+ 'id' => nil,
245
+ 'mobile' => phone,
246
+ 'text' => '哈哈',
247
+ 'reply_time' => '2014-05-08 12:03:16',
248
+ 'extend' => '11642'
249
+ }
250
+ ]
251
+ }.to_json
252
+ )
253
+ end
254
+
255
+ its(['code']) { should eq 0 }
256
+ its(['msg']) { should eq 'OK' }
257
+ its(:keys) { should include 'sms_reply' }
258
+ its(['sms_reply']) { should_not be_empty }
259
+ its(['sms_reply', 0, 'mobile']) { should eq phone }
260
+ end
261
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'rails'
4
+ rescue LoadError
5
+ end
6
+
7
+ require 'rubygems'
8
+ require 'bundler/setup'
9
+ # Bundler.require
10
+
11
+ require 'rspec/its'
12
+ require 'webmock/rspec'
13
+
14
+ require 'smart_sms'
15
+
16
+ require 'database_cleaner'
17
+
18
+ # WebMock.allow_net_connect!
19
+
20
+ if defined? Rails
21
+ require 'fake_app/rails_app'
22
+
23
+ require 'rspec/rails'
24
+ end
25
+
26
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,13 @@
1
+ DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
2
+
3
+ RSpec.configure do |config|
4
+ config.before :suite do
5
+ DatabaseCleaner.clean_with :truncation if defined? ActiveRecord
6
+ end
7
+ config.before :each do
8
+ DatabaseCleaner.start
9
+ end
10
+ config.after :each do
11
+ DatabaseCleaner.clean
12
+ end
13
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS::Template do
5
+
6
+ describe '#find_default' do
7
+ let(:url) { 'http://yunpian.com/v1/tpl/get_default.json' }
8
+ let(:tpl_id) { '1' }
9
+ subject { SmartSMS::Template.find_default tpl_id }
10
+
11
+ before do
12
+ stub_request(:post, url).with(
13
+ body: {
14
+ 'apikey' => SmartSMS.config.api_key,
15
+ 'tpl_id' => tpl_id
16
+ },
17
+ headers: {
18
+ 'Content-Type' => 'application/x-www-form-urlencoded'
19
+ }
20
+ ).to_return(
21
+ body: {
22
+ 'code' => 0,
23
+ 'msg' => 'OK',
24
+ 'template' => {
25
+ 'tpl_id' => tpl_id,
26
+ 'tpl_content' => '您的验证码是#code#【#company#】',
27
+ 'check_status' => 'SUCCESS',
28
+ 'reason' => nil
29
+ }
30
+ }.to_json
31
+ )
32
+ end
33
+
34
+ its(['code']) { should eq 0 }
35
+ its(['msg']) { should eq 'OK' }
36
+ its(:keys) { should include 'template' }
37
+ end
38
+
39
+ describe '#find' do
40
+ let(:url) { 'http://yunpian.com/v1/tpl/get.json' }
41
+ let(:tpl_id) { '325207' }
42
+ subject { SmartSMS::Template.find tpl_id }
43
+
44
+ before do
45
+ stub_request(:post, url).with(
46
+ body: {
47
+ 'apikey' => SmartSMS.config.api_key,
48
+ 'tpl_id' => tpl_id
49
+ },
50
+ headers: {
51
+ 'Content-Type' => 'application/x-www-form-urlencoded'
52
+ }
53
+ ).to_return(
54
+ body: {
55
+ 'code' => 0,
56
+ 'msg' => 'OK',
57
+ 'template' => {
58
+ 'tpl_id' => tpl_id,
59
+ 'tpl_content' => '您的验证码是#code#【#company#】',
60
+ 'check_status' => 'SUCCESS',
61
+ 'reason' => nil
62
+ }
63
+ }.to_json
64
+ )
65
+ end
66
+
67
+ its(['code']) { should eq 0 }
68
+ its(['msg']) { should eq 'OK' }
69
+ its(:keys) { should include 'template' }
70
+ end
71
+
72
+ describe '#create' do
73
+ let(:url) { 'http://yunpian.com/v1/tpl/add.json' }
74
+ let(:tpl_content) { '您的验证码是: #code#' }
75
+ subject { SmartSMS::Template.create tpl_content }
76
+
77
+ before do
78
+ stub_request(:post, url).with(
79
+ body: {
80
+ 'apikey' => SmartSMS.config.api_key,
81
+ 'tpl_content' => tpl_content
82
+ },
83
+ headers: {
84
+ 'Content-Type' => 'application/x-www-form-urlencoded'
85
+ }
86
+ ).to_return(
87
+ body: {
88
+ 'code' => 0,
89
+ 'msg' => 'OK',
90
+ 'template' => {
91
+ 'tpl_id' => '43243242',
92
+ 'tpl_content' => tpl_content,
93
+ 'check_status' => 'SUCCESS',
94
+ 'reason' => nil
95
+ }
96
+ }.to_json
97
+ )
98
+ end
99
+
100
+ its(['code']) { should eq 0 }
101
+ its(['msg']) { should eq 'OK' }
102
+ its(:keys) { should include 'template' }
103
+ end
104
+
105
+ describe '#update' do
106
+ let(:url) { 'http://yunpian.com/v1/tpl/update.json' }
107
+ let(:tpl_id) { '43243242' }
108
+ let(:tpl_content) { '您的验证码是: #code#' }
109
+ subject { SmartSMS::Template.update tpl_id, tpl_content }
110
+
111
+ before do
112
+ stub_request(:post, url).with(
113
+ body: {
114
+ 'apikey' => SmartSMS.config.api_key,
115
+ 'tpl_id' => tpl_id,
116
+ 'tpl_content' => tpl_content
117
+ },
118
+ headers: {
119
+ 'Content-Type' => 'application/x-www-form-urlencoded'
120
+ }
121
+ ).to_return(
122
+ body: {
123
+ 'code' => 0,
124
+ 'msg' => 'OK',
125
+ 'template' => {
126
+ 'tpl_id' => tpl_id,
127
+ 'tpl_content' => tpl_content,
128
+ 'check_status' => 'SUCCESS',
129
+ 'reason' => nil
130
+ }
131
+ }.to_json
132
+ )
133
+ end
134
+
135
+ its(['code']) { should eq 0 }
136
+ its(['msg']) { should eq 'OK' }
137
+ its(:keys) { should include 'template' }
138
+ end
139
+
140
+ describe '#destroy' do
141
+ let(:url) { 'http://yunpian.com/v1/tpl/del.json' }
142
+ let(:tpl_id) { '43243242' }
143
+ subject { SmartSMS::Template.destroy tpl_id }
144
+
145
+ before do
146
+ stub_request(:post, url).with(
147
+ body: {
148
+ 'apikey' => SmartSMS.config.api_key,
149
+ 'tpl_id' => tpl_id
150
+ },
151
+ headers: {
152
+ 'Content-Type' => 'application/x-www-form-urlencoded'
153
+ }
154
+ ).to_return(
155
+ body: {
156
+ 'code' => 0,
157
+ 'msg' => 'OK',
158
+ 'detail' => nil
159
+ }.to_json
160
+ )
161
+ end
162
+
163
+ its(['code']) { should eq 0 }
164
+ its(['msg']) { should eq 'OK' }
165
+ its(:keys) { should include 'detail' }
166
+ its(['detail']) { should be_nil }
167
+ end
168
+ end