-
Notifications
You must be signed in to change notification settings - Fork 6
Search for and buy DIDWW phone numbers with Ruby
- DIDWW Account API key (version 2021-04-19)
- Command line with Ruby installed
Sign into your DIDWW account at https://my.didww.com and access the “API” section of the customer portal. If you do not have an account with DIDWW, please register for free. Click on “Send API security key to email” and you will receive your private API key.
Install the DIDWW API client gem from https://github.com/didww/didww-v3-ruby.
$ gem install didww-v3
Fetching didww-v3-2.0.0.gem
Successfully installed didww-v3-2.0.0
1 gem installed
After installing the gem you can start coding in irb
.
Open up the irb and require the didww-v3 gem. You can do this in one step with the following line:
$irb -r didww
Create the client:
client = DIDWW::Client.configure do |config|
config.api_key = '********'
config.api_mode = :production
end
To do this you need to select a DID Group by country (UK) and city name (London) as well as by DID Group Type (local). You also need to select a Stock keeping unit (sku_id) which indicates DID number price and one of its available capacity options (usually DID price + 0 channels/2 channels).
This is how this functionality looks in DIDWW user panel:
#fetch the needed filter values for a DID Group
local_did_type = client.did_group_types.where(name: 'Local').first
country = client.countries.where(iso: 'GB').first
city = client.cities.where(name: 'London', 'country.id': country.id).first
#fetch the UK London local DID Group, keep in mind that there can be multiple groups available, in this case also select needed group by number prefix
did_group = client.did_groups.where(is_available: true, 'country.id': country.id, 'city.id': city.id, 'did_group_type.id': local_did_type.id).includes(:stock_keeping_units).first
#select a group Stock keeping unit with 2 channels included
sku_with_channels = did_group.stock_keeping_units.detect{|sku| sku.channels_included_count > 0 }
order = client.orders.new
order.items << DIDWW::ComplexObject::DidOrderItem.new(qty: 2, sku_id: sku_with_channels.id) #buy 2 numbers
order.save
To check purchased numbers you can filter DID numbers by order ID.
puts client.dids.where('order.id': order.id).to_a.map(&:number)
# => ["44203769****", "44203769****"]
As the Available DIDs feature is not set up by default for new DIDWW users, you firstly have to contact our team at sales@didww.com to enable phone number selection from DIDWW inventory.
#same DID group instance from previous example is initiated here
available_dids = client.available_dids.where('did_group.id': did_group.id).to_a
Each instance in available_dids
array includes ID and number.
pp available_dids
[#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"a6fd070c-7433-4e14-9b93-ed3c84158b66", "number"=>"442081571792"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"742bee61-163a-4003-8198-54c21c375012", "number"=>"442038077245"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"8994bc55-cfc8-4389-87c4-beec8ca8e2ee", "number"=>"442081571765"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"2d255413-b00e-4d54-98ed-2ee00b5d9a23", "number"=>"442038078548"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"ae17e98d-a5d7-47db-a39b-8a5c90c60eaa", "number"=>"442081571429"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"a3d80a0e-b764-4069-ae50-3d7fe10799ad", "number"=>"442081571885"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"a9808a8d-9835-4885-8949-22e75dea3437", "number"=>"442081576285"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"7fe23379-7228-464e-a878-3c8a7edbee93", "number"=>"442081576352"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"33d5f6df-4115-4116-a2d8-fda740a69399", "number"=>"442081576411"}>,
#<DIDWW::Resource::AvailableDid:@attributes={"type"=>"available_dids", "id"=>"07e83cb5-7340-4cab-a044-6c71e94671eb", "number"=>"442081576439"}>]
You can now pick the first two available_dids objects, for instance, and order them as follows:
order = client.orders.new
order.items << DIDWW::ComplexObject::DidOrderItem.new(available_did_id: available_dids.first.id, sku_id: sku_with_channels.id)
order.items << DIDWW::ComplexObject::DidOrderItem.new(available_did_id: available_dids.second.id, sku_id: sku_with_channels.id)
order.save
The Available DIDs feature also allows users to reserve selected numbers for 10 minutes providing the opportunity to check with the end-users which number they would like to order. Please note, that the time and reserved inventory size parameters can be adjusted upon request.
This is how this functionality looks in DIDWW user panel:
You can reserve and order your desirable numbers as follows:
available_did = client.available_dids.where('did_group.id': did_group.id).first
reservation = client.did_reservations.new(description: "reservation of #{available_did.number}")
reservation.relationships[:available_did] = DIDWW::Resource::AvailableDid.load(id: available_did.id)
reservation.save
Each reservation has its own expire_at
timestamp
2.7.3 :110 > reservation.expire_at
=> 2021-11-29 13:49:47.803 UTC
You can complete order as follows until reservation expiration
order = client.orders.new
order.items << DIDWW::ComplexObject::DidOrderItem.new(did_reservation_id: reservation.id, sku_id: sku_with_channels.id)
order.save
That’s all you have to know to buy a phone number at DIDWW. Take a look at the full DIDWW API functionality implemented on an open source DEMO project https://github.com/didww/didww-v3-rails-sample.
Shoot me at @Fedoronchuk, if you have any questions.