Permalink
Browse files

Added AvailablePhoneNumbers search support.

  • Loading branch information...
1 parent d0184f4 commit 83a29761d9d63455d40a8d8a4fff997a32a6e493 @amerine amerine committed Dec 21, 2010
View
@@ -35,4 +35,5 @@
require 'twilio/recording'
require 'twilio/toll_free_phone_number'
require 'twilio/conference'
-require 'twilio/verb'
+require 'twilio/verb'
+require 'twilio/available_phone_numbers'
@@ -0,0 +1,54 @@
+module Twilio
+ # An AvailablePhoneNumbers resources represents the available phone numbers
+ # that Twilio can provide you based on your search criteria. The valid
+ # query terms are outlined in the search methods.
+ # Example:
+ # Twilio.connect('my_twilio_sid', 'my_auth_token')
+ # Twilio.AvailablePhoneNumbers.search_local(:area_code => 541)
+ class AvailablePhoneNumbers < TwilioObject
+
+ # The Search method handles the searching of both local and toll-free
+ # numbers.
+ def search(opts={})
+ iso_country_code = opts[:iso_country_code] || 'US'
+ resource = opts[:resource]
+ Twilio.get("/AvailablePhoneNumbers/#{iso_country_code}/#{resource}",
+ :query => {
+ :AreaCode => opts[:area_code],
+ :InPostalCode => opts[:postal_code],
+ :InRegion => opts[:in_region],
+ :Contains => opts[:contains],
+ :NearLatLong => opts[:near_lat_long],
+ :NearNumber => opts[:near_number],
+ :InLata => opts[:in_lata],
+ :InRateCenter => opts[:in_rate_center],
+ :Distance => opts[:distance]
+ }.reject {|k,v| v == nil})
+ end
+
+ # The search_local method searches for numbers in local areas (i.e. state, zip, etc..)
+ # Search Options:
+ # :area_code
+ # :postal_code
+ # :in_region
+ # :contains
+ # :near_lat_long
+ # :near_number
+ # :in_lata
+ # :in_rate_center
+ # :distance
+ def search_local(opts ={})
+ opts = {:resource => 'Local'}.merge(opts)
+ search(opts)
+ end
+
+ # The search_toll_free method searches for available toll-free numbers
+ # Search Options
+ # :contains
+ def search_toll_free(opts ={})
+ opts = {:resource => 'TollFree'}.merge(opts)
+ search(opts)
+ end
+ end
+end
+
@@ -0,0 +1,26 @@
+TwilioResponse>
+ <AvailablePhoneNumbers uri="/2010-04-01/Accounts/ACde6f1e11047ebd6fe7a55f120be3a900/AvailablePhoneNumbers/US/Local?AreaCode=510">
+ <AvailablePhoneNumber>
+ <FriendlyName>(510) 564-7903</FriendlyName>
+ <PhoneNumber>+15105647903</PhoneNumber>
+ <Lata>722</Lata>
+ <RateCenter>OKLD TRNID</RateCenter>
+ <Latitude>37.780000</Latitude>
+ <Longitude>-122.380000</Longitude>
+ <Region>CA</Region>
+ <PostalCode>94703</PostalCode>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ <AvailablePhoneNumber>
+ <FriendlyName>(510) 488-4379</FriendlyName>
+ <PhoneNumber>+15104884379</PhoneNumber>
+ <Lata>722</Lata>
+ <RateCenter>OKLD FRTVL</RateCenter>
+ <Latitude>37.780000</Latitude>
+ <Longitude>-122.380000</Longitude>
+ <Region>CA</Region>
+ <PostalCode>94602</PostalCode>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ </AvailablePhoneNumbers>
+</TwilioResponse>
@@ -0,0 +1,15 @@
+<TwilioResponse>
+ <AvailablePhoneNumbers uri="/2010-04-01/Accounts/ACde6f1e11047ebd6fe7a55f120be3a900/AvailablePhoneNumbers/US/Local?Contains=510555****">
+ <AvailablePhoneNumber>
+ <FriendlyName>(510) 555-1214</FriendlyName>
+ <PhoneNumber>+15105551214</PhoneNumber>
+ <Lata>722</Lata>
+ <RateCenter>OKLD0349T</RateCenter>
+ <Latitude>37.806940</Latitude>
+ <Longitude>-122.270360</Longitude>
+ <Region>CA</Region>
+ <PostalCode>94612</PostalCode>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ </AvailablePhoneNumbers>
+</TwilioResponse>
@@ -0,0 +1,14 @@
+<TwilioResponse>
+ <AvailablePhoneNumbers uri="/2010-04-01/Accounts/ACde6f1e11047ebd6fe7a55f120be3a900/AvailablePhoneNumbers/US/TollFree">
+ <AvailablePhoneNumber>
+ <FriendlyName>(866) 583-8815</FriendlyName>
+ <PhoneNumber>+18665838815</PhoneNumber>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ <AvailablePhoneNumber>
+ <FriendlyName>(866) 583-0795</FriendlyName>
+ <PhoneNumber>+18665830795</PhoneNumber>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ </AvailablePhoneNumbers>
+</TwilioResponse>
@@ -0,0 +1,10 @@
+<TwilioResponse>
+ <AvailablePhoneNumbers uri="/2010-04-01/Accounts/ACde6f1e11047ebd6fe7a55f120be3a900/AvailablePhoneNumbers/US/TollFree?Contains=STORM">
+ <AvailablePhoneNumber>
+ <FriendlyName>(866) 557-8676</FriendlyName>
+ <PhoneNumber>+18665578676</PhoneNumber>
+ <IsoCountry>US</IsoCountry>
+ </AvailablePhoneNumber>
+ </AvailablePhoneNumbers>
+</TwilioResponse>
+
@@ -0,0 +1,43 @@
+require 'test_helper'
+
+class AvailablePhoneNumbersTest < Test::Unit::TestCase #:nodoc: all
+ context "Available Number Searching" do
+ setup do
+ Twilio.connect('mysid', 'mytoken')
+ end
+
+ context "Local Numbers" do
+ should "be searchable" do
+ assert_equal stub_response(:get, :available_phone_numbers_local, :resource => 'AvailablePhoneNumbers/US/Local'),
+ Twilio::AvailablePhoneNumbers.search_local
+ end
+
+ should "be searchable by area code" do
+ assert_equal stub_response(:get, :available_phone_numbers_local_search, :resource => 'AvailablePhoneNumbers/US/Local?AreaCode=510'),
+ Twilio::AvailablePhoneNumbers.search_local(:area_code => 510)
+ end
+
+ should "be searchable by Postal Code" do
+ assert_equal stub_response(:get, :available_phone_numbers_local_search, :resource => 'AvailablePhoneNumbers/US/Local?InPostalCode=94612'),
+ Twilio::AvailablePhoneNumbers.search_local(:postal_code => 94612)
+ end
+
+ should "be searchable by the rest of the paramters" do
+ assert_equal stub_response(:get, :available_phone_numbers_local_search, :resource => 'AvailablePhoneNumbers/US/Local?NearLatLong=37.806940%2C-122.270360&InRateCenter=OKLD0349T&NearNumber=15105551213&Distance=50&InRegion=CA&InLata=722&Contains=510555****'),
+ Twilio::AvailablePhoneNumbers.search_local(:in_region => 'CA', :contains => '510555****', :near_lat_long => '37.806940,-122.270360', :near_number => '15105551213', :in_lata => 722, :in_rate_center => 'OKLD0349T', :distance => 50)
+ end
+ end
+
+ context "Toll-free numbers" do
+ should "be searchable" do
+ assert_equal stub_response(:get, :available_phone_numbers_toll_free, :resource => 'AvailablePhoneNumbers/US/TollFree'),
+ Twilio::AvailablePhoneNumbers.search_toll_free
+ end
+
+ should "be able to find vanity numbers" do
+ assert_equal stub_response(:get, :available_phone_numbers_toll_free_search, :resource => 'AvailablePhoneNumbers/US/TollFree?Contains=STORM'),
+ Twilio::AvailablePhoneNumbers.search_toll_free(:contains => 'STORM')
+ end
+ end
+ end
+end

0 comments on commit 83a2976

Please sign in to comment.