smart_sms 0.0.3 → 0.1.0

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.
@@ -1,14 +1,15 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module SmartSMS
4
+ # module that handle `Template`
4
5
  module Template
5
- extend self
6
+ module_function
6
7
 
7
8
  # 取默认模板
8
9
  # Options:
9
10
  # tpl_id: 指定tpl_id时返回tpl_id对应的默认模板. 未指定时返回所有默认模板
10
11
  #
11
- def find_default tpl_id = ''
12
+ def find_default(tpl_id = '')
12
13
  Request.post 'tpl/get_default.json', tpl_id: tpl_id
13
14
  end
14
15
 
@@ -16,26 +17,26 @@ module SmartSMS
16
17
  # Options:
17
18
  # tpl_id: 指定tpl_id时返回tpl_id对应的自定义模板. 未指定时返回所有自定义模板
18
19
  #
19
- def find tpl_id = ''
20
+ def find(tpl_id = '')
20
21
  Request.post 'tpl/get.json', tpl_id: tpl_id
21
22
  end
22
23
 
23
24
  # 创建新模板
24
25
  # 规则请参见: <http://www.yunpian.com/api/tpl.html>
25
26
  #
26
- def create tpl_content = ''
27
+ def create(tpl_content = '')
27
28
  Request.post 'tpl/add.json', tpl_content: tpl_content
28
29
  end
29
30
 
30
31
  # 更新模板, 需指定id和content
31
32
  #
32
- def update tpl_id = '', tpl_content = ''
33
+ def update(tpl_id = '', tpl_content = '')
33
34
  Request.post 'tpl/update.json', tpl_id: tpl_id, tpl_content: tpl_content
34
35
  end
35
36
 
36
37
  # 删除模板, 需指定id
37
- def destroy tpl_id = ''
38
+ def destroy(tpl_id = '')
38
39
  Request.post 'tpl/del.json', tpl_id: tpl_id
39
40
  end
40
41
  end
41
- end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module SmartSMS
2
- VERSION = '0.0.3'
3
- end
2
+ VERSION = '0.1.0'
3
+ end
data/smart_sms.gemspec CHANGED
@@ -5,26 +5,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'smart_sms/version'
6
6
 
7
7
  Gem::Specification.new do |s|
8
- s.name = "smart_sms"
8
+ s.name = 'smart_sms'
9
9
  s.version = SmartSMS::VERSION
10
- s.authors = ["lyfeyaj"]
11
- s.email = ["lyfeyaj@gmail.com"]
10
+ s.authors = ['lyfeyaj']
11
+ s.email = ['lyfeyaj@gmail.com']
12
12
  s.description = %q{A smart sms verification tool}
13
13
  s.summary = %q{A smart sms verification tool used in China and integrate with yunpian.com}
14
- s.homepage = "https://github.com/lyfeyaj/smart_sms"
15
- s.license = "MIT"
14
+ s.homepage = 'https://github.com/lyfeyaj/smart_sms'
15
+ s.license = 'MIT'
16
16
 
17
17
  s.files = `git ls-files`.split($/)
18
18
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
19
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
- s.require_paths = ["lib"]
20
+ s.require_paths = ['lib']
21
21
 
22
22
  s.add_dependency 'activerecord', ['>= 3.0', '< 5.0']
23
23
  s.add_dependency 'activesupport', ['>= 3.0', '< 5.0']
24
24
 
25
25
  s.add_development_dependency 'bundler', ['>= 1.0.0']
26
26
  s.add_development_dependency 'rake', ['>= 0']
27
- s.add_development_dependency 'rspec', ['>= 0']
27
+ s.add_development_dependency 'rspec-rails', ['>= 0']
28
28
  s.add_development_dependency 'database_cleaner', ['~> 1.2.0']
29
- s.add_development_dependency "webmock", ['~> 1.17.0']
29
+ s.add_development_dependency 'webmock', ['~> 1.17.0']
30
+ s.add_development_dependency 'rails', ['>= 3.1.0']
31
+ s.add_development_dependency 'sqlite3', ['>= 0']
32
+ s.add_development_dependency 'pry', ['>= 0']
30
33
  end
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe SmartSMS::Template do
5
+
6
+ describe '#info' do
7
+ let(:url) { 'http://yunpian.com/v1/user/get.json' }
8
+ subject { SmartSMS::Account.info }
9
+
10
+ before do
11
+ stub_request(:post, url).with(
12
+ body: {
13
+ 'apikey' => SmartSMS.config.api_key
14
+ },
15
+ headers: {
16
+ 'Content-Type' => 'application/x-www-form-urlencoded'
17
+ }
18
+ ).to_return(
19
+ body: {
20
+ 'code' => 0,
21
+ 'msg' => 'OK',
22
+ 'user' => {
23
+ 'nick' => 'LYFEYAJ',
24
+ 'gmt_created' => '2014-04-01 14:00:52',
25
+ 'mobile' => '13096953122',
26
+ 'email' => 'lyfeyaj@gmail.com',
27
+ 'ip_whitelist' => nil,
28
+ 'api_version' => 'v1',
29
+ 'alarm_balance' => 150,
30
+ 'emergency_contact' => '',
31
+ 'emergency_mobile' => '',
32
+ 'balance' => 676
33
+ }
34
+ }.to_json
35
+ )
36
+ end
37
+
38
+ its(['code']) { should eq 0 }
39
+ its(['msg']) { should eq 'OK' }
40
+ its(:keys) { should include 'user' }
41
+ end
42
+
43
+ describe '#set' do
44
+ let(:url) { 'http://yunpian.com/v1/user/set.json' }
45
+ let(:emergency_contact) { 'Felix Liu' }
46
+ let(:emergency_mobile) { '13394738283' }
47
+ let(:alarm_balance) { '100' }
48
+ subject do
49
+ SmartSMS::Account.set(
50
+ emergency_contact: emergency_contact,
51
+ emergency_mobile: emergency_mobile,
52
+ alarm_balance: alarm_balance
53
+ )
54
+ end
55
+
56
+ before do
57
+ stub_request(:post, url).with(
58
+ body: {
59
+ 'apikey' => SmartSMS.config.api_key,
60
+ 'emergency_contact' => emergency_contact,
61
+ 'emergency_mobile' => emergency_mobile,
62
+ 'alarm_balance' => alarm_balance
63
+ },
64
+ headers: {
65
+ 'Content-Type' => 'application/x-www-form-urlencoded'
66
+ }
67
+ ).to_return(
68
+ body: {
69
+ 'code' => 0,
70
+ 'msg' => 'OK',
71
+ 'detail' => nil
72
+ }.to_json
73
+ )
74
+ end
75
+
76
+ its(['code']) { should eq 0 }
77
+ its(['msg']) { should eq 'OK' }
78
+ its(:keys) { should include 'detail' }
79
+ end
80
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe SmartSMS::Configuration do
6
+ subject { SmartSMS.config }
7
+
8
+ describe 'api_key' do
9
+ context 'by_default' do
10
+ its(:api_key) { should == 'fake_api_key' }
11
+ end
12
+ context 'configured via config block' do
13
+ before do
14
+ SmartSMS.configure { |c| c.api_key = 'fdswerfsffsdfdvdsrr23432' }
15
+ end
16
+ its(:api_key) { should == 'fdswerfsffsdfdvdsrr23432' }
17
+ after do
18
+ SmartSMS.configure { |c| c.api_key = 'fake_api_key' }
19
+ end
20
+ end
21
+ end
22
+
23
+ describe 'api_version' do
24
+ context 'by_default' do
25
+ its(:api_version) { should == :v1 }
26
+ end
27
+ context 'configured via config block' do
28
+ before do
29
+ SmartSMS.configure { |c| c.api_version = :v2 }
30
+ end
31
+ its(:api_version) { should == :v2 }
32
+ after do
33
+ SmartSMS.configure { |c| c.api_version = :v1 }
34
+ end
35
+ end
36
+ end
37
+
38
+ describe 'template_id' do
39
+ context 'by_default' do
40
+ its(:template_id) { should == '2' }
41
+ end
42
+ context 'configured via config block' do
43
+ before do
44
+ SmartSMS.configure { |c| c.template_id = '1' }
45
+ end
46
+ its(:template_id) { should == '1' }
47
+ after do
48
+ SmartSMS.configure { |c| c.template_id = '2' }
49
+ end
50
+ end
51
+ end
52
+
53
+ describe 'template_value' do
54
+ context 'by_default' do
55
+ its(:template_value) { should == [:code, :company] }
56
+ end
57
+ context 'configured via config block' do
58
+ before do
59
+ SmartSMS.configure { |c| c.template_value = [:code] }
60
+ end
61
+ its(:template_value) { should == [:code] }
62
+ after do
63
+ SmartSMS.configure { |c| c.template_value = [:code, :company] }
64
+ end
65
+ end
66
+ end
67
+
68
+ describe 'page_num' do
69
+ context 'by_default' do
70
+ its(:page_num) { should == 1 }
71
+ end
72
+ context 'configured via config block' do
73
+ before do
74
+ SmartSMS.configure { |c| c.page_num = 2 }
75
+ end
76
+ its(:page_num) { should == 2 }
77
+ after do
78
+ SmartSMS.configure { |c| c.page_num = 1 }
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'page_size' do
84
+ context 'by_default' do
85
+ its(:page_size) { should == 20 }
86
+ end
87
+ context 'configured via config block' do
88
+ before do
89
+ SmartSMS.configure { |c| c.page_size = 50 }
90
+ end
91
+ its(:page_size) { should == 50 }
92
+ after do
93
+ SmartSMS.configure { |c| c.page_size = 20 }
94
+ end
95
+ end
96
+ end
97
+
98
+ describe 'company' do
99
+ context 'by_default' do
100
+ its(:company) { should == 'Smart SMS' }
101
+ end
102
+ context 'configured via config block' do
103
+ before do
104
+ SmartSMS.configure { |c| c.company = 'Edgepeek' }
105
+ end
106
+ its(:company) { should == 'Edgepeek' }
107
+ after do
108
+ SmartSMS.configure { |c| c.company = 'Smart SMS' }
109
+ end
110
+ end
111
+ end
112
+
113
+ describe 'expires_in' do
114
+ context 'by_default' do
115
+ its(:expires_in) { should == 1.hour }
116
+ end
117
+ context 'configured via config block' do
118
+ before do
119
+ SmartSMS.configure { |c| c.expires_in = 45.minutes }
120
+ end
121
+ its(:expires_in) { should == 45.minutes }
122
+ after do
123
+ SmartSMS.configure { |c| c.expires_in = 1.hour }
124
+ end
125
+ end
126
+ end
127
+
128
+ describe 'default_interval' do
129
+ context 'by_default' do
130
+ its(:default_interval) { should == 1.day }
131
+ end
132
+ context 'configured via config block' do
133
+ before do
134
+ SmartSMS.configure { |c| c.default_interval = 2.day }
135
+ end
136
+ its(:default_interval) { should == 2.day }
137
+ after do
138
+ SmartSMS.configure { |c| c.default_interval = 1.day }
139
+ end
140
+ end
141
+ end
142
+
143
+ describe 'store_sms_in_local' do
144
+ context 'by_default' do
145
+ its(:store_sms_in_local) { should == true }
146
+ end
147
+ context 'configured via config block' do
148
+ before do
149
+ SmartSMS.configure { |c| c.store_sms_in_local = false }
150
+ end
151
+ its(:store_sms_in_local) { should == false }
152
+ after do
153
+ SmartSMS.configure { |c| c.store_sms_in_local = true }
154
+ end
155
+ end
156
+ end
157
+
158
+ describe 'verification_code_algorithm' do
159
+ context 'by_default' do
160
+ its(:verification_code_algorithm) { should == :simple }
161
+ end
162
+ context 'configured via config block' do
163
+ before do
164
+ SmartSMS.configure { |c| c.verification_code_algorithm = :middle }
165
+ end
166
+ its(:verification_code_algorithm) { should == :middle }
167
+ after do
168
+ SmartSMS.configure { |c| c.verification_code_algorithm = :simple }
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,9 @@
1
+ # Database
2
+ ActiveRecord::Base.configurations = {
3
+ 'test' => {
4
+ adapter: 'sqlite3',
5
+ database: ':memory:'
6
+ }
7
+ }
8
+
9
+ ActiveRecord::Base.establish_connection(:test)
@@ -0,0 +1,42 @@
1
+ # Model
2
+ class User < ActiveRecord::Base
3
+ has_sms_verification
4
+ end
5
+
6
+ # Model with customized columns
7
+ class Account < ActiveRecord::Base
8
+ has_sms_verification :mobile, :confirmed_at
9
+ end
10
+
11
+ # Migrations
12
+ class CreateAllTables < ActiveRecord::Migration
13
+ def self.up
14
+ create_table(:users) do |t|
15
+ t.string :phone
16
+ t.datetime :verified_at
17
+ end
18
+
19
+ create_table(:accounts) do |t|
20
+ t.string :mobile
21
+ t.datetime :confirmed_at
22
+ end
23
+
24
+ create_table :smart_sms_messages do |t|
25
+ t.string :sid
26
+ t.string :mobile
27
+ t.datetime :send_time
28
+ t.text :text
29
+ t.string :code
30
+ t.string :send_status
31
+ t.string :report_status
32
+ t.string :fee
33
+ t.datetime :user_receive_time
34
+ t.text :error_msg
35
+ t.belongs_to :smsable, polymorphic: true
36
+ end
37
+ add_index :smart_sms_messages, :sid
38
+ add_index :smart_sms_messages, [:smsable_id, :smsable_type]
39
+ end
40
+ end
41
+ ActiveRecord::Migration.verbose = false
42
+ CreateAllTables.up
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ SmartSMS.configure do |config|
4
+ config.api_key = 'fake_api_key'
5
+ config.api_version = :v1
6
+ config.template_id = '2'
7
+ config.template_value = [:code, :company]
8
+ config.page_num = 1
9
+ config.page_size = 20
10
+ config.company = 'Smart SMS'
11
+ config.expires_in = 1.hour
12
+ config.default_interval = 1.day
13
+ config.store_sms_in_local = true
14
+ config.verification_code_algorithm = :simple
15
+ end
@@ -0,0 +1,23 @@
1
+ # require 'rails/all'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ require 'fake_app/active_record/config' if defined? ActiveRecord
6
+
7
+ # Config
8
+ app = Class.new(Rails::Application)
9
+ app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
10
+ app.config.session_store :cookie_store, key: '_myapp_session'
11
+ app.config.active_support.deprecation = :log
12
+ app.config.eager_load = false
13
+
14
+ # Rais.root
15
+ app.config.root = File.dirname(__FILE__)
16
+ Rails.backtrace_cleaner.remove_silencers!
17
+ app.initialize!
18
+
19
+ # Initializer
20
+ require 'fake_app/initializers/smart_sms'
21
+
22
+ # Model
23
+ require 'fake_app/active_record/models' if defined? ActiveRecord
@@ -0,0 +1,275 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'SmartSMS::HasSmsVerification' do
5
+ let(:user) { User.create phone: '13764071479' }
6
+ let(:account) { Account.create mobile: '13764071479' }
7
+
8
+ context '#verify!' do
9
+ let(:verification_code) { SmartSMS::VerificationCode.simple }
10
+
11
+ describe 'Model User with default columns' do
12
+ before do
13
+ 3.times { user.deliver_fake_sms }
14
+ user.deliver_fake_sms verification_code
15
+ end
16
+
17
+ context 'with correct verification_code' do
18
+ before { user.verify! verification_code }
19
+
20
+ it 'should be verified' do
21
+ expect { user.verify! verification_code }.to be_true
22
+ expect(user.reload.verified?).to be_true
23
+ end
24
+
25
+ it 'should have right verification code' do
26
+ expect(user.latest_message.code).to eq verification_code
27
+ end
28
+
29
+ it 'should have 4 messages' do
30
+ expect(user.messages.count).to eq 4
31
+ end
32
+
33
+ it 'should not be verified by using expired verification code' do
34
+ user.verified_at = nil
35
+ user.save
36
+ expect(user.verify!(user.messages.first.code)).to be_nil
37
+ end
38
+
39
+ it 'should be the latest_message' do
40
+ expect(user.latest_message).to eq user.messages.last
41
+ end
42
+ end
43
+
44
+ context 'with incorrect verification_code' do
45
+ before { user.verify! 'kfdsfd' }
46
+
47
+ it 'should not be verified' do
48
+ expect(user.verify!('kfdsfd')).to be_nil
49
+ expect(user.reload.verified?).to be_false
50
+ end
51
+
52
+ it 'should not have right verification code' do
53
+ expect(user.latest_message.code).not_to eq 'kfdsfd'
54
+ end
55
+
56
+ it 'should have 4 messages' do
57
+ expect(user.messages.count).to eq 4
58
+ end
59
+
60
+ it 'should not be verified by using expired verification code' do
61
+ user.verified_at = nil
62
+ user.save
63
+ expect(user.verify!(user.messages.first.code)).to be_nil
64
+ end
65
+
66
+ it 'should be the latest_message' do
67
+ expect(user.latest_message).to eq user.messages.last
68
+ end
69
+ end
70
+ end
71
+
72
+ describe 'Model Account with custom columns' do
73
+ before do
74
+ 3.times { account.deliver_fake_sms }
75
+ account.deliver_fake_sms verification_code
76
+ end
77
+
78
+ context 'with correct verification_code' do
79
+ before { account.verify! verification_code }
80
+
81
+ it 'should be verified' do
82
+ expect { account.verify! verification_code }.to be_true
83
+ expect(account.reload.verified?).to be_true
84
+ end
85
+
86
+ it 'should have right verification code' do
87
+ expect(account.latest_message.code).to eq verification_code
88
+ end
89
+
90
+ it 'should have 4 messages' do
91
+ expect(account.messages.count).to eq 4
92
+ end
93
+
94
+ it 'should not be verified by using expired verification code' do
95
+ account.confirmed_at = nil
96
+ account.save
97
+ expect(account.verify!(account.messages.first.code)).to be_nil
98
+ end
99
+
100
+ it 'should be the latest_message' do
101
+ expect(account.latest_message).to eq account.messages.last
102
+ end
103
+
104
+ it 'should be the same value for verified_at and confirmed_at' do
105
+ expect(account.verified_at).to eq account.confirmed_at
106
+ end
107
+ end
108
+
109
+ context 'with incorrect verification_code' do
110
+ before { account.verify! 'kfdsfd' }
111
+
112
+ it 'should not be verified' do
113
+ expect(account.verify!('kfdsfd')).to be_nil
114
+ expect(account.reload.verified?).to be_false
115
+ end
116
+
117
+ it 'should not have right verification code' do
118
+ expect(account.latest_message.code).not_to eq 'kfdsfd'
119
+ end
120
+
121
+ it 'should have 4 messages' do
122
+ expect(account.messages.count).to eq 4
123
+ end
124
+
125
+ it 'should not be verified by using expired verification code' do
126
+ account.confirmed_at = nil
127
+ account.save
128
+ expect(account.verify!(account.messages.first.code)).to be_nil
129
+ end
130
+
131
+ it 'should be the latest_message' do
132
+ expect(account.latest_message).to eq account.messages.last
133
+ end
134
+
135
+ it 'should be the same value for verified_at and confirmed_at' do
136
+ expect(account.verified_at).to eq account.confirmed_at
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ context '#verify' do
143
+ let(:verification_code) { SmartSMS::VerificationCode.simple }
144
+
145
+ describe 'Model User with default columns' do
146
+ before do
147
+ 3.times { user.deliver_fake_sms }
148
+ user.deliver_fake_sms verification_code
149
+ end
150
+
151
+ context 'with correct verification_code' do
152
+ before { user.verify verification_code }
153
+
154
+ it 'should be verified' do
155
+ expect { user.verify verification_code }.to be_true
156
+ expect(user.reload.verified?).to be_false
157
+ end
158
+
159
+ it 'should have right verification code' do
160
+ expect(user.latest_message.code).to eq verification_code
161
+ end
162
+
163
+ it 'should have 4 messages' do
164
+ expect(user.messages.count).to eq 4
165
+ end
166
+
167
+ it 'should not be verified by using expired verification code' do
168
+ user.verified_at = nil
169
+ user.save
170
+ expect(user.verify(user.messages.first.code)).to be_false
171
+ end
172
+
173
+ it 'should be the latest_message' do
174
+ expect(user.latest_message).to eq user.messages.last
175
+ end
176
+ end
177
+
178
+ context 'with incorrect verification_code' do
179
+ before { user.verify 'kfdsfd' }
180
+
181
+ it 'should not be verified' do
182
+ expect(user.verify('kfdsfd')).to be_false
183
+ expect(user.reload.verified?).to be_false
184
+ end
185
+
186
+ it 'should not have right verification code' do
187
+ expect(user.latest_message.code).not_to eq 'kfdsfd'
188
+ end
189
+
190
+ it 'should have 4 messages' do
191
+ expect(user.messages.count).to eq 4
192
+ end
193
+
194
+ it 'should not be verified by using expired verification code' do
195
+ user.verified_at = nil
196
+ user.save
197
+ expect(user.verify(user.messages.first.code)).to be_false
198
+ end
199
+
200
+ it 'should be the latest_message' do
201
+ expect(user.latest_message).to eq user.messages.last
202
+ end
203
+ end
204
+ end
205
+
206
+ describe 'Model Account with custom columns' do
207
+ before do
208
+ 3.times { account.deliver_fake_sms }
209
+ account.deliver_fake_sms verification_code
210
+ end
211
+
212
+ context 'with correct verification_code' do
213
+ before { account.verify verification_code }
214
+
215
+ it 'should be verified' do
216
+ expect { account.verify verification_code }.to be_true
217
+ expect(account.reload.verified?).to be_false
218
+ end
219
+
220
+ it 'should have right verification code' do
221
+ expect(account.latest_message.code).to eq verification_code
222
+ end
223
+
224
+ it 'should have 4 messages' do
225
+ expect(account.messages.count).to eq 4
226
+ end
227
+
228
+ it 'should not be verified by using expired verification code' do
229
+ account.confirmed_at = nil
230
+ account.save
231
+ expect(user.verify(account.messages.first.code)).to be_false
232
+ end
233
+
234
+ it 'should be the latest_message' do
235
+ expect(account.latest_message).to eq account.messages.last
236
+ end
237
+
238
+ it 'should be the same value for verified_at and confirmed_at' do
239
+ expect(account.verified_at).to eq account.confirmed_at
240
+ end
241
+ end
242
+
243
+ context 'with incorrect verification_code' do
244
+ before { account.verify 'kfdsfd' }
245
+
246
+ it 'should not be verified' do
247
+ expect(account.verify('kfdsfd')).to be_false
248
+ expect(account.reload.verified?).to be_false
249
+ end
250
+
251
+ it 'should not have right verification code' do
252
+ expect(account.latest_message.code).not_to eq 'kfdsfd'
253
+ end
254
+
255
+ it 'should have 4 messages' do
256
+ expect(account.messages.count).to eq 4
257
+ end
258
+
259
+ it 'should not be verified by using expired verification code' do
260
+ account.confirmed_at = nil
261
+ account.save
262
+ expect(account.verify(account.messages.first.code)).to be_false
263
+ end
264
+
265
+ it 'should be the latest_message' do
266
+ expect(account.latest_message).to eq account.messages.last
267
+ end
268
+
269
+ it 'should be the same value for verified_at and confirmed_at' do
270
+ expect(account.verified_at).to eq account.confirmed_at
271
+ end
272
+ end
273
+ end
274
+ end
275
+ end