Class: Twilio::REST::Client

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

Overview

A client for accessing the Twilio API.

Constant Summary collapse

@@default_region =
'us1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, account_sid = nil, region = nil, http_client = nil, logger = nil, user_agent_extensions = nil) ⇒ Client

Initializes the Twilio Client



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/twilio-ruby/rest/client.rb', line 20

def initialize(username=nil, password=nil, =nil, region=nil, http_client=nil, logger=nil, user_agent_extensions=nil)
  @username = username || Twilio.
  @password = password || Twilio.auth_token
  @region = region || Twilio.region
  @edge = Twilio.edge
  @account_sid =  || @username
  @auth_token = @password
  @auth = [@username, @password]
  @http_client = http_client || Twilio.http_client || Twilio::HTTP::Client.new
  @logger = logger || Twilio.logger
  @user_agent_extensions = user_agent_extensions || []

  # Domains
  @accounts = nil
  @api = nil
  @autopilot = nil
  @chat = nil
  @conversations = nil
  @events = nil
  @flex_api = nil
  @frontline_api = nil
  @insights = nil
  @ip_messaging = nil
  @lookups = nil
  @media = nil
  @messaging = nil
  @monitor = nil
  @notify = nil
  @numbers = nil
  @oauth = nil
  @preview = nil
  @pricing = nil
  @proxy = nil
  @routes = nil
  @serverless = nil
  @studio = nil
  @sync = nil
  @taskrouter = nil
  @trunking = nil
  @trusthub = nil
  @verify = nil
  @video = nil
  @voice = nil
  @wireless = nil
  @supersim = nil
  @bulkexports = nil
  @microvisor = nil
end

Instance Attribute Details

#account_sidObject

Returns the value of attribute account_sid.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def 
  @account_sid
end

#auth_tokenObject

Returns the value of attribute auth_token.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def auth_token
  @auth_token
end

#edgeObject

Returns the value of attribute edge.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def edge
  @edge
end

#http_clientObject

Returns the value of attribute http_client.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def http_client
  @http_client
end

#loggerObject

Returns the value of attribute logger.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def password
  @password
end

#regionObject

Returns the value of attribute region.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def region
  @region
end

#user_agent_extensionsObject

Returns the value of attribute user_agent_extensions.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def user_agent_extensions
  @user_agent_extensions
end

#usernameObject

Returns the value of attribute username.



16
17
18
# File 'lib/twilio-ruby/rest/client.rb', line 16

def username
  @username
end

Instance Method Details

#accountsObject

Access the Accounts Twilio Domain



169
170
171
# File 'lib/twilio-ruby/rest/client.rb', line 169

def accounts
  @accounts ||= Accounts.new self
end

#addresses(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::AddressInstance, Twilio::REST::Api::V2010::AccountContext::AddressList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Address resource.

Returns:



376
377
378
# File 'lib/twilio-ruby/rest/client.rb', line 376

def addresses(sid=:unset)
  self.api.v2010..addresses(sid)
end

#apiObject

Access the Api Twilio Domain



175
176
177
# File 'lib/twilio-ruby/rest/client.rb', line 175

def api
  @api ||= Api.new self
end

#applications(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::ApplicationInstance, Twilio::REST::Api::V2010::AccountContext::ApplicationList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Application resource.

Returns:



385
386
387
# File 'lib/twilio-ruby/rest/client.rb', line 385

def applications(sid=:unset)
  self.api.v2010..applications(sid)
end

#authorized_connect_apps(connect_app_sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::AuthorizedConnectAppInstance, Twilio::REST::Api::V2010::AccountContext::AuthorizedConnectAppList

Parameters:

  • connect_app_sid (String) (defaults to: :unset)

    The SID that we assigned to the Connect App.

Returns:



393
394
395
# File 'lib/twilio-ruby/rest/client.rb', line 393

def authorized_connect_apps(connect_app_sid=:unset)
  self.api.v2010..authorized_connect_apps(connect_app_sid)
end

#autopilotObject

Access the Autopilot Twilio Domain



181
182
183
# File 'lib/twilio-ruby/rest/client.rb', line 181

def autopilot
  @autopilot ||= Autopilot.new self
end

#available_phone_numbers(country_code = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::AvailablePhoneNumberCountryInstance, Twilio::REST::Api::V2010::AccountContext::AvailablePhoneNumberCountryList

Parameters:

  • country_code (String) (defaults to: :unset)

    The ISO-3166-1 country code of the country.

Returns:



403
404
405
# File 'lib/twilio-ruby/rest/client.rb', line 403

def available_phone_numbers(country_code=:unset)
  self.api.v2010..available_phone_numbers(country_code)
end

#balanceTwilio::REST::Api::V2010::AccountContext::BalanceInstance



409
410
411
# File 'lib/twilio-ruby/rest/client.rb', line 409

def balance
  self.api.v2010..balance
end

#build_uri(uri) ⇒ Object

Build the final request uri



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/twilio-ruby/rest/client.rb', line 131

def build_uri(uri)
  if @region.nil? and @edge.nil?
    return uri
  end

  parsed_url = URI(uri)
  pieces = parsed_url.host.split('.')
  product = pieces[0]
  domain = pieces[-2, 2]
  new_edge = @edge
  new_region = @region

  if pieces.length == 4
    new_region ||= pieces[1]
  elsif pieces.length == 5
    new_edge ||= pieces[1]
    new_region ||= pieces[2]
  end

  if !new_edge.nil? && new_region.nil?
    new_region = @@default_region
  end

  parsed_url.host = [product, new_edge, new_region, domain].select {|item| !item.nil?}.join('.')
  parsed_url.to_s
end

#bulkexportsObject

Access the Bulkexports Twilio Domain



361
362
363
# File 'lib/twilio-ruby/rest/client.rb', line 361

def bulkexports
  @bulkexports ||= Bulkexports.new self
end

#calls(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::CallInstance, Twilio::REST::Api::V2010::AccountContext::CallList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that we created to identify this Call resource.

Returns:



418
419
420
# File 'lib/twilio-ruby/rest/client.rb', line 418

def calls(sid=:unset)
  self.api.v2010..calls(sid)
end

#chatObject

Access the Chat Twilio Domain



187
188
189
# File 'lib/twilio-ruby/rest/client.rb', line 187

def chat
  @chat ||= Chat.new self
end

#conferences(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::ConferenceInstance, Twilio::REST::Api::V2010::AccountContext::ConferenceList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify this Conference resource.

Returns:



427
428
429
# File 'lib/twilio-ruby/rest/client.rb', line 427

def conferences(sid=:unset)
  self.api.v2010..conferences(sid)
end

#connect_apps(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::ConnectAppInstance, Twilio::REST::Api::V2010::AccountContext::ConnectAppList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the ConnectApp resource.

Returns:



436
437
438
# File 'lib/twilio-ruby/rest/client.rb', line 436

def connect_apps(sid=:unset)
  self.api.v2010..connect_apps(sid)
end

#conversationsObject

Access the Conversations Twilio Domain



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

def conversations
  @conversations ||= Conversations.new self
end

#eventsObject

Access the Events Twilio Domain



199
200
201
# File 'lib/twilio-ruby/rest/client.rb', line 199

def events
  @events ||= Events.new self
end

#flex_apiObject

Access the FlexApi Twilio Domain



205
206
207
# File 'lib/twilio-ruby/rest/client.rb', line 205

def flex_api
  @flex_api ||= FlexApi.new self
end

#frontline_apiObject

Access the FrontlineApi Twilio Domain



211
212
213
# File 'lib/twilio-ruby/rest/client.rb', line 211

def frontline_api
  @frontline_api ||= FrontlineApi.new self
end

#incoming_phone_numbers(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::IncomingPhoneNumberInstance, Twilio::REST::Api::V2010::AccountContext::IncomingPhoneNumberList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify this IncomingPhoneNumber resource.

Returns:



445
446
447
# File 'lib/twilio-ruby/rest/client.rb', line 445

def incoming_phone_numbers(sid=:unset)
  self.api.v2010..incoming_phone_numbers(sid)
end

#insightsObject

Access the Insights Twilio Domain



217
218
219
# File 'lib/twilio-ruby/rest/client.rb', line 217

def insights
  @insights ||= Insights.new self
end

#ip_messagingObject

Access the IpMessaging Twilio Domain



223
224
225
# File 'lib/twilio-ruby/rest/client.rb', line 223

def ip_messaging
  @ip_messaging ||= IpMessaging.new self
end

#keys(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::KeyInstance, Twilio::REST::Api::V2010::AccountContext::KeyList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Key resource.

Returns:



454
455
456
# File 'lib/twilio-ruby/rest/client.rb', line 454

def keys(sid=:unset)
  self.api.v2010..keys(sid)
end

#lookupsObject

Access the Lookups Twilio Domain



229
230
231
# File 'lib/twilio-ruby/rest/client.rb', line 229

def lookups
  @lookups ||= Lookups.new self
end

#mediaObject

Access the Media Twilio Domain



235
236
237
# File 'lib/twilio-ruby/rest/client.rb', line 235

def media
  @media ||= Media.new self
end

#messages(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::MessageInstance, Twilio::REST::Api::V2010::AccountContext::MessageList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Message resource.

Returns:



463
464
465
# File 'lib/twilio-ruby/rest/client.rb', line 463

def messages(sid=:unset)
  self.api.v2010..messages(sid)
end

#messagingObject

Access the Messaging Twilio Domain



241
242
243
# File 'lib/twilio-ruby/rest/client.rb', line 241

def messaging
  @messaging ||= Messaging.new self
end

#microvisorObject

Access the Microvisor Twilio Domain



367
368
369
# File 'lib/twilio-ruby/rest/client.rb', line 367

def microvisor
  @microvisor ||= Microvisor.new self
end

#monitorObject

Access the Monitor Twilio Domain



247
248
249
# File 'lib/twilio-ruby/rest/client.rb', line 247

def monitor
  @monitor ||= Monitor.new self
end

#new_keysTwilio::REST::Api::V2010::AccountContext::NewKeyInstance



469
470
471
# File 'lib/twilio-ruby/rest/client.rb', line 469

def new_keys
  self.api.v2010..new_keys
end

#new_signing_keysTwilio::REST::Api::V2010::AccountContext::NewSigningKeyInstance



475
476
477
# File 'lib/twilio-ruby/rest/client.rb', line 475

def new_signing_keys
  self.api.v2010..new_signing_keys
end

#notifications(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::NotificationInstance, Twilio::REST::Api::V2010::AccountContext::NotificationList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Notification resource.

Returns:



484
485
486
# File 'lib/twilio-ruby/rest/client.rb', line 484

def notifications(sid=:unset)
  self.api.v2010..notifications(sid)
end

#notifyObject

Access the Notify Twilio Domain



253
254
255
# File 'lib/twilio-ruby/rest/client.rb', line 253

def notify
  @notify ||= Notify.new self
end

#numbersObject

Access the Numbers Twilio Domain



259
260
261
# File 'lib/twilio-ruby/rest/client.rb', line 259

def numbers
  @numbers ||= Numbers.new self
end

#oauthObject

Access the Oauth Twilio Domain



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

def oauth
  @oauth ||= Oauth.new self
end

#outgoing_caller_ids(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::OutgoingCallerIdInstance, Twilio::REST::Api::V2010::AccountContext::OutgoingCallerIdList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the OutgoingCallerId resource.

Returns:



493
494
495
# File 'lib/twilio-ruby/rest/client.rb', line 493

def outgoing_caller_ids(sid=:unset)
  self.api.v2010..outgoing_caller_ids(sid)
end

#previewObject

Access the Preview Twilio Domain



271
272
273
# File 'lib/twilio-ruby/rest/client.rb', line 271

def preview
  @preview ||= Preview.new self
end

#pricingObject

Access the Pricing Twilio Domain



277
278
279
# File 'lib/twilio-ruby/rest/client.rb', line 277

def pricing
  @pricing ||= Pricing.new self
end

#proxyObject

Access the Proxy Twilio Domain



283
284
285
# File 'lib/twilio-ruby/rest/client.rb', line 283

def proxy
  @proxy ||= Proxy.new self
end

#queues(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::QueueInstance, Twilio::REST::Api::V2010::AccountContext::QueueList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify this Queue resource.

Returns:



502
503
504
# File 'lib/twilio-ruby/rest/client.rb', line 502

def queues(sid=:unset)
  self.api.v2010..queues(sid)
end

#recordings(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::RecordingInstance, Twilio::REST::Api::V2010::AccountContext::RecordingList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Recording resource.

Returns:



511
512
513
# File 'lib/twilio-ruby/rest/client.rb', line 511

def recordings(sid=:unset)
  self.api.v2010..recordings(sid)
end

#request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object

Makes a request to the Twilio API using the configured http client Authentication information is automatically added if none is provided



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/twilio-ruby/rest/client.rb', line 72

def request(host, port, method, uri, params={}, data={}, headers={}, auth=nil, timeout=nil)
  auth ||= @auth

  ruby_config = RbConfig::CONFIG
  headers['User-Agent'] = "twilio-ruby/#{Twilio::VERSION} (#{ruby_config["host_os"]} #{ruby_config["host_cpu"]}) Ruby/#{RUBY_VERSION}"
  headers['Accept-Charset'] = 'utf-8'

  user_agent_extensions.each { |extension| headers['User-Agent'] += " #{extension}" }

  if method == 'POST' && !headers['Content-Type']
    headers['Content-Type'] = 'application/x-www-form-urlencoded'
  end

  unless headers['Accept']
    headers['Accept'] = 'application/json'
  end

  uri = build_uri(uri)

  if @logger
    @logger.debug("--BEGIN Twilio API Request--")
    @logger.debug("Request Method: <#{method}>")

    headers.each do |key, value|
      unless key.downcase == 'authorization'
        @logger.debug("#{key}:#{value}")
      end
    end

    url = URI(uri)
    @logger.debug("Host:#{url.host}")
    @logger.debug("Path:#{url.path}")
    @logger.debug("Query:#{url.query}")
    @logger.debug("Request Params:#{params}")
  end

  response = @http_client.request(
    host,
    port,
    method,
    uri,
    params,
    data,
    headers,
    auth,
    timeout
  )

  if @logger
    @logger.debug("Response Status Code:#{response.status_code}")
    @logger.debug("Response Headers:#{response.headers}")
    @logger.debug("--END TWILIO API REQUEST--")
  end

  response
end

#routesObject

Access the Routes Twilio Domain



289
290
291
# File 'lib/twilio-ruby/rest/client.rb', line 289

def routes
  @routes ||= Routes.new self
end

#serverlessObject

Access the Serverless Twilio Domain



295
296
297
# File 'lib/twilio-ruby/rest/client.rb', line 295

def serverless
  @serverless ||= Serverless.new self
end

#short_codes(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::ShortCodeInstance, Twilio::REST::Api::V2010::AccountContext::ShortCodeList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify this ShortCode resource.

Returns:



534
535
536
# File 'lib/twilio-ruby/rest/client.rb', line 534

def short_codes(sid=:unset)
  self.api.v2010..short_codes(sid)
end

#signing_keys(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::SigningKeyInstance, Twilio::REST::Api::V2010::AccountContext::SigningKeyList

Parameters:

  • sid (String) (defaults to: :unset)

    The sid

Returns:



519
520
521
# File 'lib/twilio-ruby/rest/client.rb', line 519

def signing_keys(sid=:unset)
  self.api.v2010..signing_keys(sid)
end

#sipTwilio::REST::Api::V2010::AccountContext::SipInstance



525
526
527
# File 'lib/twilio-ruby/rest/client.rb', line 525

def sip
  self.api.v2010..sip
end

#studioObject

Access the Studio Twilio Domain



301
302
303
# File 'lib/twilio-ruby/rest/client.rb', line 301

def studio
  @studio ||= Studio.new self
end

#supersimObject

Access the Supersim Twilio Domain



355
356
357
# File 'lib/twilio-ruby/rest/client.rb', line 355

def supersim
  @supersim ||= Supersim.new self
end

#syncObject

Access the Sync Twilio Domain



307
308
309
# File 'lib/twilio-ruby/rest/client.rb', line 307

def sync
  @sync ||= Sync.new self
end

#taskrouterObject

Access the Taskrouter Twilio Domain



313
314
315
# File 'lib/twilio-ruby/rest/client.rb', line 313

def taskrouter
  @taskrouter ||= Taskrouter.new self
end

#to_sObject

Provide a user friendly representation



567
568
569
# File 'lib/twilio-ruby/rest/client.rb', line 567

def to_s
  "#<Twilio::REST::Client #{@account_sid}>"
end

#tokensTwilio::REST::Api::V2010::AccountContext::TokenInstance



540
541
542
# File 'lib/twilio-ruby/rest/client.rb', line 540

def tokens
  self.api.v2010..tokens
end

#transcriptions(sid = :unset) ⇒ Twilio::REST::Api::V2010::AccountContext::TranscriptionInstance, Twilio::REST::Api::V2010::AccountContext::TranscriptionList

Parameters:

  • sid (String) (defaults to: :unset)

    The unique string that that we created to identify the Transcription resource.

Returns:



549
550
551
# File 'lib/twilio-ruby/rest/client.rb', line 549

def transcriptions(sid=:unset)
  self.api.v2010..transcriptions(sid)
end

#trunkingObject

Access the Trunking Twilio Domain



319
320
321
# File 'lib/twilio-ruby/rest/client.rb', line 319

def trunking
  @trunking ||= Trunking.new self
end

#trusthubObject

Access the Trusthub Twilio Domain



325
326
327
# File 'lib/twilio-ruby/rest/client.rb', line 325

def trusthub
  @trusthub ||= Trusthub.new self
end

#usageTwilio::REST::Api::V2010::AccountContext::UsageInstance



555
556
557
# File 'lib/twilio-ruby/rest/client.rb', line 555

def usage
  self.api.v2010..usage
end

#validate_ssl_certificateObject

Validate the new SSL certificate for the Twilio API



160
161
162
163
164
165
# File 'lib/twilio-ruby/rest/client.rb', line 160

def validate_ssl_certificate
  response = request('api.twilio.com', '8443', 'GET', 'https://api.twilio.com:8443/.json')
  if response.status_code < 200 || response.status_code >= 300
    raise RestError.new 'Unexpected response from certificate endpoint', response
  end
end

#validation_requestsTwilio::REST::Api::V2010::AccountContext::ValidationRequestInstance



561
562
563
# File 'lib/twilio-ruby/rest/client.rb', line 561

def validation_requests
  self.api.v2010..validation_requests
end

#verifyObject

Access the Verify Twilio Domain



331
332
333
# File 'lib/twilio-ruby/rest/client.rb', line 331

def verify
  @verify ||= Verify.new self
end

#videoObject

Access the Video Twilio Domain



337
338
339
# File 'lib/twilio-ruby/rest/client.rb', line 337

def video
  @video ||= Video.new self
end

#voiceObject

Access the Voice Twilio Domain



343
344
345
# File 'lib/twilio-ruby/rest/client.rb', line 343

def voice
  @voice ||= Voice.new self
end

#wirelessObject

Access the Wireless Twilio Domain



349
350
351
# File 'lib/twilio-ruby/rest/client.rb', line 349

def wireless
  @wireless ||= Wireless.new self
end