Class: Plivo::Resources::NumberInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/numbers.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

expected_type?, expected_value?, raise_invalid_request, valid_account?, valid_mainaccount?, valid_param?, valid_signature?, valid_subaccount?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ NumberInterface

Returns a new instance of NumberInterface.



184
185
186
187
188
189
# File 'lib/plivo/resources/numbers.rb', line 184

def initialize(client, resource_list_json = nil)
  @_name = 'Number'
  @_resource_type = Number
  @_identifier_string = 'number'
  super
end

Instance Method Details

#add_number(numbers, carrier, region, options = nil) ⇒ Object

Parameters:

  • numbers (Array)

    An array of numbers that need to be added for the carrier. Make sure that you configure the numbers to point to the sip server @sbc.plivo.com. Eg: If the number you own from your carrier is 18554675486 then the sip address it needs to point to is [email protected]

  • carrier (String)

    The carrier_id of the IncomingCarrier that the number is associated with. For more information, check our IncomingCarrier API /

  • region (String)

    This is the region that is associated with the Number. You can use it to organize numbers based on the area they are from.

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :number_type (String)

    This field does not impact the way Plivo uses this number. It is primarily adding more information about your number. You may use this field to categorize between local and tollfree numbers. Default is local.

  • :app_id (String)

    The application id of the application that is to be linked.

  • :subaccount (String)

    The auth_id of the subaccount to which this number should be added. This can only be performed by a main account holder.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/plivo/resources/numbers.rb', line 269

def add_number(numbers, carrier, region, options = nil)
  valid_param?(:carrier, carrier, [String, Symbol], true)
  valid_param?(:region, region, [String, Symbol], true)
  valid_param?(:numbers, numbers, Array, true)
  numbers.each do |number|
    valid_param?(:number, number, [Integer, String, Symbol], true)
  end

  params = {
    numbers: numbers.join(','),
    carrier: carrier,
    region: region
  }

  return perform_post(params) if options.nil?

  if options.key?(:subaccount) &&
     valid_subaccount?(options[:subaccount], true)
    params[:subaccount] = options[:subaccount]
  end

  %i[number_type app_id].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  perform_post(params)
end

#delete(number) ⇒ Object

Parameters:

  • number (String)


312
313
314
315
# File 'lib/plivo/resources/numbers.rb', line 312

def delete(number)
  valid_param?(:number, number, [String, Symbol], true)
  Number.new(@_client, resource_id: number).delete
end

#eachObject



252
253
254
255
256
257
258
259
260
# File 'lib/plivo/resources/numbers.rb', line 252

def each
  offset = 0
  loop do
    number_list = list(offset: offset)
    number_list[:objects].each { |number| yield number }
    offset += 20
    return unless number_list.length == 20
  end
end

#get(number) ⇒ Object

Parameters:

  • number (String)


192
193
194
# File 'lib/plivo/resources/numbers.rb', line 192

def get(number)
  perform_get(number)
end

#list(options = nil) ⇒ Object

Parameters:

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :type (String)

    The type of number you are filtering. You can filter by local and tollfree numbers. Defaults to a local number.

  • :number_startswith (String|Int)

    Used to specify the beginning of the number. For example, if the number ‘24’ is specified, the API will fetch only those numbers beginning with ‘24’.

  • :subaccount (String)

    Requires the auth_id of the subaccount as input. If this parameter is included in the request, all numbers of the particular subaccount are displayed.

  • :alias (String)

    This is a name given to the number. The API will fetch only those numbers with the alias specified.

  • :services (String)

    Filters out phone numbers according to the services you want from that number. The following values are valid:

    • voice - Returns a list of numbers that provide ‘voice’ services. Additionally, if the numbers offer both ‘voice’ and ‘sms’, they are also listed. Note - This option does not exclusively list those services that provide both voice and sms .

    • voice,sms - Returns a list of numbers that provide both ‘voice’ and ‘sms’ services.

    • sms - Returns a list of numbers that provide only ‘sms’ services.

  • :limit (Int)

    Used to display the number of results per page. The maximum number of results that can be fetched is 20.

  • :offset (Int)

    Denotes the number of value items by which the results should be offset. Eg:- If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/plivo/resources/numbers.rb', line 207

def list(options = nil)
  return perform_list if options.nil?

  valid_param?(:options, options, Hash, true)

  params = {}

  %i[number_startswith subaccount alias].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  if options.key?(:services) &&
     valid_param?(:services, options[:services], [String, Symbol],
                  true, %w[sms voice voice,sms])
    params[:services] = options[:services]
  end

  if options.key?(:type) &&
     valid_param?(:type, options[:type], [String, Symbol],
                  true, %w[local tollfree])
    params[:type] = options[:type]
  end

  %i[offset limit].each do |param|
    if options.key?(param) && valid_param?(param, options[param],
                                           [Integer, Integer], true)
      params[param] = options[param]
    end
  end

  if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
    raise_invalid_request('The maximum number of results that can be '\
    "fetched is 20. limit can't be more than 20 or less than 1")
  end

  if options.key?(:offset) && options[:offset] < 0
    raise_invalid_request("Offset can't be negative")
  end

  perform_list(params)
end

#update(number, options = nil) ⇒ Object

Parameters:

  • number (String)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :alias (String)

    The textual name given to the number.

  • :app_id (String)

    The application id of the application that is to be linked.

  • :subaccount (String)

    The auth_id of the subaccount to which this number should be added. This can only be performed by a main account holder.



305
306
307
308
309
# File 'lib/plivo/resources/numbers.rb', line 305

def update(number, options = nil)
  valid_param?(:number, number, [String, Symbol], true)
  Number.new(@_client,
             resource_id: number).update(options)
end