As I said in a comment on your question, I am the author of the twilio-rb gem you mentioned. At the top of my head, I would use a verification resource to which you send the phone number.
POST /verifications.voice { telephone_number: '+12125551234' }
In the create action, use Twilio::Call.create to create a new call with Twilio
def create @verification = Verification.new params[:verification] if @verification.save Twilio::Call.create to: @verification.telephone_number, from: YOUR_CALLER_ID, url: verification_url(@verification, format: :voice)
You will also want to save any API errors that twilio-rb may raise. The URL refers to the show action of the validation resource instance. Then Twilio dials the phone number indicated on the phone, and when the call is connected, it asks for a URL, for example. GET /verifications/1.voice , so you will need a view view that requests a verification code and collects numbers using <Gather> verb :
res.gather num_digits: 4, action: twilio_hack_verification_url(@verification, :format => :voice), method: 'POST' do |form| form.say 'Please enter the your 4 digit verification code' end
Since Twilio does not currently implement the PUT verb, you must add a member to your resource
resources :verifications do member { post 'twilio_hack' } end
Then in your controller, update the object using user input:
def twilio_hack @verification = Verification.find(params[:id]).tap do |v| v.user_input params['Digits'] v.save end if @verification.confirmed?
Finally, in your model you will need a code that generates a verification code and checks to see if it is validated.
class Verification < ActiveRecord::Base before_save -> { self[:confirmed] = true if user_input == verification_code }, if: user_input before_create -> { self[:verification_code] = rand.to_s[2..5] } end
This is all untested from head to head, which we thought about 2 minutes, but you need to start.