Last active
August 29, 2015 14:14
-
-
Save bionicpill/27e3909100eeb7dbe2ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/models/forwarding_phone.rb b/app/models/forwarding_phone.rb | |
index af93735..d4a9854 100644 | |
--- a/app/models/forwarding_phone.rb | |
+++ b/app/models/forwarding_phone.rb | |
@@ -46,15 +46,7 @@ class ForwardingPhone < ActiveRecord::Base | |
end | |
def provision_number | |
- if product_session? | |
- voice_url = incoming_call_product_session_url | |
- status_callback = call_status_product_session_url | |
- else | |
- voice_url = incoming_call_advice_session_url | |
- status_callback = call_status_advice_session_url | |
- end | |
- | |
- twilio_number = TWILIO_CLIENT.account.incoming_phone_numbers.create( | |
+ twilio_number = TwilioPhoneNumberProvisioning.provision_number( | |
:phone_number => available_phone_number, | |
:voice_url => voice_url, | |
:status_callback => status_callback | |
@@ -62,17 +54,26 @@ class ForwardingPhone < ActiveRecord::Base | |
self.sid = twilio_number.sid | |
self.provisioned_phone_number = twilio_number.phone_number | |
- rescue Twilio::REST::RequestError => e | |
- # TODO: Handle twilio error codes | |
- # 21421: Invalid number | |
- # 21422: Unavailable number | |
- raise e | |
+ end | |
+ | |
+ def status_callback | |
+ if product_session? | |
+ call_status_product_session_url | |
+ else | |
+ call_status_advice_session_url | |
+ end | |
+ end | |
+ | |
+ def voice_url | |
+ if product_session? | |
+ incoming_call_product_session_url | |
+ else | |
+ incoming_call_advice_session_url | |
+ end | |
end | |
def deprovision_number | |
- TWILIO_CLIENT.account.incoming_phone_numbers.get(sid).delete | |
- rescue Twilio::REST::RequestError | |
- # Eat the twilio error, incase the phone number was already deprovisioned through another means. | |
+ TwillioPhoneNumberProvisioning.deprovision_number(sid) | |
end | |
def available_phone_number | |
diff --git a/app/models/twilio_phone_number_provisioning.rb b/app/models/twilio_phone_number_provisioning.rb | |
index e69de29..21d7b02 100644 | |
--- a/app/models/twilio_phone_number_provisioning.rb | |
+++ b/app/models/twilio_phone_number_provisioning.rb | |
@@ -0,0 +1,39 @@ | |
+class TwilioPhoneNumberProvisioning | |
+ | |
+ class FakeTwilioNumber | |
+ attr_accessor :sid, | |
+ :phone_number | |
+ | |
+ def initialize | |
+ @sid = 'ThisIsAFakeSIDForMyFakeTwilioPhone' | |
+ @phone_number = Settings.new('twilio')['fake_provisioned_number'] | |
+ end | |
+ end | |
+ | |
+ def self.provision_number(create_options) | |
+ return FakeTwilioNumber.new unless Rails.env.production? | |
+ TWILIO_CLIENT.account.incoming_phone_numbers.create(create_options) | |
+ | |
+ rescue Twilio::REST::RequestError => e | |
+ tries ||= Settings.new('twilio')['retry_count'].to_i | |
+ case e.code | |
+ when 21421 # Invalid number | |
+ retry unless (tries -= 1).zero? | |
+ when 21422 # Unavailable number | |
+ retry unless (tries -= 1).zero? | |
+ else | |
+ raise e | |
+ end | |
+ else | |
+ raise e | |
+ end | |
+ | |
+ def self.deprovision_number(sid) | |
+ return unless Rails.env.production? | |
+ TWILIO_CLIENT.account.incoming_phone_numbers.get(sid).delete | |
+ rescue Twilio::REST::RequestError | |
+ # Assume number was already deprovisioned | |
+ end | |
+ | |
+ | |
+end | |
diff --git a/config/twilio.yml b/config/twilio.yml | |
index 4aa6818..54fa697 100644 | |
--- a/config/twilio.yml | |
+++ b/config/twilio.yml | |
@@ -6,6 +6,8 @@ test: | |
subaccount_token: 80ec616bcfc707b12928e457c21d021c | |
sms_sender: "+15005550006" | |
package_sms_sender: "+15005550007" | |
+ fake_provisioned_number: "+15005550008" | |
+ retry_count: 3 | |
# Ocato Development subaccount (REAL) | |
development: | |
@@ -15,6 +17,8 @@ development: | |
subaccount_token: f03f12a92a94206c8856d733ed55cd6e | |
sms_sender: "+12067455851" | |
package_sms_sender: "+13602076879" | |
+ fake_provisioned_number: "+15005550008" | |
+ retry_count: 3 | |
whitelist: | |
- '205-617-3307' # Kyle Rippey | |
- '206-399-1220' # Henry Ho | |
@@ -29,6 +33,8 @@ ec2: | |
subaccount_token: f03f12a92a94206c8856d733ed55cd6e | |
sms_sender: "+12067455851" | |
package_sms_sender: "+13602076879" | |
+ fake_provisioned_number: "+15005550008" | |
+ retry_count: 3 | |
whitelist: | |
- '206-607-9046' # Becker | |
- '205-617-3307' # Kyle Rippey | |
@@ -46,3 +52,4 @@ production: | |
subaccount_token: 8c6aa3a07f302da3713331fc0d7a874c | |
sms_sender: "+12067455905" | |
package_sms_sender: "" | |
+ retry_count: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment