After finishing Michael H.'s tutorial on Rails, I’m trying to build a
call-tracking app with Twilio, to get a sense of things.
So far, I’ve managed to make an authentication system, a plans system,
integrated stripe, and integrated the creation of Twilio subaccounts
with user signup.
Now, I’m looking to figure out how to get users to search for a phone
number, choose from the list, and buy the phone number.
They have documentation on this here – >
http://www.twilio.com/docs/howto/search-and-buy
But, the Sinatra is really confusing me. I’m not sure how to separate
the views from the controllers while looking at that code.
Currently, I have 2 relevant models
The User model, which has_many phone numbers.
I tried creating a new controller, the find_numbers controller, that
would do the search for the numbers, and transmit the chosen number to
the phone_numbers controller, but ended up riddled with errors.
Could someone give me pointers on how I would convert the following to
Rails controller/views?
=========================================
We take the user’s criteria and search for available phone numbers that
match
post ‘/search-numbers’ do
account_sid = ‘ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
auth_token = ‘YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY’
client = Twilio::REST::Client.new(account_sid, auth_token)
####After here I’m confused!!
search_params = {}
%w[in_postal_code near_number contains].each do |p|
search_params[p] = params[p] unless params[p].nil? ||
params[p].empty?
end
begin
local_numbers =
client.account.available_phone_numbers.get(‘US’).local
numbers = local_numbers.list(search_params)
unless numbers.empty?
out = '<html><head><title>Choose a
number
Choose a number
’numbers.each do |number|
out << “”
out << “#{number.friendly_name}”
out << “”
out << “”
end
out << ‘’
else
‘Sorry! Twilio doesn't have any numbers available that
match those constraints.’
end
rescue StandardError => e
'Sorry! ’ + e.message + ‘.’
end
end