twilio_contactable 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +24 -19
- data/VERSION +1 -1
- data/lib/contactable.rb +17 -3
- data/lib/twilio_contactable.rb +1 -3
- data/test/twilio_contactable_contactable_test.rb +34 -0
- data/twilio_contactable.gemspec +2 -2
- metadata +3 -9
data/README.markdown
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
Twilio Contactable
|
2
2
|
=====
|
3
3
|
|
4
|
-
Twilio makes voice and SMS interactions easy. But if you want to
|
5
|
-
|
4
|
+
Twilio makes voice and SMS interactions easy. But if you want to ensure that your user's phone numbers really belong
|
5
|
+
to them there's a lot of buggy code you'll have to add in your Ruby app. Unless you use this gem.
|
6
6
|
|
7
7
|
Don't Write Twilio Ruby Code Like It's PHP
|
8
8
|
==
|
9
9
|
|
10
|
-
You don't want to be passing strings around and writing procedural code in your Ruby app. This gem lets you ask for a phone number from your users (or any ActiveRecord
|
10
|
+
You don't want to be passing strings around between multiple web requests and writing procedural code in your Ruby app. Not for such a simple feature. This gem lets you ask for a phone number from your users (or any ActiveRecord-like Ruby class), confirm their ownership of it via SMS or Voice (or both), and automatically handle number invalidation.
|
11
11
|
|
12
12
|
|
13
13
|
Install It
|
@@ -15,8 +15,14 @@ Install It
|
|
15
15
|
|
16
16
|
Install TwilioContactable as a gem:
|
17
17
|
|
18
|
-
gem install twilio_contactable
|
19
|
-
|
18
|
+
$ gem install twilio_contactable
|
19
|
+
|
20
|
+
# For Rails add it in environment.rb:
|
21
|
+
config.gem 'twilio_contactable'
|
22
|
+
|
23
|
+
# then edit your .gems file or run
|
24
|
+
$ rake gems:unpack
|
25
|
+
# to unpack the gem into your vendor/gems directory
|
20
26
|
|
21
27
|
Or as a Plugin:
|
22
28
|
|
@@ -27,7 +33,7 @@ Connect This Code To Your App
|
|
27
33
|
==
|
28
34
|
|
29
35
|
|
30
|
-
Include Twilio::Contactable into your User class or whatever you're using to represent an entity with a phone number.
|
36
|
+
Include Twilio::Contactable into your User class or whatever you're using to represent an entity with a contactable phone number.
|
31
37
|
|
32
38
|
class User < ActiveRecord::Base
|
33
39
|
|
@@ -37,7 +43,7 @@ Include Twilio::Contactable into your User class or whatever you're using to rep
|
|
37
43
|
|
38
44
|
end
|
39
45
|
|
40
|
-
If you're using custom column names you can easily overwrite any of them:
|
46
|
+
If you're using custom column names you can easily overwrite any of them by passing in a configuration block:
|
41
47
|
|
42
48
|
class User < ActiveRecord::Base
|
43
49
|
|
@@ -77,8 +83,8 @@ You'll need to add those columns to your database table using a migration that l
|
|
77
83
|
t.string :voice_confirmed_phone_number
|
78
84
|
end
|
79
85
|
|
80
|
-
You don't necessarily need all those columns though. Say you have users that
|
81
|
-
just need to have their retail phone number
|
86
|
+
You don't necessarily need all those columns though. Say you have users that want to use SMS and business locations that
|
87
|
+
just need to have their retail phone number to confirm their identity:
|
82
88
|
|
83
89
|
change_table :users do |t|
|
84
90
|
t.string :phone_number
|
@@ -99,21 +105,23 @@ just need to have their retail phone number validated:
|
|
99
105
|
|
100
106
|
Both the User and the BusinessLocation models are now prepared for SMS and Voice confirmation, respectively.
|
101
107
|
|
102
|
-
You'll also need to create a controller that is capable of receiving connections from Twilio.com
|
108
|
+
You'll also need to create a controller that is capable of receiving connections from Twilio.com. You can reuse an existing controller or
|
109
|
+
create a new one just for this task. TwilioContactable will guess the path from the controller name, so if you include it into a class called CheckPhoneNumbersController:
|
103
110
|
|
104
|
-
# app/controllers/
|
105
|
-
class
|
111
|
+
# app/controllers/check_phone_numbers_controller.rb
|
112
|
+
class CheckPhoneNumbersController < ActionController::Base
|
106
113
|
include TwilioContactable::Controller
|
107
114
|
|
108
115
|
# any models that you want to have phone numbers confirmed for:
|
109
116
|
twilio_contactable User, BusinessLocation
|
110
117
|
end
|
111
118
|
|
119
|
+
then Twilio.com would try to contact your site at '/check_phone_numbers'. As long as that route works then you don't need to configure anything else.
|
112
120
|
|
113
|
-
Configure It With Twilio Account Info
|
121
|
+
Configure It With Your Twilio Account Info
|
114
122
|
==
|
115
123
|
|
116
|
-
Because it can be expensive to send TXTs or make calls accidentally, it's required that you manually
|
124
|
+
Because it can be expensive to send TXTs or make calls accidentally, it's required that you manually set TwilioContactable.mode in your app. Put this line in config/environments/production.rb or anything that loads _only_ in your production environment:
|
117
125
|
|
118
126
|
TwilioContactable.mode = :live
|
119
127
|
|
@@ -203,12 +211,9 @@ Make sure Twilio.com knows to POST all SMS messages and block notices to you at:
|
|
203
211
|
|
204
212
|
This gem will handle all those incoming messages automatically. Now if your users reply to an SMS with 'STOP' or 'BLOCK' the appropriate record in your database will be updated so that sms messages no longer can be sent to them (i.e.: @user.sms_blocked? will be true)
|
205
213
|
|
206
|
-
All other incoming TXTs (besides 'BLOCK' and 'STOP') from a user will automatically be sent to that user's record:
|
214
|
+
All other incoming TXTs (besides 'BLOCK' and 'STOP') from a user will automatically be sent to that user's record. If "I love you!" is sent to you from a user with the phone number "555-111-9999" then the following will be executed:
|
207
215
|
|
208
|
-
|
209
|
-
# the phone number "555-111-9999"
|
210
|
-
# then the following will be executed:
|
211
|
-
User.find_by_formatted_phone_number('+15551119999').receive_sms("I love you!")
|
216
|
+
User.find_by_formatted_phone_number('+15551119999').receive_sms("I love you!")
|
212
217
|
|
213
218
|
It's up to you to implement the 'receive_sms' method on User.
|
214
219
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.5
|
data/lib/contactable.rb
CHANGED
@@ -79,7 +79,7 @@ module TwilioContactable
|
|
79
79
|
|
80
80
|
def has_valid_phone_number?
|
81
81
|
format_phone_number
|
82
|
-
_TC_formatted_phone_number =~ /^\+[\d]{
|
82
|
+
_TC_formatted_phone_number =~ /^\+[\d]{10,12}$/
|
83
83
|
end
|
84
84
|
|
85
85
|
# Sends an SMS validation request through the gateway
|
@@ -89,7 +89,14 @@ module TwilioContactable
|
|
89
89
|
return false if _TC_phone_number.blank?
|
90
90
|
|
91
91
|
format_phone_number
|
92
|
-
confirmation_code =
|
92
|
+
confirmation_code =
|
93
|
+
if sms_confirmation_attempted &&
|
94
|
+
sms_confirmation_code &&
|
95
|
+
sms_confirmation_attempted > Time.now.utc - 60*5
|
96
|
+
sms_confirmation_code
|
97
|
+
else
|
98
|
+
TwilioContactable.generate_confirmation_code
|
99
|
+
end
|
93
100
|
|
94
101
|
# Use this class' confirmation_message method if it
|
95
102
|
# exists, otherwise use the generic message
|
@@ -118,7 +125,14 @@ module TwilioContactable
|
|
118
125
|
return false if _TC_phone_number.blank?
|
119
126
|
|
120
127
|
format_phone_number
|
121
|
-
confirmation_code =
|
128
|
+
confirmation_code =
|
129
|
+
if voice_confirmation_attempted &&
|
130
|
+
voice_confirmation_code &&
|
131
|
+
voice_confirmation_attempted > Time.now.utc - 60*5
|
132
|
+
voice_confirmation_code
|
133
|
+
else
|
134
|
+
TwilioContactable.generate_confirmation_code
|
135
|
+
end
|
122
136
|
|
123
137
|
response = TwilioContactable::Gateway.initiate_voice_call(self, _TC_formatted_phone_number)
|
124
138
|
|
data/lib/twilio_contactable.rb
CHANGED
@@ -34,6 +34,40 @@ class TwilioContactableContactableTest < ActiveSupport::TestCase
|
|
34
34
|
should "normalize phone number" do
|
35
35
|
assert_equal '+15551234567', @user.formatted_phone_number
|
36
36
|
end
|
37
|
+
context "has_valid_phone_number?" do
|
38
|
+
context "with a two-digit country code" do
|
39
|
+
setup {
|
40
|
+
@user.update_attribute :phone_number, '+11 (222) 333-7777'
|
41
|
+
}
|
42
|
+
should "be valid" do
|
43
|
+
assert @user.has_valid_phone_number?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context "with a one-digit country code" do
|
47
|
+
setup {
|
48
|
+
@user.update_attribute :phone_number, '+1 (222) 333-7777'
|
49
|
+
}
|
50
|
+
should "be valid" do
|
51
|
+
assert @user.has_valid_phone_number?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
context "with no country code" do
|
55
|
+
setup {
|
56
|
+
@user.update_attribute :phone_number, '(222) 333-7777'
|
57
|
+
}
|
58
|
+
should "be valid" do
|
59
|
+
assert @user.has_valid_phone_number?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
context "with no area code" do
|
63
|
+
setup {
|
64
|
+
@user.update_attribute :phone_number, '333-7777'
|
65
|
+
}
|
66
|
+
should "not be valid" do
|
67
|
+
assert !@user.has_valid_phone_number?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
37
71
|
context "when phone number is blank" do
|
38
72
|
setup { @user._TC_phone_number = nil}
|
39
73
|
context "confirming phone number" do
|
data/twilio_contactable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{twilio_contactable}
|
8
|
-
s.version = "0.8.
|
8
|
+
s.version = "0.8.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jack Danger Canty"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-03}
|
13
13
|
s.description = %q{Does all the hard work with letting you confirm your user's phone numbers for Voice or TXT over the Twilio API}
|
14
14
|
s.email = %q{gitcommit@6brand.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twilio_contactable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 55
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
8
|
+
- 5
|
9
|
+
version: 0.8.5
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Jack Danger Canty
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-03 00:00:00 -07:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 5
|
30
28
|
segments:
|
31
29
|
- 2
|
32
30
|
- 0
|
@@ -42,7 +40,6 @@ dependencies:
|
|
42
40
|
requirements:
|
43
41
|
- - ">="
|
44
42
|
- !ruby/object:Gem::Version
|
45
|
-
hash: 3
|
46
43
|
segments:
|
47
44
|
- 0
|
48
45
|
version: "0"
|
@@ -56,7 +53,6 @@ dependencies:
|
|
56
53
|
requirements:
|
57
54
|
- - ">="
|
58
55
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 3
|
60
56
|
segments:
|
61
57
|
- 0
|
62
58
|
version: "0"
|
@@ -102,7 +98,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
98
|
requirements:
|
103
99
|
- - ">="
|
104
100
|
- !ruby/object:Gem::Version
|
105
|
-
hash: 3
|
106
101
|
segments:
|
107
102
|
- 0
|
108
103
|
version: "0"
|
@@ -111,7 +106,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
106
|
requirements:
|
112
107
|
- - ">="
|
113
108
|
- !ruby/object:Gem::Version
|
114
|
-
hash: 3
|
115
109
|
segments:
|
116
110
|
- 0
|
117
111
|
version: "0"
|