Class: Restcomm::REST::Client

Inherits:
BaseClient show all
Defined in:
lib/restcomm-ruby/rest/client.rb

Overview

The Restcomm::REST::Client class caches authentication parameters and exposes methods to make HTTP requests to Restcomm’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 Restcomm::REST::Call.

Instantiate a client like so:

@client = Restcomm::REST::Client.new , auth_token

There are a few options you can use to configure the way your client will communicate with Restcomm. 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 Restcomm: account and accounts.

@client.account

Most of the time you’ll want to start with the account attribute. This object is an instance of Restcomm::REST::Account that wraps the account referenced by the account_sid you used when instantiating the client.

An instance of Restcomm::REST::Account exposes objects wrapping all of the account-level Restcomm resources as properties. So

@client..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 Restcomm::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 Restcomm::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 =
'2012-04-24'

Constants inherited from BaseClient

BaseClient::DEFAULTS, BaseClient::HTTP_HEADERS

Instance Attribute Summary collapse

Attributes inherited from BaseClient

#account_sid, #last_request, #last_response

Instance Method Summary collapse

Methods included from Utils

#derestify, #restify

Methods included from Util

#get_string, #url_encode

Constructor Details

#initialize(*args) ⇒ Client

Instantiate a new HTTP client to talk to Restcomm. 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.restcomm.com'

The domain to which you’d like the client to make HTTP requests. Useful for testing. Defaults to ‘api.restcomm.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 Restcomm’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.



261
262
263
# File 'lib/restcomm-ruby/rest/client.rb', line 261

def initialize(*args)
  super(*args)
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.



328
329
330
331
332
333
334
# File 'lib/restcomm-ruby/rest/client.rb', line 328

def method_missing(method_name, *args, &block)
  if .respond_to?(method_name)
    .send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



193
194
195
# File 'lib/restcomm-ruby/rest/client.rb', line 193

def 
  @account
end

#accountsObject (readonly)

Returns the value of attribute accounts.



193
194
195
# File 'lib/restcomm-ruby/rest/client.rb', line 193

def accounts
  @accounts
end

Instance Method Details

#inspectObject

:nodoc:



265
266
267
# File 'lib/restcomm-ruby/rest/client.rb', line 265

def inspect # :nodoc:
  "<Restcomm::REST::Client @account_sid=#{@account_sid}>"
end

#methodObject

Define #get, #put, #post and #delete helper methods for sending HTTP requests to Restcomm. 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.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/restcomm-ruby/rest/client.rb', line 274

[:get, :put, :post, :delete].each do |method|
  method_class = Net::HTTP.const_get method.to_s.capitalize
  define_method method do |path, *args|
    params = restify args[0]; params = {} if params.empty?
    unless args[1] # build the full path unless already given
   path = "/restcomm#{path}.json"

	if method == :get

		if  path.include?("/Accounts/#{@account_sid}.json" )
				path.gsub!("/Accounts/#{@account_sid}.json", "/Accounts.json/#{@account_sid}")
	 	end
		

		if   path.match(/Calls\/CA.*\/Recordings.json/ )		
			path.gsub!("/Recordings.json", ".json")
	 	end

		if  path.include?("/Accounts/#{@account_sid}/AvailablePhoneNumbers/US/Local.json" )
				path.gsub!("/AvailablePhoneNumbers/US/Local.json", "/AvailablePhoneNumbers/US/Local")
	 	end

		if  path.include?("/Accounts/#{@account_sid}.json" )
				path.gsub!("/Accounts/#{@account_sid}.json", "/Accounts.json/#{@account_sid}")
	 	end
	end


#### Make Restcomm path compatible with Twilio -  convert Restcomm 2012-04-24/Accounts/ACae6e420f425248d6a26948c17a9e2acf/Calls/CA76d50db1b78d4a34bfaa708bc669d7db.json to /2012-04-24/Accounts/ACae6e420f425248d6a26948c17a9e2acf/Calls.json/CA76d50db1b78d4a34bfaa708bc669d7db 

	if method == :post

		if   path.match(/Calls\/CA.*/ )
			call_sid = path.split("/").last	
			call_sid = call_sid.gsub!(".json", "")	
			path.gsub!("Calls/#{call_sid}.json", "Calls.json/#{call_sid}")
			
			
	 	end
	end	

	 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

Returns:

  • (Boolean)


336
337
338
339
340
341
342
# File 'lib/restcomm-ruby/rest/client.rb', line 336

def respond_to?(method_name, include_private=false)
  if .respond_to?(method_name, include_private)
    true
  else
    super
  end
end