Class: Twilio::REST::InstanceResource

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/twilio-ruby/rest/instance_resource.rb

Overview

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.

Instance Method Summary collapse

Methods included from Utils

#detwilify, #twilify

Constructor Details

#initialize(path, client, params = {}) ⇒ InstanceResource

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.



20
21
22
23
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 20

def initialize(path, client, params = {})
  @path, @client = path, client
  set_up_properties_from params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

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



67
68
69
70
71
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 67

def method_missing(method, *args)
  super if @updated
  set_up_properties_from(@client.get(@path))
  self.send method, *args
end

Instance Method Details

#deleteObject

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.



59
60
61
62
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 59

def delete
  raise "Can't delete a resource without a REST Client" unless @client
  @client.delete @path
end

#inspectObject

:nodoc:



25
26
27
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 25

def inspect # :nodoc:
  "<#{self.class} @path=#{@path}>"
end

#refreshObject

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



48
49
50
51
52
53
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 48

def refresh
  raise "Can't refresh a resource without a REST Client" unless @client
  @updated = false
  set_up_properties_from(@client.get(@path))
  self
end

#update(params = {}) ⇒ Object

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.



39
40
41
42
43
# File 'lib/twilio-ruby/rest/instance_resource.rb', line 39

def update(params = {})
  raise "Can't update a resource without a REST Client" unless @client
  set_up_properties_from(@client.post(@path, params))
  self
end