Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
Canonicalize phone numbers to match Twilio
Browse files Browse the repository at this point in the history
  • Loading branch information
martymcguire committed Jan 13, 2011
1 parent c0a0607 commit a208a60
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/phone.rb
Expand Up @@ -2,6 +2,7 @@ class Phone < ActiveRecord::Base
belongs_to :user

before_create :generate_verification_code
before_save :canonicalize_phone

def verified?
verification_code.nil?
Expand All @@ -15,4 +16,13 @@ def generate_verification_code
o = [('0'..'9'),('A'..'Z')].map{|i| i.to_a}.flatten;
self['verification_code'] = (0..5).map{ o[rand(o.length)] }.join;
end

def canonicalize_phone
if(self['number'])
self['number'].gsub!(/[- ()]/,'')
if(! self['number'].starts_with? '+1')
self['number'].gsub!(/^/,'+1')
end
end
end
end

0 comments on commit a208a60

Please sign in to comment.