twilio-ruby 5.34.1 → 5.35.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f3afee506f82f8d295b1b7bd408c090df9f2984bb639c1165559f837282cd0e
4
- data.tar.gz: 3f3f1b4854f5df815c42617e0b089397925cb27f8207f22b35d0cf49011bdbdf
3
+ metadata.gz: 3a3e996f0e5e03d78e9be169b6254e444bac36f39c70a6c8ff1a0a682b672866
4
+ data.tar.gz: 3ccf875613f2bcce4316fa3e35ccdfa25bb5eefb0aeb1f424a388f5cf9e9ae23
5
5
  SHA512:
6
- metadata.gz: 715032b7a617559d21cdacaaaa769567f873ccbdc432ac40b5e3d5761c4f32c9c18d51512a3221fe35527821b0945774a0770a43653fd7815b4a7011e3f76798
7
- data.tar.gz: 6b305639f5b8ca5fa69e97d185221e13d9b1969d95b3d89b7d112b046e82dbab4c88a8cb7d633e0eefa0bde94d6aec9a424ea2cb3cc5897368c022d35c1d25f8
6
+ metadata.gz: 98f01d3f1f616b93ae64d25bdd6de991dc85c6c5b7a774b059aa60d07b3fe28650b05e0a73e234dbff7f16c4d405af298c7547596c7e19eda03bc618a875ffcd
7
+ data.tar.gz: 6a4ed10718140c2c528f0b053f2f34167d39d40283fc226b59bb7f0563137e953019ad0fd84d27f731f1994f64a8b1c2b0c3391aeed4b096cec6d1b7b252a68d
data/CHANGES.md CHANGED
@@ -1,6 +1,12 @@
1
1
  twilio-ruby changelog
2
2
  =====================
3
3
 
4
+ [2020-05-20] Version 5.35.0
5
+ ---------------------------
6
+ **Library - Feature**
7
+ - [PR #512](https://github.com/twilio/twilio-ruby/pull/512): add regional and edge support. Thanks to [@eshanholtz](https://github.com/eshanholtz)!
8
+
9
+
4
10
  [2020-05-13] Version 5.34.1
5
11
  ---------------------------
6
12
  **Api**
data/README.md CHANGED
@@ -32,13 +32,13 @@ This library supports the following Ruby implementations:
32
32
  To install using [Bundler][bundler] grab the latest stable version:
33
33
 
34
34
  ```ruby
35
- gem 'twilio-ruby', '~> 5.34.1'
35
+ gem 'twilio-ruby', '~> 5.35.0'
36
36
  ```
37
37
 
38
38
  To manually install `twilio-ruby` via [Rubygems][rubygems] simply gem install:
39
39
 
40
40
  ```bash
41
- gem install twilio-ruby -v 5.34.1
41
+ gem install twilio-ruby -v 5.35.0
42
42
  ```
43
43
 
44
44
  To build and install the development branch yourself from the latest source:
@@ -64,6 +64,21 @@ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
64
64
  @client = Twilio::REST::Client.new account_sid, auth_token
65
65
  ```
66
66
 
67
+ ### Specify a Region and/or Edge
68
+
69
+ ```ruby
70
+ # set up a client to talk to the Twilio REST API over a specific region and edge
71
+ @client = Twilio::REST::Client.new account_sid, auth_token, nil, 'au1'
72
+ @client.edge = 'sydney'
73
+
74
+ # you may also specify the region and/or edge after client creation
75
+ @client = Twilio::REST::Client.new account_sid, auth_token
76
+ @client.region = 'au1'
77
+ @client.edge = 'sydney'
78
+ ```
79
+
80
+ This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
81
+
67
82
  ### Make a Call
68
83
 
69
84
  ```ruby
@@ -38,7 +38,7 @@ module Twilio
38
38
  autoload :JWT, File.join(File.dirname(__FILE__), 'twilio-ruby', 'jwt', 'jwt.rb')
39
39
  autoload :TwiML, File.join(File.dirname(__FILE__), 'twilio-ruby', 'twiml', 'twiml.rb')
40
40
 
41
- def_delegators :configuration, :account_sid, :auth_token, :http_client
41
+ def_delegators :configuration, :account_sid, :auth_token, :http_client, :region, :edge
42
42
 
43
43
  ##
44
44
  # Pre-configure with account SID and auth token so that you don't need to
@@ -11,14 +11,17 @@ module Twilio
11
11
  ##
12
12
  # A client for accessing the Twilio API.
13
13
  class Client
14
- attr_accessor :http_client, :username, :password, :account_sid, :auth_token, :region
14
+ @@default_region = 'us1'
15
+
16
+ attr_accessor :http_client, :username, :password, :account_sid, :auth_token, :region, :edge
15
17
 
16
18
  ##
17
19
  # Initializes the Twilio Client
18
20
  def initialize(username=nil, password=nil, account_sid=nil, region=nil, http_client=nil)
19
21
  @username = username || Twilio.account_sid
20
22
  @password = password || Twilio.auth_token
21
- @region = region
23
+ @region = region || Twilio.region
24
+ @edge = Twilio.edge
22
25
  @account_sid = account_sid || @username
23
26
  @auth_token = @password
24
27
  @auth = [@username, @password]
@@ -75,12 +78,7 @@ module Twilio
75
78
  headers['Accept'] = 'application/json'
76
79
  end
77
80
 
78
- if !region.nil?
79
- head, tail = uri.split('.', 2)
80
- if !tail.start_with?(region)
81
- uri = [head, region, tail].join('.')
82
- end
83
- end
81
+ uri = build_uri(uri)
84
82
 
85
83
  @http_client.request(
86
84
  host,
@@ -95,6 +93,35 @@ module Twilio
95
93
  )
96
94
  end
97
95
 
96
+ ##
97
+ # Build the final request uri
98
+ def build_uri(uri)
99
+ if @region.nil? and @edge.nil?
100
+ return uri
101
+ end
102
+
103
+ parsed_url = URI(uri)
104
+ pieces = parsed_url.host.split('.')
105
+ product = pieces[0]
106
+ domain = pieces[-2, 2]
107
+ new_edge = @edge
108
+ new_region = @region
109
+
110
+ if pieces.length == 4
111
+ new_region ||= pieces[1]
112
+ elsif pieces.length == 5
113
+ new_edge ||= pieces[1]
114
+ new_region ||= pieces[2]
115
+ end
116
+
117
+ if !new_edge.nil? && new_region.nil?
118
+ new_region = @@default_region
119
+ end
120
+
121
+ parsed_url.host = [product, new_edge, new_region, domain].select {|item| !item.nil?}.join('.')
122
+ parsed_url.to_s
123
+ end
124
+
98
125
  ##
99
126
  # Validate the new SSL certificate for the Twilio API
100
127
  def validate_ssl_certificate
@@ -3,7 +3,7 @@
3
3
  module Twilio
4
4
  module Util
5
5
  class Configuration
6
- attr_accessor :account_sid, :auth_token, :http_client
6
+ attr_accessor :account_sid, :auth_token, :http_client, :region, :edge
7
7
 
8
8
  def account_sid=(value)
9
9
  @account_sid = value
@@ -16,6 +16,14 @@ module Twilio
16
16
  def http_client=(value)
17
17
  @http_client = value
18
18
  end
19
+
20
+ def region=(value)
21
+ @region = value
22
+ end
23
+
24
+ def edge=(value)
25
+ @edge = value
26
+ end
19
27
  end
20
28
  end
21
29
  end
@@ -1,3 +1,3 @@
1
1
  module Twilio
2
- VERSION = '5.34.1'
2
+ VERSION = '5.35.0'
3
3
  end
@@ -49,75 +49,185 @@ describe Twilio::REST::TrunkingClient do
49
49
  end
50
50
 
51
51
  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'
52
+ context 'configuration' do
53
+ before do
54
+ Twilio.configure do |config|
55
+ config.account_sid = 'someSid'
56
+ config.auth_token = 'someToken'
57
+ config.http_client = 'someClient'
58
+ config.region = 'someRegion'
59
+ config.edge = 'someEdge'
60
+ end
57
61
  end
58
- end
59
62
 
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
63
+ it 'uses the global configuration by default' do
64
+ @client = Twilio::REST::Client.new
65
+ expect(@client.account_sid).to eq('someSid')
66
+ expect(@client.auth_token).to eq('someToken')
67
+ expect(@client.http_client).to eq('someClient')
68
+ expect(@client.region).to eq('someRegion')
69
+ expect(@client.edge).to eq('someEdge')
70
+ end
66
71
 
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
72
+ it 'uses the arguments over global configuration' do
73
+ @client = Twilio::REST::Client.new('myUser', 'myPassword', nil, 'myRegion', 'myClient')
74
+ @client.edge = 'myEdge'
75
+ expect(@client.account_sid).to eq('myUser')
76
+ expect(@client.auth_token).to eq('myPassword')
77
+ expect(@client.http_client).to eq('myClient')
78
+ expect(@client.region).to eq('myRegion')
79
+ expect(@client.edge).to eq('myEdge')
80
+ end
73
81
 
74
- it 'successfully validates the working SSL certificate' do
75
- @holodeck.mock Twilio::Response.new(200, '')
76
- expect { @client.validate_ssl_certificate }.not_to raise_error
77
- end
82
+ class MyVersion < Twilio::REST::Version
83
+ def initialize(domain)
84
+ super
85
+ @version = 'v1'
86
+ end
87
+ end
78
88
 
79
- it 'fails to validate broken SSL certificates' do
80
- @holodeck.mock Twilio::Response.new(504, '')
81
- expect { @client.validate_ssl_certificate }.to raise_error(Twilio::REST::RestError)
89
+ class MyDomain < Twilio::REST::Domain
90
+ def initialize(client)
91
+ super
92
+ @host = 'twilio.com'
93
+ @base_url = 'https://twilio.com'
94
+ @port = 443
95
+ end
96
+ end
82
97
  end
83
98
 
84
- it 'translates bad request error params' do
85
- @domain = MyDomain.new(@client)
86
- @version = MyVersion.new(@domain)
87
- @error_message = '{
88
- "code": 20001,
89
- "message": "Bad request",
90
- "more_info": "https://www.twilio.com/docs/errors/20001",
91
- "status": 400,
92
- "details": {
93
- "foo":"bar"
94
- }}'
95
- @holodeck.mock Twilio::Response.new(400, @error_message)
96
- expect {
97
- @version.fetch('GET', 'http://foobar.com')
98
- }.to raise_error { |error|
99
- expect(error).to be_a(Twilio::REST::RestError)
100
- expect(error.status_code).to eq(400)
101
- expect(error.code).to eq(20_001)
102
- expect(error.details).to eq({ 'foo' => 'bar' })
103
- expect(error.error_message).to eq('Bad request')
104
- expect(error.more_info).to eq('https://www.twilio.com/docs/errors/20001')
105
- }
106
- end
99
+ context 'validation' do
100
+ before do
101
+ Twilio.configure do |config|
102
+ config.account_sid = 'someSid'
103
+ config.auth_token = 'someToken'
104
+ config.http_client = 'someClient'
105
+ config.region = nil
106
+ config.edge = nil
107
+ end
108
+ end
107
109
 
108
- class MyVersion < Twilio::REST::Version
109
- def initialize(domain)
110
- super
111
- @version = 'v1'
110
+ it 'successfully validates the working SSL certificate' do
111
+ @holodeck.mock Twilio::Response.new(200, '')
112
+ expect { @client.validate_ssl_certificate }.not_to raise_error
113
+ end
114
+
115
+ it 'fails to validate broken SSL certificates' do
116
+ @holodeck.mock Twilio::Response.new(504, '')
117
+ expect { @client.validate_ssl_certificate }.to raise_error(Twilio::REST::RestError)
118
+ end
119
+
120
+ it 'translates bad request error params' do
121
+ @domain = MyDomain.new(@client)
122
+ @version = MyVersion.new(@domain)
123
+ @error_message = '{
124
+ "code": 20001,
125
+ "message": "Bad request",
126
+ "more_info": "https://www.twilio.com/docs/errors/20001",
127
+ "status": 400,
128
+ "details": {
129
+ "foo":"bar"
130
+ }}'
131
+ @holodeck.mock Twilio::Response.new(400, @error_message)
132
+ expect {
133
+ @version.fetch('GET', 'http://foobar.com')
134
+ }.to raise_error { |error|
135
+ expect(error).to be_a(Twilio::REST::RestError)
136
+ expect(error.status_code).to eq(400)
137
+ expect(error.code).to eq(20_001)
138
+ expect(error.details).to eq({ 'foo' => 'bar' })
139
+ expect(error.error_message).to eq('Bad request')
140
+ expect(error.more_info).to eq('https://www.twilio.com/docs/errors/20001')
141
+ }
112
142
  end
113
143
  end
114
144
 
115
- class MyDomain < Twilio::REST::Domain
116
- def initialize(client)
117
- super
118
- @host = 'twilio.com'
119
- @base_url = 'https://twilio.com'
120
- @port = 443
145
+ describe '#build_uri' do
146
+ before(:all) do
147
+ Twilio.configure do |config|
148
+ config.account_sid = 'username'
149
+ config.auth_token = 'password'
150
+ config.region = nil
151
+ config.edge = nil
152
+ end
153
+ end
154
+
155
+ context 'no region or edge in url' do
156
+ it "doesn't set region or edge" do
157
+ @client = Twilio::REST::Client.new
158
+ expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.twilio.com')
159
+ end
160
+
161
+ it 'uses the default region if edge set' do
162
+ @client = Twilio::REST::Client.new
163
+ @client.edge = 'edge'
164
+ expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.edge.us1.twilio.com')
165
+ end
166
+
167
+ it 'sets region' do
168
+ @client = Twilio::REST::Client.new
169
+ @client.region = 'region'
170
+ expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.region.twilio.com')
171
+ end
172
+
173
+ it 'sets region and edge' do
174
+ @client = Twilio::REST::Client.new
175
+ @client.region = 'region'
176
+ @client.edge = 'edge'
177
+ expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.edge.region.twilio.com')
178
+ end
179
+ end
180
+
181
+ context 'region in url' do
182
+ it 'uses url region' do
183
+ @client = Twilio::REST::Client.new
184
+ expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.urlRegion.twilio.com')
185
+ end
186
+
187
+ it 'uses client edge and url region' do
188
+ @client = Twilio::REST::Client.new
189
+ @client.edge = 'edge'
190
+ expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.edge.urlRegion.twilio.com')
191
+ end
192
+
193
+ it 'prefers client region' do
194
+ @client = Twilio::REST::Client.new
195
+ @client.region = 'region'
196
+ expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.region.twilio.com')
197
+ end
198
+
199
+ it 'uses client edge and prefers client region' do
200
+ @client = Twilio::REST::Client.new
201
+ @client.region = 'region'
202
+ @client.edge = 'edge'
203
+ expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.edge.region.twilio.com')
204
+ end
205
+ end
206
+
207
+ context 'region and edge in url' do
208
+ it 'uses url region and edge' do
209
+ @client = Twilio::REST::Client.new
210
+ expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.urlEdge.urlRegion.twilio.com')
211
+ end
212
+
213
+ it 'prefers client edge' do
214
+ @client = Twilio::REST::Client.new
215
+ @client.edge = 'edge'
216
+ expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.edge.urlRegion.twilio.com')
217
+ end
218
+
219
+ it 'prefers client region' do
220
+ @client = Twilio::REST::Client.new
221
+ @client.region = 'region'
222
+ expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.urlEdge.region.twilio.com')
223
+ end
224
+
225
+ it 'prefers client region and edge' do
226
+ @client = Twilio::REST::Client.new
227
+ @client.region = 'region'
228
+ @client.edge = 'edge'
229
+ expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.edge.region.twilio.com')
230
+ end
121
231
  end
122
232
  end
123
233
  end
@@ -18,4 +18,16 @@ describe Twilio::Util::Configuration do
18
18
  config.http_client = 'someClient'
19
19
  expect(config.http_client).to eq('someClient')
20
20
  end
21
+
22
+ it 'should have a region attribute' do
23
+ config = Twilio::Util::Configuration.new
24
+ config.region = 'someRegion'
25
+ expect(config.region).to eq('someRegion')
26
+ end
27
+
28
+ it 'should have an edge attribute' do
29
+ config = Twilio::Util::Configuration.new
30
+ config.edge = 'someEdge'
31
+ expect(config.edge).to eq('someEdge')
32
+ end
21
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twilio-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.34.1
4
+ version: 5.35.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Twilio API Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-13 00:00:00.000000000 Z
11
+ date: 2020-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -1072,8 +1072,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1072
1072
  - !ruby/object:Gem::Version
1073
1073
  version: '0'
1074
1074
  requirements: []
1075
- rubyforge_project:
1076
- rubygems_version: 2.7.10
1075
+ rubygems_version: 3.0.8
1077
1076
  signing_key:
1078
1077
  specification_version: 4
1079
1078
  summary: The official library for communicating with the Twilio REST API, building