twilio-ruby 5.75.0 → 5.77.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +31 -0
- data/CONTRIBUTING.md +1 -7
- data/README.md +132 -45
- data/advanced-examples/custom-http-client.md +170 -0
- data/lib/twilio-ruby/rest/conversations/v1/conversation.rb +43 -5
- data/lib/twilio-ruby/rest/conversations/v1/service/conversation.rb +43 -5
- data/lib/twilio-ruby/rest/insights/v1/call_summaries.rb +9 -3
- data/lib/twilio-ruby/rest/lookups/v2/phone_number.rb +0 -7
- data/lib/twilio-ruby/rest/messaging/v1/brand_registration/brand_registration_otp.rb +134 -0
- data/lib/twilio-ruby/rest/messaging/v1/brand_registration.rb +26 -3
- data/lib/twilio-ruby/rest/messaging/v1/domain_config.rb +4 -29
- data/lib/twilio-ruby/rest/messaging/v1/tollfree_verification.rb +14 -0
- data/lib/twilio-ruby/rest/numbers/v1/eligibility.rb +111 -0
- data/lib/twilio-ruby/rest/numbers/v1.rb +35 -0
- data/lib/twilio-ruby/rest/numbers.rb +13 -0
- data/lib/twilio-ruby/rest/verify/v2/service.rb +3 -3
- data/lib/twilio-ruby/rest/verify/v2/template.rb +3 -3
- data/lib/twilio-ruby/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a318c2179298abd4418a2ce946ffa89d3d14a1a
|
4
|
+
data.tar.gz: 95898f51182dca1ab44a774ae83108cb494f010c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22146fa2f467d8f02f2100c1358ced77ec7cbe8b0d9078a9748bd5baae30f25e1f47c0b20f4412cec3d95ac07375adfa2846f66036085c4b0ca6905c853fb80d
|
7
|
+
data.tar.gz: fc56f4af9f61277c749d4f6df039493ed6dfdd821f3b3eed01e63201b00501243c22556388128d386924e3ab7c9131f81476cbcba4ac1af773374ced104d37e4
|
data/CHANGES.md
CHANGED
@@ -1,6 +1,37 @@
|
|
1
1
|
twilio-ruby changelog
|
2
2
|
=====================
|
3
3
|
|
4
|
+
[2023-04-19] Version 5.77.0
|
5
|
+
---------------------------
|
6
|
+
**Library - Docs**
|
7
|
+
- [PR #645](https://github.com/twilio/twilio-ruby/pull/645): consolidate. Thanks to [@stern-shawn](https://github.com/stern-shawn)!
|
8
|
+
|
9
|
+
**Messaging**
|
10
|
+
- Remove `messaging_service_sids` and `messaging_service_sid_action` from domain config endpoint **(breaking change)**
|
11
|
+
- Add error_code and rejection_reason properties to tollfree verification API response
|
12
|
+
|
13
|
+
**Numbers**
|
14
|
+
- Added the new Eligibility API under version `/v1`.
|
15
|
+
|
16
|
+
|
17
|
+
[2023-04-05] Version 5.76.0
|
18
|
+
---------------------------
|
19
|
+
**Conversations**
|
20
|
+
- Expose query parameters `start_date`, `end_date` and `state` in list operation on Conversations resource for sorting and filtering
|
21
|
+
|
22
|
+
**Insights**
|
23
|
+
- Added answered by filter in Call Summaries
|
24
|
+
|
25
|
+
**Lookups**
|
26
|
+
- Remove `disposable_phone_number_risk` package **(breaking change)**
|
27
|
+
|
28
|
+
**Messaging**
|
29
|
+
- Add support for `SOLE_PROPRIETOR` brand type and `SOLE_PROPRIETOR` campaign use case.
|
30
|
+
- New Sole Proprietor Brands should be created with `SOLE_PROPRIETOR` brand type. Brand registration requests with `STARTER` brand type will be rejected.
|
31
|
+
- New Sole Proprietor Campaigns should be created with `SOLE_PROPRIETOR` campaign use case. Campaign registration requests with `STARTER` campaign use case will be rejected.
|
32
|
+
- Add Brand Registrations OTP API
|
33
|
+
|
34
|
+
|
4
35
|
[2023-03-22] Version 5.75.0
|
5
36
|
---------------------------
|
6
37
|
**Library - Chore**
|
data/CONTRIBUTING.md
CHANGED
@@ -21,7 +21,7 @@ it can be.
|
|
21
21
|
## <a name="question"></a> Got an API/Product Question or Problem?
|
22
22
|
|
23
23
|
If you have questions about how to use `twilio-ruby`, please see our
|
24
|
-
[docs]
|
24
|
+
[docs](./README.md), and if you don't find the answer there, please contact
|
25
25
|
[help@twilio.com](mailto:help@twilio.com) with any issues you have.
|
26
26
|
|
27
27
|
## <a name="issue"></a> Found an Issue?
|
@@ -68,10 +68,6 @@ you're working on.
|
|
68
68
|
For large fixes, please build and test the documentation before submitting the
|
69
69
|
PR to be sure you haven't accidentally introduced layout or formatting issues.
|
70
70
|
|
71
|
-
If you want to help improve the docs at
|
72
|
-
[https://www.twilio.com/docs/libraries/ruby][docs-link], please contact
|
73
|
-
[help@twilio.com](mailto:help@twilio.com).
|
74
|
-
|
75
71
|
## <a name="submit"></a> Submission Guidelines
|
76
72
|
|
77
73
|
### Submitting an Issue
|
@@ -155,7 +151,5 @@ you are working:
|
|
155
151
|
* All features or bug fixes **must be tested** by one or more tests.
|
156
152
|
* All classes and methods **must be documented**.
|
157
153
|
|
158
|
-
|
159
|
-
[docs-link]: https://www.twilio.com/docs/libraries/ruby
|
160
154
|
[issue-link]: https://github.com/twilio/twilio-ruby/issues/new
|
161
155
|
[github]: https://github.com/twilio/twilio-ruby
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
The documentation for the Twilio API can be found [here][apidocs].
|
10
10
|
|
11
|
-
The
|
11
|
+
The individual releases [here][refdocs].
|
12
12
|
|
13
13
|
## Versions
|
14
14
|
|
@@ -18,12 +18,12 @@ The Ruby library documentation can be found [here][libdocs] and individual relea
|
|
18
18
|
|
19
19
|
This library supports the following Ruby implementations:
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
- Ruby 2.4
|
22
|
+
- Ruby 2.5
|
23
|
+
- Ruby 2.6
|
24
|
+
- Ruby 2.7
|
25
|
+
- Ruby 3.0
|
26
|
+
- Ruby 3.1
|
27
27
|
|
28
28
|
### Migrating from 4.x
|
29
29
|
|
@@ -34,13 +34,13 @@ This library supports the following Ruby implementations:
|
|
34
34
|
To install using [Bundler][bundler] grab the latest stable version:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
gem 'twilio-ruby', '~> 5.
|
37
|
+
gem 'twilio-ruby', '~> 5.77.0'
|
38
38
|
```
|
39
39
|
|
40
40
|
To manually install `twilio-ruby` via [Rubygems][rubygems] simply gem install:
|
41
41
|
|
42
42
|
```bash
|
43
|
-
gem install twilio-ruby -v 5.
|
43
|
+
gem install twilio-ruby -v 5.77.0
|
44
44
|
```
|
45
45
|
|
46
46
|
To build and install the development branch yourself from the latest source:
|
@@ -51,18 +51,47 @@ cd twilio-ruby
|
|
51
51
|
make install
|
52
52
|
```
|
53
53
|
|
54
|
-
|
54
|
+
> **Info**
|
55
|
+
> If the command line gives you an error message that says Permission Denied, try running the above commands with sudo.
|
56
|
+
>
|
57
|
+
> For example: `sudo gem install twilio-ruby`
|
55
58
|
|
56
|
-
###
|
59
|
+
### Test your installation
|
60
|
+
|
61
|
+
To make sure the installation was successful, try sending yourself an SMS message, like this:
|
62
|
+
|
63
|
+
```rb
|
64
|
+
require "twilio-ruby"
|
65
|
+
|
66
|
+
# Your Account SID and Auth Token from console.twilio.com
|
67
|
+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
68
|
+
auth_token = "your_auth_token"
|
69
|
+
|
70
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
71
|
+
message = @client.messages.create(
|
72
|
+
body: "Hello from Ruby",
|
73
|
+
to: "+12345678901", # Text this number
|
74
|
+
from: "+15005550006", # From a valid Twilio number
|
75
|
+
)
|
76
|
+
|
77
|
+
puts message.sid
|
78
|
+
```
|
79
|
+
|
80
|
+
> **Warning**
|
81
|
+
> It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out [How to Set Environment Variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information.
|
82
|
+
|
83
|
+
## Usage
|
84
|
+
|
85
|
+
### Authenticate the Client
|
57
86
|
|
58
87
|
```ruby
|
59
88
|
require 'twilio-ruby'
|
60
89
|
|
61
|
-
#
|
90
|
+
# Your Account SID and Auth Token from console.twilio.com
|
62
91
|
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
63
92
|
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
|
64
93
|
|
65
|
-
#
|
94
|
+
# Initialize the Twilio Client with your credentials
|
66
95
|
@client = Twilio::REST::Client.new account_sid, auth_token
|
67
96
|
```
|
68
97
|
|
@@ -71,8 +100,9 @@ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
|
|
71
100
|
```ruby
|
72
101
|
require 'twilio-ruby'
|
73
102
|
|
74
|
-
#
|
103
|
+
# Your Account SID from console.twilio.com
|
75
104
|
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
105
|
+
# API Key from twilio.com/console/project/api-keys
|
76
106
|
api_key_sid = 'zzzzzzzzzzzzzzzzzzzzzz'
|
77
107
|
api_key_secret = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
|
78
108
|
|
@@ -97,22 +127,6 @@ To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/doc
|
|
97
127
|
|
98
128
|
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
|
99
129
|
|
100
|
-
### Enable Debug logging
|
101
|
-
|
102
|
-
In order to enable debug logging, pass in a 'logger' instance to the client with the level set to at least 'DEBUG'
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
@client = Twilio::REST::Client.new account_sid, auth_token
|
106
|
-
myLogger = Logger.new(STDOUT)
|
107
|
-
myLogger.level = Logger::DEBUG
|
108
|
-
@client.logger = myLogger
|
109
|
-
|
110
|
-
@client = Twilio::REST::Client.new account_sid, auth_token
|
111
|
-
myLogger = Logger.new('my_log.log')
|
112
|
-
myLogger.level = Logger::DEBUG
|
113
|
-
@client.logger = myLogger
|
114
|
-
```
|
115
|
-
|
116
130
|
### Make a Call
|
117
131
|
|
118
132
|
```ruby
|
@@ -147,27 +161,59 @@ message_sid = 'SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
|
147
161
|
@client.messages(message_sid).fetch
|
148
162
|
```
|
149
163
|
|
150
|
-
###
|
164
|
+
### Iterate through records
|
151
165
|
|
152
|
-
`
|
166
|
+
The library automatically handles paging for you. Collections, such as `calls` and `messages`, have `list` and stream methods that page under the hood. With both `list` and `stream`, you can specify the number of records you want to receive (`limit`) and the maximum size you want each page fetch to be (`page_size`). The library will then handle the task for you.
|
153
167
|
|
154
|
-
|
155
|
-
|
168
|
+
`list` eagerly fetches all records and returns them as a list, whereas `stream` returns an enumerator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the `page` method.
|
169
|
+
|
170
|
+
For more information about these methods, view the [auto-generated library docs](https://www.twilio.com/docs/libraries/reference/twilio-ruby).
|
171
|
+
|
172
|
+
```rb
|
173
|
+
require 'twilio-ruby'
|
174
|
+
|
175
|
+
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
176
|
+
auth_token = 'your_auth_token'
|
177
|
+
|
178
|
+
@client = Twilio::REST::Client.new(account_sid, auth_token)
|
179
|
+
|
180
|
+
@client.calls.list
|
181
|
+
.each do |call|
|
182
|
+
puts call.direction
|
183
|
+
end
|
156
184
|
```
|
157
185
|
|
158
|
-
|
186
|
+
### Enable Debug logging
|
159
187
|
|
160
|
-
|
188
|
+
In order to enable debug logging, pass in a 'logger' instance to the client with the level set to at least 'DEBUG'
|
161
189
|
|
162
190
|
```ruby
|
163
|
-
@client.
|
164
|
-
|
165
|
-
|
191
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
192
|
+
myLogger = Logger.new(STDOUT)
|
193
|
+
myLogger.level = Logger::DEBUG
|
194
|
+
@client.logger = myLogger
|
195
|
+
|
196
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
197
|
+
myLogger = Logger.new('my_log.log')
|
198
|
+
myLogger.level = Logger::DEBUG
|
199
|
+
@client.logger = myLogger
|
166
200
|
```
|
167
201
|
|
168
|
-
###
|
202
|
+
### Handle Exceptions {#exceptions}
|
203
|
+
|
204
|
+
If the Twilio API returns a 400 or a 500 level HTTP response, the `twilio-ruby`
|
205
|
+
library will throw a `Twilio::REST::RestError`. 400-level errors are normal
|
206
|
+
during API operation (`“Invalid number”`, `“Cannot deliver SMS to that number”`,
|
207
|
+
for example) and should be handled appropriately.
|
208
|
+
|
209
|
+
```rb
|
210
|
+
require 'twilio-ruby'
|
211
|
+
|
212
|
+
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
213
|
+
auth_token = 'your_auth_token'
|
214
|
+
|
215
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
169
216
|
|
170
|
-
```ruby
|
171
217
|
begin
|
172
218
|
messages = @client.messages.list(limit: 20)
|
173
219
|
rescue Twilio::REST::RestError => e
|
@@ -175,9 +221,51 @@ rescue Twilio::REST::RestError => e
|
|
175
221
|
end
|
176
222
|
```
|
177
223
|
|
178
|
-
|
224
|
+
### Debug API requests
|
225
|
+
|
226
|
+
To assist with debugging, the library allows you to access the underlying request and response objects. This capability is built into the default HTTP client that ships with the library.
|
227
|
+
|
228
|
+
For example, you can retrieve the status code of the last response like so:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
require 'rubygems' # Not necessary with ruby 1.9 but included for completeness
|
232
|
+
require 'twilio-ruby'
|
233
|
+
|
234
|
+
# Your Account SID and Auth Token from console.twilio.com
|
235
|
+
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
236
|
+
auth_token = 'your_auth_token'
|
237
|
+
|
238
|
+
@client = Twilio::REST::Client.new(account_sid, auth_token)
|
239
|
+
|
240
|
+
@message = @client.messages.create(
|
241
|
+
to: '+14158675309',
|
242
|
+
from: '+14258675310',
|
243
|
+
body: 'Ahoy!'
|
244
|
+
)
|
245
|
+
|
246
|
+
# Retrieve the status code of the last response from the HTTP client
|
247
|
+
puts @client.http_client.last_response.status_code
|
248
|
+
```
|
249
|
+
|
250
|
+
### Customize your HTTP Client
|
251
|
+
|
252
|
+
`twilio-ruby` uses [Faraday][faraday] to make HTTP requests. You can tell `Twilio::REST::Client` to use any of the Faraday adapters like so:
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
@client.http_client.adapter = :typhoeus
|
256
|
+
```
|
257
|
+
|
258
|
+
To use a custom HTTP client with this helper library, please see the [advanced example of how to do so](./advanced-examples/custom-http-client.md).
|
259
|
+
|
260
|
+
To apply customizations such as middleware, you can use the `configure_connection` method like so:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
@client.http_client.configure_connection do |faraday|
|
264
|
+
faraday.use SomeMiddleware
|
265
|
+
end
|
266
|
+
```
|
179
267
|
|
180
|
-
###
|
268
|
+
### Get started With Client Capability Tokens
|
181
269
|
|
182
270
|
If you just need to generate a Capability Token for use with Twilio Client, you can do this:
|
183
271
|
|
@@ -205,7 +293,7 @@ capability.add_scope(incoming_scope)
|
|
205
293
|
|
206
294
|
There is a slightly more detailed document in the [Capability][capability] section of the wiki.
|
207
295
|
|
208
|
-
###
|
296
|
+
### Generate TwiML
|
209
297
|
|
210
298
|
To control phone calls, your application needs to output [TwiML][twiml].
|
211
299
|
|
@@ -249,7 +337,6 @@ If you've instead found a bug in the library or would like new features added, g
|
|
249
337
|
|
250
338
|
[apidocs]: https://www.twilio.com/docs/api
|
251
339
|
[twiml]: https://www.twilio.com/docs/api/twiml
|
252
|
-
[libdocs]: https://www.twilio.com/docs/libraries/ruby
|
253
340
|
[refdocs]: https://twilio.github.io/twilio-ruby
|
254
341
|
[capability]: https://github.com/twilio/twilio-ruby/wiki/JWT-Tokens
|
255
342
|
[wiki]: https://github.com/twilio/twilio-ruby/wiki
|
@@ -0,0 +1,170 @@
|
|
1
|
+
# Custom HTTP Clients for the Twilio Ruby Helper Library
|
2
|
+
|
3
|
+
If you are working with the Twilio Ruby Helper Library, and you need to be able to modify the HTTP requests that the library makes to the Twilio servers, you’re in the right place. The most common need to alter the HTTP request is to connect and authenticate with an enterprise’s proxy server. We’ll provide sample code that you can drop right into your app to handle this use case.
|
4
|
+
|
5
|
+
Connect and authenticate with a proxy server
|
6
|
+
To connect and provide credentials to a proxy server that may be between your app and Twilio, you need a way to modify the HTTP requests that the Twilio helper library makes on your behalf to invoke the Twilio REST API.
|
7
|
+
|
8
|
+
The Twilio Ruby helper library uses the [Faraday](https://rubygems.org/gems/faraday) gem under the hood to make the HTTP requests. The following example shows a typical request, without a custom `http_client`:
|
9
|
+
|
10
|
+
```rb
|
11
|
+
@client = Twilio::REST::Client.new(account_sid, auth_token)
|
12
|
+
|
13
|
+
message = @client.messages
|
14
|
+
.create(
|
15
|
+
to: "+15558675310",
|
16
|
+
body: "Hey there!",
|
17
|
+
from: "+15017122661",
|
18
|
+
)
|
19
|
+
```
|
20
|
+
|
21
|
+
Out of the box, the helper library is creating a default `Twilio::Http::Client` for you, using the Twilio credentials you provide. However, you can create your own `Twilio::Http::Client`, and pass it to any Twilio REST API resource action you want.
|
22
|
+
|
23
|
+
Here’s an example of sending an SMS message with a custom client:
|
24
|
+
|
25
|
+
```rb
|
26
|
+
# Download the helper library from https://www.twilio.com/docs/ruby/install
|
27
|
+
require "rubygems"
|
28
|
+
require "twilio-ruby"
|
29
|
+
require "dotenv/load"
|
30
|
+
|
31
|
+
# Custom HTTP Client
|
32
|
+
require_relative "MyRequestClass"
|
33
|
+
|
34
|
+
# Your Account Sid and Auth Token from twilio.com/console
|
35
|
+
account_sid = ENV["ACCOUNT_SID"]
|
36
|
+
auth_token = ENV["AUTH_TOKEN"]
|
37
|
+
proxy_address = ENV["PROXY_ADDRESS"]
|
38
|
+
proxy_protocol = ENV["PROXY_PROTOCOL"]
|
39
|
+
proxy_port = ENV["PROXY_PORT"]
|
40
|
+
|
41
|
+
my_request_client = MyRequestClass.new(proxy_protocol, proxy_address, proxy_port)
|
42
|
+
|
43
|
+
@client = Twilio::REST::Client.new(account_sid, auth_token,
|
44
|
+
nil, nil, my_request_client)
|
45
|
+
|
46
|
+
message = @client.messages
|
47
|
+
.create(
|
48
|
+
to: "+593978613041",
|
49
|
+
body: "RB This is the ship that made the Kesssssel Run in fourteen parsecs?",
|
50
|
+
from: "+13212855389",
|
51
|
+
)
|
52
|
+
|
53
|
+
puts "Message SID: #{message.sid}"
|
54
|
+
```
|
55
|
+
|
56
|
+
## Create your custom TwilioRestClient
|
57
|
+
|
58
|
+
When you take a closer look at the constructor for `Twilio::Http::Client`, you see that this class provides it to the Twilio helper library to make the necessary HTTP requests.
|
59
|
+
|
60
|
+
## Call Twilio through the proxy server
|
61
|
+
|
62
|
+
Now that we understand how all the components fit together, we can create our own `http_client` that can connect through a proxy server. To make this reusable, here’s a class that you can use to create this `http_client` whenever you need one.
|
63
|
+
|
64
|
+
```rb
|
65
|
+
class MyRequestClass
|
66
|
+
attr_accessor :adapter
|
67
|
+
attr_reader :timeout, :last_response, :last_request
|
68
|
+
|
69
|
+
def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, timeout: nil)
|
70
|
+
@proxy_prot = proxy_prot
|
71
|
+
@proxy_addr = proxy_addr
|
72
|
+
@proxy_port = proxy_port
|
73
|
+
@timeout = timeout
|
74
|
+
@adapter = Faraday.default_adapter
|
75
|
+
end
|
76
|
+
|
77
|
+
def _request(request)
|
78
|
+
@connection = Faraday.new(url: request.host + ":" + request.port.to_s, ssl: { verify: true }) do |f|
|
79
|
+
f.options.params_encoder = Faraday::FlatParamsEncoder
|
80
|
+
f.request :url_encoded
|
81
|
+
f.adapter @adapter
|
82
|
+
f.headers = request.headers
|
83
|
+
f.basic_auth(request.auth[0], request.auth[1])
|
84
|
+
if @proxy_addr
|
85
|
+
f.proxy = "#{@proxy_prot}://#{@proxy_addr}:#{@proxy_port}"
|
86
|
+
end
|
87
|
+
f.options.open_timeout = request.timeout || @timeout
|
88
|
+
f.options.timeout = request.timeout || @timeout
|
89
|
+
end
|
90
|
+
|
91
|
+
@last_request = request
|
92
|
+
@last_response = nil
|
93
|
+
response = @connection.send(request.method.downcase.to_sym,
|
94
|
+
request.url,
|
95
|
+
request.method == "GET" ? request.params : request.data)
|
96
|
+
|
97
|
+
if response.body && !response.body.empty?
|
98
|
+
object = response.body
|
99
|
+
elsif response.status == 400
|
100
|
+
object = { message: "Bad request", code: 400 }.to_json
|
101
|
+
end
|
102
|
+
|
103
|
+
twilio_response = Twilio::Response.new(response.status, object, headers: response.headers)
|
104
|
+
@last_response = twilio_response
|
105
|
+
|
106
|
+
twilio_response
|
107
|
+
end
|
108
|
+
|
109
|
+
def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
|
110
|
+
request = Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout)
|
111
|
+
_request(request)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
In this example, we are using some environment variables loaded at the program startup to retrieve various configuration settings:
|
117
|
+
|
118
|
+
- Your Twilio Account Sid and Auth Token ([found here, in the Twilio console](https://console.twilio.com))
|
119
|
+
- A proxy address in the form of `http://127.0.0.1:8888`
|
120
|
+
|
121
|
+
These settings are located in a file like `.env` like so:
|
122
|
+
|
123
|
+
```env
|
124
|
+
ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
125
|
+
AUTH_TOKEN= your_auth_token
|
126
|
+
|
127
|
+
HTTPS_PROXY=https://127.0.0.1:8888
|
128
|
+
HTTP_PROXY=http://127.0.0.1:8888
|
129
|
+
```
|
130
|
+
|
131
|
+
Here’s the full console program that sends a text message and shows how it all can work together. It loads the `.env` file.
|
132
|
+
|
133
|
+
```rb
|
134
|
+
# Download the helper library from https://www.twilio.com/docs/ruby/install
|
135
|
+
require "rubygems"
|
136
|
+
require "twilio-ruby"
|
137
|
+
require "dotenv/load"
|
138
|
+
|
139
|
+
# Custom HTTP Client
|
140
|
+
require_relative "MyRequestClass"
|
141
|
+
|
142
|
+
# Your Account Sid and Auth Token from twilio.com/console
|
143
|
+
account_sid = ENV["ACCOUNT_SID"]
|
144
|
+
auth_token = ENV["AUTH_TOKEN"]
|
145
|
+
proxy_address = ENV["PROXY_ADDRESS"]
|
146
|
+
proxy_protocol = ENV["PROXY_PROTOCOL"]
|
147
|
+
proxy_port = ENV["PROXY_PORT"]
|
148
|
+
|
149
|
+
my_request_client = MyRequestClass.new(proxy_protocol, proxy_address, proxy_port)
|
150
|
+
|
151
|
+
@client = Twilio::REST::Client.new(account_sid, auth_token,
|
152
|
+
nil, nil, my_request_client)
|
153
|
+
|
154
|
+
message = @client.messages
|
155
|
+
.create(
|
156
|
+
to: "+593978613041",
|
157
|
+
body: "RB This is the ship that made the Kesssssel Run in fourteen parsecs?",
|
158
|
+
from: "+13212855389",
|
159
|
+
)
|
160
|
+
|
161
|
+
puts "Message SID: #{message.sid}"
|
162
|
+
```
|
163
|
+
|
164
|
+
## What else can this technique be used for?
|
165
|
+
|
166
|
+
Now that you know how to inject your own `http_client` into the Twilio API request pipeline, you could use this technique to add custom HTTP headers and authorization to the requests (perhaps as required by an upstream proxy server).
|
167
|
+
|
168
|
+
You could also implement your own `http_client` to mock the Twilio API responses so your unit and integration tests can run quickly without the need to make a connection to Twilio. In fact, there’s already an example online showing [how to do exactly that with Node.js and Prism](https://www.twilio.com/docs/openapi/mock-api-generation-with-twilio-openapi-spec).
|
169
|
+
|
170
|
+
We can’t wait to see what you build!
|
@@ -71,6 +71,14 @@ module Twilio
|
|
71
71
|
# Lists ConversationInstance records from the API as a list.
|
72
72
|
# Unlike stream(), this operation is eager and will load `limit` records into
|
73
73
|
# memory before returning.
|
74
|
+
# @param [String] start_date Start date or time in ISO8601 format for filtering
|
75
|
+
# list of Conversations. If a date is provided, the start time of the date is used
|
76
|
+
# (YYYY-MM-DDT00:00:00Z). Can be combined with other filters.
|
77
|
+
# @param [String] end_date End date or time in ISO8601 format for filtering list
|
78
|
+
# of Conversations. If a date is provided, the end time of the date is used
|
79
|
+
# (YYYY-MM-DDT23:59:59Z). Can be combined with other filters.
|
80
|
+
# @param [conversation.State] state State for sorting and filtering list of
|
81
|
+
# Conversations. Can be `active`, `inactive` or `closed`
|
74
82
|
# @param [Integer] limit Upper limit for the number of records to return. stream()
|
75
83
|
# guarantees to never return more than limit. Default is no limit
|
76
84
|
# @param [Integer] page_size Number of records to fetch per request, when
|
@@ -78,14 +86,28 @@ module Twilio
|
|
78
86
|
# but a limit is defined, stream() will attempt to read the limit with the most
|
79
87
|
# efficient page size, i.e. min(limit, 1000)
|
80
88
|
# @return [Array] Array of up to limit results
|
81
|
-
def list(limit: nil, page_size: nil)
|
82
|
-
self.stream(
|
89
|
+
def list(start_date: :unset, end_date: :unset, state: :unset, limit: nil, page_size: nil)
|
90
|
+
self.stream(
|
91
|
+
start_date: start_date,
|
92
|
+
end_date: end_date,
|
93
|
+
state: state,
|
94
|
+
limit: limit,
|
95
|
+
page_size: page_size
|
96
|
+
).entries
|
83
97
|
end
|
84
98
|
|
85
99
|
##
|
86
100
|
# Streams ConversationInstance records from the API as an Enumerable.
|
87
101
|
# This operation lazily loads records as efficiently as possible until the limit
|
88
102
|
# is reached.
|
103
|
+
# @param [String] start_date Start date or time in ISO8601 format for filtering
|
104
|
+
# list of Conversations. If a date is provided, the start time of the date is used
|
105
|
+
# (YYYY-MM-DDT00:00:00Z). Can be combined with other filters.
|
106
|
+
# @param [String] end_date End date or time in ISO8601 format for filtering list
|
107
|
+
# of Conversations. If a date is provided, the end time of the date is used
|
108
|
+
# (YYYY-MM-DDT23:59:59Z). Can be combined with other filters.
|
109
|
+
# @param [conversation.State] state State for sorting and filtering list of
|
110
|
+
# Conversations. Can be `active`, `inactive` or `closed`
|
89
111
|
# @param [Integer] limit Upper limit for the number of records to return. stream()
|
90
112
|
# guarantees to never return more than limit. Default is no limit.
|
91
113
|
# @param [Integer] page_size Number of records to fetch per request, when
|
@@ -93,10 +115,15 @@ module Twilio
|
|
93
115
|
# but a limit is defined, stream() will attempt to read the limit with the most
|
94
116
|
# efficient page size, i.e. min(limit, 1000)
|
95
117
|
# @return [Enumerable] Enumerable that will yield up to limit results
|
96
|
-
def stream(limit: nil, page_size: nil)
|
118
|
+
def stream(start_date: :unset, end_date: :unset, state: :unset, limit: nil, page_size: nil)
|
97
119
|
limits = @version.read_limits(limit, page_size)
|
98
120
|
|
99
|
-
page = self.page(
|
121
|
+
page = self.page(
|
122
|
+
start_date: start_date,
|
123
|
+
end_date: end_date,
|
124
|
+
state: state,
|
125
|
+
page_size: limits[:page_size],
|
126
|
+
)
|
100
127
|
|
101
128
|
@version.stream(page, limit: limits[:limit], page_limit: limits[:page_limit])
|
102
129
|
end
|
@@ -118,12 +145,23 @@ module Twilio
|
|
118
145
|
##
|
119
146
|
# Retrieve a single page of ConversationInstance records from the API.
|
120
147
|
# Request is executed immediately.
|
148
|
+
# @param [String] start_date Start date or time in ISO8601 format for filtering
|
149
|
+
# list of Conversations. If a date is provided, the start time of the date is used
|
150
|
+
# (YYYY-MM-DDT00:00:00Z). Can be combined with other filters.
|
151
|
+
# @param [String] end_date End date or time in ISO8601 format for filtering list
|
152
|
+
# of Conversations. If a date is provided, the end time of the date is used
|
153
|
+
# (YYYY-MM-DDT23:59:59Z). Can be combined with other filters.
|
154
|
+
# @param [conversation.State] state State for sorting and filtering list of
|
155
|
+
# Conversations. Can be `active`, `inactive` or `closed`
|
121
156
|
# @param [String] page_token PageToken provided by the API
|
122
157
|
# @param [Integer] page_number Page Number, this value is simply for client state
|
123
158
|
# @param [Integer] page_size Number of records to return, defaults to 50
|
124
159
|
# @return [Page] Page of ConversationInstance
|
125
|
-
def page(page_token: :unset, page_number: :unset, page_size: :unset)
|
160
|
+
def page(start_date: :unset, end_date: :unset, state: :unset, page_token: :unset, page_number: :unset, page_size: :unset)
|
126
161
|
params = Twilio::Values.of({
|
162
|
+
'StartDate' => start_date,
|
163
|
+
'EndDate' => end_date,
|
164
|
+
'State' => state,
|
127
165
|
'PageToken' => page_token,
|
128
166
|
'Page' => page_number,
|
129
167
|
'PageSize' => page_size,
|