class Restcomm::REST::ListResource
Public Class Methods
# File lib/restcomm-ruby/rest/list_resource.rb 8 def initialize(path, client) 9 custom_names = { 10 'Activities' => 'Activity', 11 'Media' => 'MediaInstance', 12 'IpAddresses' => 'IpAddress', 13 'Feedback' => 'FeedbackInstance', 14 'Addresses' => 'Address' 15 } 16 @path, @client = path, client 17 18 resource_name = self.class.name.split('::')[-1] 19 20 instance_name = custom_names.fetch(resource_name, resource_name.chop) 21 22 23 # The next line grabs the enclosing module. Necessary for resources 24 # contained in their own submodule like /SMS/Messages 25 parent_module = self.class.to_s.split('::')[-2] 26 full_module_path = if parent_module == "REST" 27 Restcomm::REST 28 else 29 Restcomm::REST.const_get(parent_module) 30 end 31 32 @instance_class = full_module_path.const_get(instance_name) 33 34 @list_key, @instance_id_key = derestify(resource_name), 'sid' 35 36 end
Public Instance Methods
Return a newly created resource. Some params
may be required. Consult the Restcomm
REST
API documentation related to the kind of resource you are attempting to create for details. Calling this method makes an HTTP POST request to @path
with the given params
# File lib/restcomm-ruby/rest/list_resource.rb 130 def create(params={}) 131 raise "Can't create a resource without a REST Client" unless @client 132 response = @client.post @path, params 133 @instance_class.new "#{@path}/#{response[@instance_id_key]}", @client, 134 135 response 136 end
Return an empty instance resource object with the proper path. Note that this will never raise a Restcomm::REST::RequestError
on 404 since no HTTP request is made. The HTTP request is made when attempting to access an attribute of the returned instance resource object, such as its date_created or voice_url attributes.
# File lib/restcomm-ruby/rest/list_resource.rb 120 def get(sid) 121 @instance_class.new "#{@path}/#{sid}", @client 122 end
Grab a list of this kind of resource and return it as an array. The array includes a special attribute named total
which will return the total number of items in the list on Restcomm’s server. This may differ from the size
and length
attributes of the returned array since by default Restcomm
will only return 50 resources, and the maximum number of resources you can request is 1000.
The optional params
hash allows you to filter the list returned. The filters for each list resource type are defined by Restcomm
.
# File lib/restcomm-ruby/rest/list_resource.rb 52 def list(params={}, full_path=false) 53 raise "Can't get a resource list without a REST Client" unless @client 54 55 56 response = @client.get( @path, params, full_path) 57 58 if @list_key == "calls" 59 resources = response[@list_key] 60 elsif @list_key == "recordings" 61 resources = response 62 63 else 64 resources = response 65 end 66 67 #raise "********#{@instance_class}*******#{resources[0]["FriendlyName"]}***************" 68 path = full_path ? @path.split('.')[0] : @path 69 70 if @list_key == "available_phone_numbers" 71 resources = response["RestcommResponse"]["AvailablePhoneNumbers"]["AvailablePhoneNumber"] 72 resource_list = resources.map do |resource| 73 @instance_class.new("#{path}/#{resource[@instance_id_key]}", @client, resource) 74 end 75 else 76 resource_list = resources.map do |resource| 77 @instance_class.new("#{path}/#{resource[@instance_id_key]}", @client, resource) 78 end 79 end 80 81 # set the +total+ and +next_page+ properties on the array 82 client, list_class = @client, self.class 83 resource_list.instance_eval do 84 eigenclass = class << self; self; end 85 eigenclass.send :define_method, :total, &lambda { response['total'] } 86 eigenclass.send :define_method, :next_page, &lambda { 87 if response['next_page_uri'] 88 list_class.new(response['next_page_uri'], client).list({}, true) 89 else 90 [] 91 end 92 } 93 end 94 95 resource_list 96 97 98 end
Ask Restcomm
for the total number of items in the list. Calling this method makes an HTTP GET request to @path
with a page size parameter of 1 to minimize data over the wire while still obtaining the total. Don’t use this if you are planning to call list
anyway, since the array returned from list
will have a total
attribute as well.
# File lib/restcomm-ruby/rest/list_resource.rb 109 def total 110 raise "Can't get a resource total without a REST Client" unless @client 111 @client.get(@path, page_size: 1)['total'] 112 end