Class: Sendly::NumbersResource
- Inherits:
-
Object
- Object
- Sendly::NumbersResource
- Defined in:
- lib/sendly/numbers_resource.rb
Overview
Numbers resource — search, list, and purchase phone numbers.
Instance Method Summary collapse
-
#buy(phone_number:, country_code:, phone_number_type:, monthly_cost:, action_code: nil) ⇒ NumberPurchase
Buy a number.
-
#get(id) ⇒ PhoneNumber
Get a single number owned by the account.
-
#initialize(client) ⇒ NumbersResource
constructor
A new instance of NumbersResource.
-
#list ⇒ Hash
List the numbers owned by the account.
-
#list_available(country:, type:, contains: nil) ⇒ Hash
Search for numbers available to purchase in a country.
-
#list_countries ⇒ Hash
List the countries in which numbers can be searched and purchased, along with the number types available in each.
-
#release(id) ⇒ NumberRelease
Release a number you own.
-
#update(id, is_default: nil, pending_cancellation: nil) ⇒ PhoneNumber
Update a number you own.
Constructor Details
#initialize(client) ⇒ NumbersResource
Returns a new instance of NumbersResource.
230 231 232 |
# File 'lib/sendly/numbers_resource.rb', line 230 def initialize(client) @client = client end |
Instance Method Details
#buy(phone_number:, country_code:, phone_number_type:, monthly_cost:, action_code: nil) ⇒ NumberPurchase
Buy a number.
Returns a Sendly::NumberPurchase. When its status is documents_required or
payment_required, hand the user purchase.action_url +
purchase.action_code (the short display code), wait for that hosted
step to complete, then re-call buy with the SAME arguments plus
action_code: set to purchase.action_identifier (the 32-hex action
identifier) — NOT the display code.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/sendly/numbers_resource.rb', line 287 def buy(phone_number:, country_code:, phone_number_type:, monthly_cost:, action_code: nil) raise ValidationError, "phone_number is required" if phone_number.nil? || phone_number.to_s.empty? raise ValidationError, "country_code is required" if country_code.nil? || country_code.to_s.empty? raise ValidationError, "phone_number_type is required" if phone_number_type.nil? || phone_number_type.to_s.empty? raise ValidationError, "monthly_cost is required" if monthly_cost.nil? || monthly_cost.to_s.empty? body = { phoneNumber: phone_number, countryCode: country_code, phoneNumberType: phone_number_type, monthlyCost: monthly_cost } body[:actionCode] = action_code if action_code response = @client.post("/numbers/buy", body) NumberPurchase.new(response) end |
#get(id) ⇒ PhoneNumber
Get a single number owned by the account.
Unlike the entries #list returns, this includes is_default — whether
the number is the workspace's default sender.
313 314 315 316 317 318 319 |
# File 'lib/sendly/numbers_resource.rb', line 313 def get(id) raise ValidationError, "id is required" if id.nil? || id.to_s.empty? encoded_id = URI.encode_www_form_component(id) response = @client.get("/numbers/#{encoded_id}") PhoneNumber.new(response) end |
#list ⇒ Hash
List the numbers owned by the account.
265 266 267 268 269 |
# File 'lib/sendly/numbers_resource.rb', line 265 def list response = @client.get("/numbers") numbers = (response["numbers"] || []).map { |n| PhoneNumber.new(n) } { numbers: numbers } end |
#list_available(country:, type:, contains: nil) ⇒ Hash
Search for numbers available to purchase in a country.
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/sendly/numbers_resource.rb', line 250 def list_available(country:, type:, contains: nil) raise ValidationError, "country is required" if country.nil? || country.to_s.empty? raise ValidationError, "type is required" if type.nil? || type.to_s.empty? params = { country: country, type: type } params[:contains] = contains if contains response = @client.get("/numbers/available", params) numbers = (response["numbers"] || []).map { |n| AvailableNumber.new(n) } { numbers: numbers } end |
#list_countries ⇒ Hash
List the countries in which numbers can be searched and purchased, along with the number types available in each.
238 239 240 241 242 |
# File 'lib/sendly/numbers_resource.rb', line 238 def list_countries response = @client.get("/numbers/countries") countries = (response["countries"] || []).map { |c| NumberCountry.new(c) } { countries: countries } end |
#release(id) ⇒ NumberRelease
Release a number you own. A live paid purchase is cancelled at the end of
the paid period (the result then reports scheduled? true with a
scheduled_release_at); everything else is released immediately.
359 360 361 362 363 364 365 |
# File 'lib/sendly/numbers_resource.rb', line 359 def release(id) raise ValidationError, "id is required" if id.nil? || id.to_s.empty? encoded_id = URI.encode_www_form_component(id) response = @client.delete("/numbers/#{encoded_id}") NumberRelease.new(response) end |
#update(id, is_default: nil, pending_cancellation: nil) ⇒ PhoneNumber
Update a number you own. Supply at least one supported mutation — these are the only two the API supports:
is_default: true— make this the workspace's default sending number (the number must beactive).pending_cancellation: false— cancel a previously scheduled release and keep the number.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/sendly/numbers_resource.rb', line 336 def update(id, is_default: nil, pending_cancellation: nil) raise ValidationError, "id is required" if id.nil? || id.to_s.empty? if is_default.nil? && pending_cancellation.nil? raise ValidationError, "Provide is_default: true and/or pending_cancellation: false" end body = {} body[:isDefault] = is_default unless is_default.nil? body[:pendingCancellation] = pending_cancellation unless pending_cancellation.nil? encoded_id = URI.encode_www_form_component(id) response = @client.patch("/numbers/#{encoded_id}", body) PhoneNumber.new(response) end |