class TwilioLookups::REST::InstanceResource

A class to wrap an instance resource (like a call or application) within the Twilio API. All other instance resource classes within this library inherit from this class. You shouldn't need to instantiate this class directly. But reviewing the available methods is informative since they are rarely overridden in the inheriting class.

Public Class Methods

new(path, client, params = {}) click to toggle source

Instantiate a new instance resource object. You must pass the path of the instance (e.g. /2010-04-01/Accounts/AC123/Calls/CA456) as well as a client object that responds to get post and delete. This client is meant to be an instance of Twilio::REST::Client but could just as well be a mock object if you want to test the interface. The optional params hash will be converted into attributes on the instantiated object.

   # File lib/twilio-lookups/rest/instance_resource.rb
20 def initialize(path, client, params = {})
21   @path, @client = path, client
22   set_up_properties_from params
23 end

Public Instance Methods

delete() click to toggle source

Delete an instance resource from Twilio. This operation isn't always supported. For instance, you can't delete an SMS. Calling this method makes an HTTP DELETE request to @path.

   # File lib/twilio-lookups/rest/instance_resource.rb
59 def delete
60   raise "Can't delete a resource without a REST Client" unless @client
61   @client.delete @path
62 end
method_missing(method, *args) click to toggle source

Lazily load attributes of the instance resource by waiting to fetch it until an attempt is made to access an unknown attribute.

Calls superclass method
   # File lib/twilio-lookups/rest/instance_resource.rb
67 def method_missing(method, *args)
68   super if @updated
69   set_up_properties_from(@client.get(@path))
70   self.send method, *args
71 end
refresh() click to toggle source

Refresh the attributes of this instance resource object by fetching it from Twilio. Calling this makes an HTTP GET request to @path.

   # File lib/twilio-lookups/rest/instance_resource.rb
48 def refresh
49   raise "Can't refresh a resource without a REST Client" unless @client
50   @updated = false
51   set_up_properties_from(@client.get(@path))
52   self
53 end
update(params = {}) click to toggle source

Update the properties of this instance resource using the key/value pairs in params. This makes an HTTP POST request to @path to handle the update. For example, to update the VoiceUrl of a Twilio Application you could write:

@app.update voice_url: 'http://my.other.app.com/handle_voice'

After returning, the object will contain the most recent state of the instance resource, including the newly updated properties.

   # File lib/twilio-lookups/rest/instance_resource.rb
39 def update(params = {})
40   raise "Can't update a resource without a REST Client" unless @client
41   set_up_properties_from(@client.post(@path, params))
42   self
43 end

Protected Instance Methods

set_up_properties_from(hash) click to toggle source
   # File lib/twilio-lookups/rest/instance_resource.rb
75 def set_up_properties_from(hash)
76   eigenclass = class << self; self; end
77   hash.each do |p,v|
78     property = detwilify p
79     unless ['client', 'updated'].include? property
80       eigenclass.send :define_method, property.to_sym, &lambda { v }
81     end
82   end
83   @updated = !hash.keys.empty?
84 end