Skip to content

Commit 0c05c05

Browse files
author
childish-sambino
authored
Add a global configuration for the HTTP client (#467)
1 parent 063dbda commit 0c05c05

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

lib/twilio-ruby.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
module Twilio
4646
extend SingleForwardable
4747

48-
def_delegators :configuration, :account_sid, :auth_token
48+
def_delegators :configuration, :account_sid, :auth_token, :http_client
4949

5050
##
5151
# Pre-configure with account SID and auth token so that you don't need to

lib/twilio-ruby/rest/client.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class Client
1515

1616
##
1717
# Initializes the Twilio Client
18-
def initialize(username=nil, password=nil, account_sid=nil, region=nil, http_client=Twilio::HTTP::Client.new)
18+
def initialize(username=nil, password=nil, account_sid=nil, region=nil, http_client=nil)
1919
@username = username || Twilio.account_sid
2020
@password = password || Twilio.auth_token
2121
@region = region
2222
@account_sid = account_sid || @username
2323
@auth_token = @password
2424
@auth = [@username, @password]
25-
@http_client = http_client
25+
@http_client = http_client || Twilio.http_client || Twilio::HTTP::Client.new
2626

2727
# Domains
2828
@accounts = nil

lib/twilio-ruby/util/configuration.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
module Twilio
44
module Util
55
class Configuration
6-
attr_accessor :account_sid, :auth_token
7-
def account_sid=value
8-
@account_sid=value
6+
attr_accessor :account_sid, :auth_token, :http_client
7+
8+
def account_sid=(value)
9+
@account_sid = value
10+
end
11+
12+
def auth_token=(value)
13+
@auth_token = value
914
end
1015

11-
def auth_token=value
12-
@auth_token=value
16+
def http_client=(value)
17+
@http_client = value
1318
end
1419
end
1520
end

spec/rest/client_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@
4949
end
5050

5151
describe Twilio::REST::Client do
52+
before do
53+
Twilio.configure do |config|
54+
config.account_sid = 'someSid'
55+
config.auth_token = 'someToken'
56+
config.http_client = 'someClient'
57+
end
58+
end
59+
60+
it 'uses the global configuration by default' do
61+
@client = Twilio::REST::Client.new
62+
expect(@client.account_sid).to eq('someSid')
63+
expect(@client.auth_token).to eq('someToken')
64+
expect(@client.http_client).to eq('someClient')
65+
end
66+
67+
it 'uses the arguments over global configuration' do
68+
@client = Twilio::REST::Client.new('myUser', 'myPassword', nil, nil, 'myClient')
69+
expect(@client.account_sid).to eq('myUser')
70+
expect(@client.auth_token).to eq('myPassword')
71+
expect(@client.http_client).to eq('myClient')
72+
end
73+
5274
it 'successfully validates the working SSL certificate' do
5375
@holodeck.mock Twilio::Response.new(200, '')
5476
expect { @client.validate_ssl_certificate }.not_to raise_error

spec/twilio_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
Twilio.instance_variable_set('@configuration', nil)
44
end
55

6-
it 'should set the account sid and auth token with a config block' do
6+
it 'should set the configuration with a config block' do
77
Twilio.configure do |config|
88
config.account_sid = 'someSid'
99
config.auth_token = 'someToken'
10+
config.http_client = 'someClient'
1011
end
1112

1213
expect(Twilio.account_sid).to eq('someSid')
1314
expect(Twilio.auth_token).to eq('someToken')
15+
expect(Twilio.http_client).to eq('someClient')
1416
end
1517
end

spec/util/configuration_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
config.auth_token = 'someToken'
1313
expect(config.auth_token).to eq('someToken')
1414
end
15+
16+
it 'should have an http client attribute' do
17+
config = Twilio::Util::Configuration.new
18+
config.http_client = 'someClient'
19+
expect(config.http_client).to eq('someClient')
20+
end
1521
end

0 commit comments

Comments
 (0)