Class: Twilio::REST::ListResource
- Inherits:
-
Object
- Object
- Twilio::REST::ListResource
- Includes:
- Utils
- Defined in:
- lib/twilio-ruby/rest/list_resource.rb
Direct Known Subclasses
Accounts, Applications, AuthorizedConnectApps, AvailablePhoneNumbers, Calls, Conferences, ConnectApps, CredentialListMappings, CredentialLists, Credentials, Domains, IncomingPhoneNumbers, IpAccessControlListMappings, IpAccessControlLists, IpAddresses, Local, Media, Members, Messages, Mobile, Notifications, NumberType::Local, NumberType::Mobile, NumberType::TollFree, OutgoingCallerIds, Participants, Queues, Recordings, Records, SMS::Messages, SMS::ShortCodes, TollFree, Transcriptions, Triggers
Instance Method Summary collapse
-
#create(params = {}) ⇒ Object
Return a newly created resource.
-
#get(sid) ⇒ Object
(also: #find)
Return an empty instance resource object with the proper path.
-
#initialize(path, client) ⇒ ListResource
constructor
A new instance of ListResource.
-
#inspect ⇒ Object
:nodoc:.
-
#list(params = {}, full_path = false) ⇒ Object
Grab a list of this kind of resource and return it as an array.
- #resource(*resources) ⇒ Object
-
#total ⇒ Object
Ask Twilio for the total number of items in the list.
Methods included from Utils
Constructor Details
#initialize(path, client) ⇒ ListResource
Returns a new instance of ListResource.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 7 def initialize(path, client) custom_names = {"Media" => "MediaInstance", "IpAddresses" => "IpAddress"} @path, @client = path, client resource_name = self.class.name.split('::')[-1] instance_name = custom_names.fetch(resource_name, resource_name.chop) # The next line grabs the enclosing module. Necessary for resources # contained in their own submodule like /SMS/Messages parent_module = self.class.to_s.split('::')[-2] full_module_path = parent_module == "REST" ? (Twilio::REST) : (Twilio::REST.const_get parent_module) @instance_class = full_module_path.const_get instance_name @list_key, @instance_id_key = detwilify(resource_name), 'sid' end |
Instance Method Details
#create(params = {}) ⇒ Object
Return a newly created resource. Some params may be required. Consult the Twilio 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
89 90 91 92 93 94 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 89 def create(params={}) raise "Can't create a resource without a REST Client" unless @client response = @client.post @path, params @instance_class.new "#{@path}/#{response[@instance_id_key]}", @client, response end |
#get(sid) ⇒ Object Also known as: find
Return an empty instance resource object with the proper path. Note that this will never raise a Twilio::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.
79 80 81 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 79 def get(sid) @instance_class.new "#{@path}/#{sid}", @client end |
#inspect ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 22 def inspect # :nodoc: "<#{self.class} @path=#{@path}>" end |
#list(params = {}, full_path = false) ⇒ Object
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 Twilio’s server. This may differ from the size and length attributes of the returned array since by default Twilio 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 Twilio.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 36 def list(params={}, full_path=false) raise "Can't get a resource list without a REST Client" unless @client response = @client.get @path, params, full_path resources = response[@list_key] path = full_path ? @path.split('.')[0] : @path resource_list = resources.map do |resource| @instance_class.new "#{path}/#{resource[@instance_id_key]}", @client, resource end # set the +total+ and +next_page+ properties on the array client, list_class = @client, self.class resource_list.instance_eval do eigenclass = class << self; self; end eigenclass.send :define_method, :total, &lambda {response['total']} eigenclass.send :define_method, :next_page, &lambda { if response['next_page_uri'] list_class.new(response['next_page_uri'], client).list({}, true) else [] end } end resource_list end |
#resource(*resources) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 96 def resource(*resources) custom_resource_names = {:sms => 'SMS', :sip => 'SIP'} resources.each do |r| resource = twilify r relative_path = custom_resource_names.fetch(r, resource) path = "#{@path}/#{relative_path}" enclosing_module = @submodule == nil ? (Twilio::REST) : (Twilio::REST.const_get(@submodule)) resource_class = enclosing_module.const_get resource instance_variable_set("@#{r}", resource_class.new(path, @client)) end self.class.instance_eval {attr_reader *resources} end |
#total ⇒ Object
Ask Twilio 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.
68 69 70 71 |
# File 'lib/twilio-ruby/rest/list_resource.rb', line 68 def total raise "Can't get a resource total without a REST Client" unless @client @client.get(@path, :page_size => 1)['total'] end |