Class: Twilio::REST::ClientBase

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/base/client_base.rb

Direct Known Subclasses

Client

Constant Summary collapse

@@default_region =

rubocop:disable Style/ClassVars

'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) ⇒ ClientBase

rubocop:disable Metrics/ParameterLists



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twilio-ruby/base/client_base.rb', line 12

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 || []
end

Instance Attribute Details

#account_sidObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def 
  @account_sid
end

#auth_tokenObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def auth_token
  @auth_token
end

#edgeObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def edge
  @edge
end

#http_clientObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def http_client
  @http_client
end

#loggerObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def logger
  @logger
end

#passwordObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def password
  @password
end

#regionObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def region
  @region
end

#user_agent_extensionsObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def user_agent_extensions
  @user_agent_extensions
end

#usernameObject

rubocop:enable Style/ClassVars



8
9
10
# File 'lib/twilio-ruby/base/client_base.rb', line 8

def username
  @username
end

Instance Method Details

#build_uri(uri) ⇒ Object

Build the final request uri



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/twilio-ruby/base/client_base.rb', line 73

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

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

  case pieces.length
  when 4
    new_region ||= pieces[1]
  when 5
    new_edge ||= pieces[1]
    new_region ||= pieces[2]
  end

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

  parsed_url.host = [product, new_edge, new_region, domain].reject(&:nil?).join('.')
  parsed_url.to_s
end

#generate_headers(method, headers) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/twilio-ruby/base/client_base.rb', line 106

def generate_headers(method, headers)
  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}" }

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

  headers['Accept'] = 'application/json' unless headers['Accept']
  headers
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



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
68
69
# File 'lib/twilio-ruby/base/client_base.rb', line 30

def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) # rubocop:disable Metrics/MethodLength
  auth ||= @auth
  headers = generate_headers(method, headers)
  uri = build_uri(uri)

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

    headers.each do |key, value|
      @logger.debug("#{key}:#{value}") unless key.downcase == 'authorization'
    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

#validate_ssl_certificateObject

Validate the new SSL certificate for the Twilio API



99
100
101
102
103
104
# File 'lib/twilio-ruby/base/client_base.rb', line 99

def validate_ssl_certificate
  response = request('api.twilio.com', '8443', 'GET', 'https://api.twilio.com:8443/.json')
  return unless response.status_code < 200 || response.status_code >= 300

  raise RestError.new 'Unexpected response from certificate endpoint', response
end