class TwilioLookups::REST::ListResource
Public Class Methods
# File lib/twilio-lookups/rest/list_resource.rb 6 def initialize(path, client) 7 custom_names = { 8 'Activities' => 'Activity', 9 'Addresses' => 'Address', 10 'Countries' => 'Country', 11 'Feedback' => 'FeedbackInstance', 12 'IpAddresses' => 'IpAddress', 13 'Media' => 'MediaInstance', 14 } 15 @path, @client = path, client 16 resource_name = self.class.name.split('::')[-1] 17 instance_name = custom_names.fetch(resource_name, resource_name.chop) 18 19 # The next line grabs the enclosing module. Necessary for resources 20 # contained in their own submodule like /SMS/Messages 21 parent_module = self.class.to_s.split('::')[-2] 22 full_module_path = if parent_module == "REST" 23 TwilioLookups::REST 24 else 25 TwilioLookups::REST.const_get parent_module 26 end 27 28 @instance_class = full_module_path.const_get instance_name 29 @list_key, @instance_id_key = detwilify(resource_name), 'sid' 30 end
Public Instance Methods
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
# File lib/twilio-lookups/rest/list_resource.rb 92 def create(params={}) 93 raise "Can't create a resource without a REST Client" unless @client 94 response = @client.post @path, params 95 @instance_class.new "#{@path}/#{response[@instance_id_key]}", @client, 96 response 97 end
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.
# File lib/twilio-lookups/rest/list_resource.rb 82 def get(sid) 83 @instance_class.new "#{@path}/#{sid}", @client 84 end
Grab a list of this kind of resource and return it as an array. The array includes two special methods previous_page
and next_page
which will return the previous or next page or resources. By default Twilio will only return 50 resources, and the maximum number of resources you can request (using the :page_size param) is 1000.
The optional params
hash allows you to filter the list returned. The filters for each list resource type are defined by Twilio.
# File lib/twilio-lookups/rest/list_resource.rb 45 def list(params={}, full_path=false) 46 raise "Can't get a resource list without a REST Client" unless @client 47 response = @client.get @path, params, full_path 48 resources = response[@list_key] 49 path = full_path ? @path.split('.')[0] : @path 50 resource_list = resources.map do |resource| 51 @instance_class.new "#{path}/#{resource[@instance_id_key]}", @client, 52 resource 53 end 54 # set the +previous_page+ and +next_page+ properties on the array 55 client, list_class = @client, self.class 56 resource_list.instance_eval do 57 eigenclass = class << self; self; end 58 eigenclass.send :define_method, :previous_page, &lambda { 59 if response['previous_page_uri'] 60 list_class.new(response['previous_page_uri'], client).list({}, true) 61 else 62 [] 63 end 64 } 65 eigenclass.send :define_method, :next_page, &lambda { 66 if response['next_page_uri'] 67 list_class.new(response['next_page_uri'], client).list({}, true) 68 else 69 [] 70 end 71 } 72 end 73 resource_list 74 end