Class: Twilio::REST::Client
- Inherits:
-
Object
- Object
- Twilio::REST::Client
- Defined in:
- lib/twilio-ruby/rest/client.rb
Overview
The Twilio::REST::Client class caches authentication parameters and exposes methods to make HTTP requests to Twilio’s REST API. However, you should never really need to call these methods yourself since you can work with the more pleasant wrapper objects like Twilio::REST::Call.
Instantiate a client like so:
@client = Twilio::REST::Client.new account_sid, auth_token
There are a few options you can use to configure the way your client will communicate with Twilio. See #new for a list and descriptions.
Once you have a client object you can use it to do fun things. Every client object exposes two wrapper objects which you can use as entry points into Twilio: account and accounts.
@client.account
Most of the time you’ll want to start with the account attribute. This object is an instance of Twilio::REST::Account that wraps the account referenced by the account_sid you used when instantiating the client.
An instance of Twilio::REST::Account exposes objects wrapping all of the account-level Twilio resources as properties. So
@client.account.calls
For convenience, the resources of the default account are also available on the client object. So the following call is equivalent to the example above
@client.calls
represents an account’s call list.
@client.accounts
If you are doing anything related to subaccounts you’ll want to start here. This object is an instance of Twilio::REST::Accounts that wraps the list of accounts belonging to the master account referenced by the account_sid used to instantiate the client.
This class inherits from Twilio::REST::ListResource, so you can use methods like ListResource#list to return a (possibly filtered) list of accounts and ListResource#create to create a new account. Use ListResource#get to grab a particular account once you know its sid.
Constant Summary collapse
- API_VERSION =
'2010-04-01'- HTTP_HEADERS =
{ 'Accept' => 'application/json', 'Accept-Charset' => 'utf-8', 'User-Agent' => "twilio-ruby/#{Twilio::VERSION}" \ " (#{RUBY_ENGINE}/#{RUBY_PLATFORM}" \ " #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})" }
- DEFAULTS =
{ host: 'api.twilio.com', port: 443, use_ssl: true, ssl_verify_peer: true, ssl_ca_file: File.dirname(__FILE__) + '/../../../conf/cacert.pem', timeout: 30, proxy_addr: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil, retry_limit: 1 }
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#account_sid ⇒ Object
readonly
Returns the value of attribute account_sid.
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Client
constructor
Instantiate a new HTTP client to talk to Twilio.
-
#inspect ⇒ Object
:nodoc:.
-
#method ⇒ Object
Define #get, #put, #post and #delete helper methods for sending HTTP requests to Twilio.
-
#method_missing(method_name, *args, &block) ⇒ Object
Delegate account methods from the client.
- #respond_to?(method_name, include_private = false) ⇒ Boolean
Methods included from Utils
Methods included from Util
Constructor Details
#initialize(*args) ⇒ Client
Instantiate a new HTTP client to talk to Twilio. The parameters account_sid and auth_token are required, unless you have configured them already using the block configure syntax, and used to generate the HTTP basic auth header in each request. The options parameter is a hash of connection configuration options. the following keys are supported:
host: 'api.twilio.com'
The domain to which you’d like the client to make HTTP requests. Useful for testing. Defaults to ‘api.twilio.com’.
port: 443
The port on which to connect to the above domain. Defaults to 443 and should be left that way except in testing environments.
use_ssl: true
Declare whether ssl should be used for connections to the above domain. Defaults to true and should be left alone except when testing.
ssl_verify_peer: true
Declare whether to verify the host’s ssl cert when setting up the connection to the above domain. Defaults to true, but can be turned off to avoid ssl certificate verification failures in environments without the necessary ca certificates.
ssl_ca_file: '/path/to/ca/file'
Specify the path to the certificate authority bundle you’d like to use to verify Twilio’s SSL certificate on each request. If not specified, a certificate bundle extraced from Firefox is packaged with the gem and used by default.
timeout: 30
Set the time in seconds to wait before timing out the HTTP request. Defaults to 30 seconds. If you aren’t fetching giant pages of call or SMS logs you can safely decrease this to something like 3 seconds or lower. In paricular if you are sending SMS you can set this to 1 second or less and swallow the exception if you don’t care about the response.
proxy_addr: 'proxy.host.domain'
The domain of a proxy through which you’d like the client to make HTTP requests. Defaults to nil.
proxy_port: 3128
The port on which to connect to the above proxy. Defaults to nil.
proxy_user: 'username'
The user name to use for authentication with the proxy. Defaults to nil.
proxy_pass: 'password'
The password to use for authentication with the proxy. Defaults to nil.
retry_limit: 1
The number of times to retry a request that has failed before throwing an exception. Defaults to one.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/twilio-ruby/rest/client.rb', line 147 def initialize(*args) = args.last.is_a?(Hash) ? args.pop : {} @config = DEFAULTS.merge! @account_sid = args[0] || Twilio.account_sid @auth_token = args[1] || Twilio.auth_token if @account_sid.nil? || @auth_token.nil? raise ArgumentError, 'Account SID and auth token are required' end set_up_connection set_up_subresources end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Delegate account methods from the client. This saves having to call client.account every time for resources on the default account.
189 190 191 192 193 194 195 |
# File 'lib/twilio-ruby/rest/client.rb', line 189 def method_missing(method_name, *args, &block) if account.respond_to?(method_name) account.send(method_name, *args, &block) else super end end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
78 79 80 |
# File 'lib/twilio-ruby/rest/client.rb', line 78 def account @account end |
#account_sid ⇒ Object (readonly)
Returns the value of attribute account_sid.
78 79 80 |
# File 'lib/twilio-ruby/rest/client.rb', line 78 def account_sid @account_sid end |
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
78 79 80 |
# File 'lib/twilio-ruby/rest/client.rb', line 78 def accounts @accounts end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
78 79 80 |
# File 'lib/twilio-ruby/rest/client.rb', line 78 def last_request @last_request end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
78 79 80 |
# File 'lib/twilio-ruby/rest/client.rb', line 78 def last_response @last_response end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
161 162 163 |
# File 'lib/twilio-ruby/rest/client.rb', line 161 def inspect # :nodoc: "<Twilio::REST::Client @account_sid=#{@account_sid}>" end |
#method ⇒ Object
Define #get, #put, #post and #delete helper methods for sending HTTP requests to Twilio. You shouldn’t need to use these methods directly, but they can be useful for debugging. Each method returns a hash obtained from parsing the JSON object in the response body.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/twilio-ruby/rest/client.rb', line 170 [:get, :put, :post, :delete].each do |method| method_class = Net::HTTP.const_get method.to_s.capitalize define_method method do |path, *args| params = twilify args[0]; params = {} if params.empty? unless args[1] # build the full path unless already given path = "#{path}.json" path << "?#{url_encode(params)}" if method == :get && !params.empty? end request = method_class.new path, HTTP_HEADERS request.basic_auth @account_sid, @auth_token request.form_data = params if [:post, :put].include? method connect_and_send request end end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
197 198 199 200 201 202 203 |
# File 'lib/twilio-ruby/rest/client.rb', line 197 def respond_to?(method_name, include_private=false) if account.respond_to?(method_name, include_private) true else super end end |