Class: Twilio::REST::BaseClient

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

Constant Summary collapse

HTTP_HEADERS =
{
    'Accept' => 'application/json',
    'Accept-Charset' => 'utf-8',
    'User-Agent' => "twilio-ruby/#{Twilio::VERSION}" \
                  " (#{RUBY_ENGINE}/#{RUBY_PLATFORM}" \
                  " #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#detwilify, #twilify

Methods included from Util

#get_string, #url_encode

Constructor Details

#initialize(*args) ⇒ BaseClient

Returns a new instance of BaseClient.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/twilio-ruby/rest/base_client.rb', line 24

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:host] ||= self.class.host
  @config = Twilio::Util::ClientConfig.new options

  @account_sid = args[0] || Twilio.
  @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

Instance Attribute Details

#account_sidObject (readonly)

Returns the value of attribute account_sid.



22
23
24
# File 'lib/twilio-ruby/rest/base_client.rb', line 22

def 
  @account_sid
end

#last_requestObject (readonly)

Returns the value of attribute last_request.



22
23
24
# File 'lib/twilio-ruby/rest/base_client.rb', line 22

def last_request
  @last_request
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



22
23
24
# File 'lib/twilio-ruby/rest/base_client.rb', line 22

def last_response
  @last_response
end

Class Method Details

.host(host = nil) ⇒ Object

Override the default host for a REST Client (api.twilio.com)



17
18
19
20
# File 'lib/twilio-ruby/rest/base_client.rb', line 17

def self.host(host=nil)
  return @host unless host
  @host = host
end

Instance Method Details

#methodObject

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twilio-ruby/rest/base_client.rb', line 44

[: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?
    # build the full path unless already given
    path = build_full_path(path, params, method) unless args[1]
    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