twilio-ruby 4.3.0 → 4.4.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
  SHA1:
3
- metadata.gz: b89103d98fe5a0cc274231f662de67e7a3b33c35
4
- data.tar.gz: 23519a4229477cd81a3d0a03023c3c0d0032eb28
3
+ metadata.gz: ecb07fd26a80b320199741a2ce2c188331969654
4
+ data.tar.gz: 1dbdea6628f1a8f9f7bda6fd25d28b7ec30a4884
5
5
  SHA512:
6
- metadata.gz: 22efb989dd0661f6e15f2357faff19b67cb9ee0314d38024ea5a399b46fb8dd1d44f30975bc1b744ef3f75854d97df17d71992c528b9ee231cd75d5d685399f1
7
- data.tar.gz: e3b6739ff15ef1c1ab643f85923776e8831213412c8a1ddc3d36c23cc2331324976f6357aabd098f667ebaf651e97abab5883d26595c03e90714e86fa4e58a55
6
+ metadata.gz: 624f9abd8990248bea0fdbb3fc7a235cddd6ff0edbe197237dc6b048dea14a052a3200f237acaea47754b72f7d8b4e95dfafc274a48c7dc5bc145ded3b071726
7
+ data.tar.gz: 5fb8fe1662af3af16b1efc90eb746ca78aa6a96d7eff2568574ffc23725e51518913367e2858ae9c028479845eec5332db3ccdaee6eb180493ea0f0306b5e2c7
data/CHANGES.md CHANGED
@@ -1,6 +1,14 @@
1
1
  twilio-ruby changelog
2
2
  =====================
3
3
 
4
+ Version 4.4.0
5
+ -------------
6
+
7
+ Release September 23, 2015
8
+
9
+ - Add fetching a list of worker reservations
10
+ - Add TaskRouter Workflow builder
11
+
4
12
  Version 4.3.0
5
13
  -------------
6
14
 
data/README.md CHANGED
@@ -118,7 +118,30 @@ end
118
118
  @client.incoming_phone_numbers.create(phone_number: @number)
119
119
  ```
120
120
 
121
- ## Getting Started With Client Capability Tokens
121
+ ## Create a Task with TaskRouter
122
+
123
+ If you need to create a Task to TaskRouter, you can do so by using the TaskRouterClient.
124
+
125
+ Additional resources had off of the workspace object (task_queues, workers, workflows, activities, tasks, statistics, events).
126
+
127
+ ``` ruby
128
+ require 'rubygems'
129
+ require 'twilio-ruby'
130
+
131
+ # put your own account credentials here:
132
+ account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
133
+ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
134
+ workspace_sid = 'WSzzzzzzzzzzzzzzzzzzzzzzzzzzz'
135
+
136
+ # set up a client
137
+ client = Twilio::REST::TaskRouterClient.new account_sid, auth_token, workspace_sid
138
+
139
+ # create a task
140
+ workflow_sid = 'WWffffffffffffffffffffffffffff'
141
+ client.workspace.tasks.create(attributes: '{"foo": "bar"}', workflow_sid: "WWfffffffffffffffffffffffffffffff")
142
+ ```
143
+
144
+ ## Create a Twilio Client Capability Token
122
145
 
123
146
  If you just need to generate a Capability Token for use with Twilio Client, you
124
147
  can do this:
@@ -128,8 +151,8 @@ require 'rubygems' # not necessary with ruby 1.9 but included for completeness
128
151
  require 'twilio-ruby'
129
152
 
130
153
  # put your own account credentials here:
131
- account_sid = 'AC043dcf9844e13758bc3a36a84c29761'
132
- auth_token = '62ea81de3a5b413254eb263595357c69'
154
+ account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
155
+ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
133
156
 
134
157
  # set up
135
158
  capability = Twilio::Util::Capability.new account_sid, auth_token
@@ -87,6 +87,27 @@ to easily create such responses.
87
87
  <Play loop="5">https://api.twilio.com/cowbell.mp3</Play>
88
88
  <Response>
89
89
 
90
+ Create a Task with TaskRouter
91
+ =================
92
+
93
+ If you need to create a Task to TaskRouter, you can do so by using the TaskRouterClient.
94
+
95
+ Additional resources had off of the workspace object (task_queues, workers, workflows, activities, tasks, statistics, events).
96
+
97
+ .. code-block:: ruby
98
+
99
+ require 'twilio-ruby'
100
+
101
+ # To find these visit https://www.twilio.com/user/account
102
+ account_sid = "ACXXXXXXXXXXXXXXXXX"
103
+ auth_token = "YYYYYYYYYYYYYYYYYY"
104
+ workspace_sid = "WSzzzzzzzzzzzzzzzzzzzzzzzzzzz"
105
+
106
+ client = Twilio::REST::TaskRouterClient.new account_sid, auth_token, workspace_sid
107
+
108
+ workflow_sid = "WFzzzzzzzzzzzzz"
109
+ task = client.workspace.tasks.create(attributes: '{"foo":"bar"}', workflow_sid: workflow_sid)
110
+
90
111
 
91
112
  Digging Deeper
92
113
  ========================
@@ -11,17 +11,15 @@ can best handle them.
11
11
  For more information, see the `TaskRouter documentation
12
12
  <https://www.twilio.com/docs/taskrouter>_`.
13
13
 
14
+ Note: Passed in Form/Query Parameters are based on their json equivalent attribute.
14
15
 
15
- Creating a Workspace
16
+ Workspaces
16
17
  --------------------
17
18
 
18
19
  A Workspace is a container for your Tasks, Workers, TaskQueues, Workflows and
19
20
  Activities. Each of these items exists within a single Workspace and will not
20
21
  be shared across Workspaces.
21
22
 
22
- The following code will create a new :class:`Workspace` resource and print
23
- its unique ID.
24
-
25
23
  .. code-block:: ruby
26
24
 
27
25
  require 'twilio-ruby'
@@ -35,12 +33,29 @@ its unique ID.
35
33
 
36
34
  @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
37
35
 
36
+ # creating a workspace
38
37
  @workspace = @client.workspaces.create(
39
38
  friendly_name: "Customer Support",
40
39
  template: "FIFO", # Sets up default activities and a FIFO TaskQueue
41
40
  )
42
41
  puts @workspace.sid
43
42
 
43
+ # fetching a workspace
44
+ @workspace = @client.workspace
45
+ puts @workspace.sid
46
+ puts @workspace.friendly_name
47
+
48
+ # fetching a list of workspaces
49
+ @client.workspaces.list.each do |workspace|
50
+ puts workspace.friendly_name
51
+ end
52
+
53
+ # updating a workspace
54
+ @client.workspace.update(friendly_name: 'MyWorkspace2')
55
+
56
+ # deleting a workspace
57
+ @client.workspace.delete
58
+
44
59
 
45
60
  Workflows
46
61
  ---------
@@ -50,6 +65,9 @@ how Tasks should escalate in priority or move across queues over time.
50
65
  Workflows are described in a simple JSON format and can be modified through the
51
66
  REST API or through the account portal.
52
67
 
68
+ Creating a Workflow
69
+ --------------------
70
+
53
71
  The following code will create a new :class:`Workflow` resource and print its
54
72
  unique ID:
55
73
 
@@ -75,14 +93,14 @@ unique ID:
75
93
  "expression":"customer_value == 'Gold' AND type == 'ticket'",
76
94
  "targets":[
77
95
  {
78
- "task_queue_sid":"WQ0123456789abcdef0123456789abcdef",
96
+ "queue":"YourGoldTicketQueueSid",
79
97
  "priority":"2"
80
98
  }
81
99
  ]
82
100
  }
83
101
  ],
84
102
  "default_filter":{
85
- "task_queue_sid":"WQabcdef01234567890123456789abcdef"
103
+ "queue":"YourDefaultQueueSid"
86
104
  }
87
105
  }
88
106
  }
@@ -90,14 +108,75 @@ unique ID:
90
108
 
91
109
  @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
92
110
 
93
- @workspace = @client.workflows.create(
111
+ @workflow = @client.workspace.workflows.create(
94
112
  friendly_name: "Incoming Call Flow",
95
113
  assignment_callback_url: "https://example.com/callback",
96
114
  fallback_assignment_callback_url: "https://example.com/callback2",
97
115
  configuration: CONFIG
98
116
  )
99
- puts @workspace.sid
117
+ puts @workflow.sid
118
+
119
+ You can also utilize our Workflow Builder to make this process a bit easier utilizing objects:
120
+
121
+ .. code-block:: ruby
122
+
123
+ require twilio-ruby
124
+
125
+ # To find these visit https://www.twilio.com/user/account
126
+ ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
127
+ AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
128
+
129
+ # See previous examples to create a Workspace
130
+ WORKSPACE_SID = "WSZZZZZZZZZZZZZZ"
131
+
132
+ @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
133
+
134
+ gold_ticket_queue_sid = 'YourGoldTicketQueueSid'
135
+ default_queue_sid = 'YourDefaultQueueSid'
136
+
137
+ gold_ticket_targets = [Twilio::TaskRouter::WorkflowRuleTarget.new(gold_ticket_queue_sid)]
138
+ gold_ticket_rule = Twilio::TaskRouter::WorkflowRule.new 'customer_value == "Gold" AND type == "ticket"', gold_ticket_targets, 'Gold Tickets'
139
+
140
+ @rules = [gold_ticket_rule]
141
+ @default_target = Twilio::TaskRouter::WorkflowRuleTarget.new default_queue_sid
100
142
 
143
+ @config = Twilio::TaskRouter::WorkflowConfiguration.new @rules, @default_target
144
+ puts @config.to_json
145
+
146
+ @workflow = @client.workspace.workflows.create(configuration: @config.to_json, friendly_name: 'Incoming Call Flow', assignment_callback_url: 'https://example.com/callback', fallback_assignment_callback_url: 'https://example.com/callback2')
147
+ puts @workflow.sid
148
+
149
+ Fetching, Updating, Deleting Workflows
150
+ --------------------
151
+
152
+ .. code-block:: ruby
153
+
154
+ require twilio-ruby
155
+
156
+ # To find these visit https://www.twilio.com/user/account
157
+ ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
158
+ AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
159
+
160
+ # See previous examples to create a Workspace
161
+ WORKSPACE_SID = "WSZZZZZZZZZZZZZZ"
162
+ WORKFLOW_SID = "WFZZZZZZZZZZZZZZZ"
163
+
164
+ # fetching a workflow
165
+ @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
166
+ @workflow = @client.workspace.workflows.get(WORKFLOW_SID)
167
+ puts @workflow.sid
168
+ puts @workflow.friendly_name
169
+
170
+ # fetching a list of workflows
171
+ @client.workspace.workflows.list.each do |workflow|
172
+ puts workflow.friendly_name
173
+ end
174
+
175
+ # updating a workflow
176
+ @workflow.update(friendly_name: 'NewWorkflowName')
177
+
178
+ # deleting a workflow
179
+ @workflow.delete
101
180
 
102
181
  Activities
103
182
  ----------
@@ -106,8 +185,6 @@ Activities describe the current status of your Workers, which determines
106
185
  whether they are eligible to receive task assignments. Workers are always set
107
186
  to a single Activity.
108
187
 
109
- To create a new :class:`Activity`:
110
-
111
188
  .. code-block:: ruby
112
189
 
113
190
  require 'twilio-ruby'
@@ -121,12 +198,27 @@ To create a new :class:`Activity`:
121
198
 
122
199
  @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
123
200
 
124
- @activity = @client.activities.create(
201
+ # creating an activity
202
+ @activity = @client.workspace.activities.create(
125
203
  friendly_name: "Coffee Break",
126
204
  available: false # Whether workers are available to handle tasks during this activity
127
205
  )
128
206
  puts @activity.sid
129
207
 
208
+ # fetching a list of activities
209
+ @client.workspace.activities.list.each do |activity|
210
+ puts activity.friendly_name
211
+ end
212
+
213
+ # fetching an activity
214
+ ACTIVITY_SID = "WAZZZZZZZZZZZZZZZZZ"
215
+ @activity = @client.workspace.activities.get(ACTIVITY_SID)
216
+
217
+ # updating an activity
218
+ @activity.update(friendly_name: 'NewFriendlyName')
219
+
220
+ # deleting an activity
221
+ @activity.delete
130
222
 
131
223
  Workers
132
224
  -------
@@ -134,8 +226,6 @@ Workers
134
226
  Workers represent an entity that is able to perform tasks, such as an agent
135
227
  working in a call center, or a salesperson handling leads.
136
228
 
137
- To create a new :class:`Worker`:
138
-
139
229
  .. code-block:: ruby
140
230
 
141
231
  require 'twilio-ruby'
@@ -149,12 +239,33 @@ To create a new :class:`Worker`:
149
239
 
150
240
  @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
151
241
 
152
- @worker = @client.workers.create(
242
+ # creating a worker
243
+ @worker = @client.workspace.workers.create(
153
244
  friendly_name:"Jamie",
154
245
  attributes:'{"phone": "+14155551234", "languages": ["EN", "ES"]}'
155
246
  )
156
247
  puts @worker.sid
157
248
 
249
+ # fetching a list of workers
250
+ @client.workspace.workers.list.each do |worker|
251
+ puts worker.friendly_name
252
+ end
253
+
254
+ # fetching a list of workers based on activity
255
+ @client.workspace.workers.list(activity_name: 'Offline').each do |worker|
256
+ puts worker.friendly_name + ' is offline'
257
+ end
258
+
259
+ # fetching an worker
260
+ WORKER_SID = "WKZZZZZZZZZZZZZZZZZ"
261
+ @worker = @client.workspace.workers.get(WORKER_SID)
262
+
263
+ # updating an worker
264
+ @worker.update(friendly_name: 'NewFriendlyName')
265
+
266
+ # deleting an worker
267
+ @worker.delete
268
+
158
269
 
159
270
  TaskQueues
160
271
  ----------
@@ -164,8 +275,6 @@ Workers are eligible to handle those Tasks. As your Workflows process Tasks,
164
275
  those Tasks will pass through one or more TaskQueues until the Task is assigned
165
276
  and accepted by an eligible Worker.
166
277
 
167
- To create a new :class:`TaskQueue`:
168
-
169
278
  .. code-block:: ruby
170
279
 
171
280
  require 'twilio-ruby'
@@ -179,14 +288,30 @@ To create a new :class:`TaskQueue`:
179
288
 
180
289
  @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
181
290
 
182
- @queue = @client.task_queues.create(
291
+ # creating a task_queue
292
+ @taskqueue = @client.workspace.task_queues.create(
183
293
  friendly_name: "Sales",
184
294
  # The Activity to assign workers when a task is reserved for them
185
295
  reservation_activity_sid: "WA11111111111",
186
296
  # The Activity to assign workers when a task is assigned to them
187
297
  assignment_activity_sid: "WA222222222222",
188
298
  )
189
- puts @queue.sid
299
+ puts @taskqueue.sid
300
+
301
+ # fetching a list of task_queues
302
+ @client.workspace.task_queues.list.each do |task_queue|
303
+ puts task_queue.friendly_name
304
+ end
305
+
306
+ # fetching an taskqueue
307
+ TASK_QUEUE_SID = "WQZZZZZZZZZZZZZZZZZ"
308
+ @taskqueue = @client.workspace.task_queues.get(TASK_QUEUE_SID)
309
+
310
+ # updating an taskqueue
311
+ @taskqueue.update(friendly_name: 'NewFriendlyName')
312
+
313
+ # deleting an taskqueue
314
+ @taskqueue.delete
190
315
 
191
316
 
192
317
  Tasks
@@ -195,8 +320,6 @@ Tasks
195
320
  A Task instance resource represents a single item of work waiting to be
196
321
  processed.
197
322
 
198
- To create a new :class:`Task` via the REST API:
199
-
200
323
  .. code-block:: ruby
201
324
 
202
325
  # To find these visit https://www.twilio.com/user/account
@@ -219,8 +342,99 @@ To create a new :class:`Task` via the REST API:
219
342
  }
220
343
  EOS
221
344
 
222
- @task = @client.create(
345
+ # creating a task
346
+ @task = @client.workspace.tasks.create(
223
347
  attributes: TASK_ATTRIBUTES,
224
348
  assignment_status: 'pending',
225
349
  )
226
350
  puts @task.sid
351
+
352
+ # fetching a list of tasks
353
+ @client.workspace.tasks.list.each do |task|
354
+ puts task.sid
355
+ end
356
+
357
+ # fetching a list of tasks that are pending
358
+ @client.workspace.tasks.list(assignment_status: 'pending').each do |task|
359
+ puts task.sid
360
+ end
361
+
362
+ # fetching an task
363
+ TASK_SID = "WTZZZZZZZZZZZZZZZZZ"
364
+ @task = @client.workspace.tasks.get(TASK_SID)
365
+
366
+ # updating an task
367
+ @task.update(friendly_name: 'NewFriendlyName')
368
+
369
+ # deleting an task
370
+ @task.delete
371
+
372
+ Reservations
373
+ -----
374
+
375
+ A Reservation instance resource represents a single matching item of work from a task to a worker.
376
+
377
+ .. code-block:: ruby
378
+
379
+ # To find these visit https://www.twilio.com/user/account
380
+ ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
381
+ AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
382
+
383
+ # See previous examples to create a Workspace
384
+ WORKSPACE_SID = "WSZZZZZZZZZZZZZZ"
385
+
386
+ @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
387
+
388
+ # fetching an task
389
+ TASK_SID = "WTZZZZZZZZZZZZZZZZZ"
390
+ @task = @client.workspace.tasks.get(TASK_SID)
391
+
392
+ # fetching reservations for said task
393
+ @task.reservations.list.each do |reservation|
394
+ puts reservation.sid
395
+ end
396
+
397
+ Statistics
398
+ -----
399
+
400
+ A Statistics resource represents the statistics over a time period for a particular resource
401
+
402
+ .. code-block:: ruby
403
+
404
+ # To find these visit https://www.twilio.com/user/account
405
+ ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
406
+ AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
407
+
408
+ # See previous examples to create a Workspace
409
+ WORKSPACE_SID = "WSZZZZZZZZZZZZZZ"
410
+
411
+ @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
412
+
413
+ # fetching statistics based on the last 4 hours
414
+ @workspace_stats = @client.workspace.statistics(minutes: '240')
415
+ @cumulative = @workspace_stats.cumulative
416
+ puts 'Avg Task Acceptance Time: ' + @cumulative['avg_task_acceptance_time'].to_s \
417
+ + ' with ' + @cumulative['tasks_created'].to_s + ' tasks created'
418
+
419
+ Events
420
+ -----
421
+
422
+ A Event represents an internal TaskRouter event that occurred and has been logged.
423
+ You can query based on time the event occurred, a certain resource or combination.
424
+
425
+ .. code-block:: ruby
426
+
427
+ # To find these visit https://www.twilio.com/user/account
428
+ ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
429
+ AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"
430
+
431
+ # See previous examples to create a Workspace
432
+ WORKSPACE_SID = "WSZZZZZZZZZZZZZZ"
433
+
434
+ @client = Twilio::REST::TaskRouterClient.new ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID
435
+
436
+ # fetching events for a workspace for the last 15 minutes
437
+ @events = @client.workspace.events.list(minutes: '15')
438
+ @events.each do |event|
439
+ puts event.event_type + ' at ' + event.event_date
440
+ end
@@ -75,6 +75,25 @@ for outputting valid TwiML to control phone calls and messages.
75
75
  # Twilio Application Sid
76
76
  application_sid = "APabe7650f654fc34655fc81ae71caa3ff"
77
77
  @capability.allow_client_outgoing(application_sid)
78
+
79
+ Add Parameters to an Outgoing Scope
80
+ ===================================
81
+
82
+ You can send parameters to your Twilio Application's :attr:`VoiceUrl` by passing
83
+ a hash to :attr:`#allow_client_outgoing`. Here we pass along a hypothetical user
84
+ id.
85
+
86
+ .. code-block:: ruby
87
+
88
+ application_sid = "APabe7650f654fc34655fc81ae71caa3ff"
89
+ params = {'user_id' => @user.id}
90
+
91
+ # Allow outgoing calls to an application and pass the user id to your server.
92
+ @capability.allow_client_outgoing(application_sid, params)
93
+
94
+ The :attr:`user_id` parameter and its value will be sent to your Application's
95
+ :attr:`VoiceUrl` along with the other parameters that Twilio usually sends, like
96
+ :attr:`From`, :attr:`To` and :attr:`CallSid`.
78
97
 
79
98
 
80
99
  Generate a Token
@@ -0,0 +1,86 @@
1
+ require 'rubygems'
2
+ require 'twilio-ruby'
3
+
4
+ # taskrouter examples version 1
5
+ account_sid = 'YourAccountSid'
6
+ auth_token = 'YourAuthToken'
7
+ workspace_sid = 'YourWorkspaceSid'
8
+
9
+ # set up a client
10
+ client = Twilio::REST::TaskRouterClient.new account_sid, auth_token, workspace_sid
11
+ puts client.workspace.sid + ' with ' + client.workspace.friendly_name
12
+
13
+ client.workspace.update(friendly_name: 'MyWorkspace2')
14
+
15
+ # fetch the list of workers and their status
16
+ client.workspace.workers.list.each do |worker|
17
+ puts worker.friendly_name + ' is available ' + worker.available.to_s
18
+ end
19
+
20
+ # fetch the list of workers and their status
21
+ client.workspace.workers.list(activity_name: 'Offline').each do |worker|
22
+ puts worker.friendly_name + ' is available ' + worker.available.to_s
23
+ end
24
+
25
+ # create a task
26
+ workflow_sid = 'YourWorkflowSid'
27
+ task = client.workspace.tasks.create(attributes: '{"foo":"bar"}', workflow_sid: workflow_sid)
28
+
29
+ # list all the tasks created
30
+ client.workspace.tasks.list.each do |task|
31
+ attributes = JSON.parse(task.attributes)
32
+ puts attributes['foo'] + ' with age ' + task.age.to_s
33
+ task.reservations.list.each do |reservation|
34
+ puts reservation.sid
35
+ end
36
+ end
37
+
38
+ # fetching a list of tasks that are pending
39
+ client.workspace.tasks.list(assignment_status: 'pending').each do |task|
40
+ puts 'pending task ' + task.sid
41
+ end
42
+
43
+ # delete all tasks
44
+ client.workspace.tasks.list.each do |task|
45
+ puts 'deleting task ' + task.sid
46
+ task.delete
47
+ end
48
+
49
+ # get all the statistics for a workspace
50
+ workspace_stats = client.workspace.statistics(minutes: '240')
51
+ cumulative = workspace_stats.cumulative
52
+ puts 'Avg Task Acceptance Time: ' + cumulative['avg_task_acceptance_time'].to_s \
53
+ + ' with ' + cumulative['tasks_created'].to_s + ' tasks created'
54
+
55
+ # get events for a workspace
56
+ events = client.workspace.events.list(minutes: '15')
57
+ events.each do |event|
58
+ puts event.event_type + ' at ' + event.event_date
59
+ end
60
+
61
+ # build a workflow
62
+ support_queue_sid = 'YourSupportQueueSid'
63
+ marketing_queue_sid = 'YourMarketingQueueSid'
64
+ default_queue_sid = 'YourDefaultQueue'
65
+
66
+ support_targets = [Twilio::TaskRouter::WorkflowRuleTarget.new support_queue_sid]
67
+ support_rule = Twilio::TaskRouter::WorkflowRule.new 'type == "support"', support_targets
68
+
69
+ marketing_targets = [Twilio::TaskRouter::WorkflowRuleTarget.new marketing_queue_sid]
70
+ marketing_rule = Twilio::TaskRouter::WorkflowRule.new 'type == "marketing"', marketing_targets
71
+
72
+ rules = [support_rule, marketing_rule]
73
+ default_target = Twilio::TaskRouter::WorkflowRuleTarget.new default_queue_sid
74
+
75
+ config = Twilio::TaskRouter::WorkflowConfiguration.new rules, default_target
76
+ puts config.to_json
77
+
78
+ workflowCreated = client.workspace.workflows.create(configuration: config.to_json, friendly_name: 'MyWorkflow2', assignment_callback_url: 'http://example.com')
79
+ workflowFetched = client.workspace.workflows.get(workflowCreated.sid)
80
+
81
+ config_string = workflowFetched.configuration
82
+ config_obj = Twilio::TaskRouter::WorkflowConfiguration.parse_json(config_string)
83
+
84
+ puts config_string
85
+ puts config_obj.rules[0].expression
86
+ puts config_obj.default_target.queue
data/lib/twilio-ruby.rb CHANGED
@@ -17,6 +17,7 @@ require 'twilio-ruby/util/capability'
17
17
  require 'twilio-ruby/twiml/response'
18
18
  require 'twilio-ruby/task_router'
19
19
  require 'twilio-ruby/task_router/capability'
20
+ require 'twilio-ruby/task_router/workflow_builder'
20
21
  require 'twilio-ruby/rest/errors'
21
22
  require 'twilio-ruby/rest/utils'
22
23
  require 'twilio-ruby/rest/list_resource'
@@ -7,6 +7,12 @@ module Twilio
7
7
 
8
8
  class Worker < InstanceResource
9
9
  include Twilio::REST::TaskRouter::Statistics
10
+
11
+ def initialize(path, client, params={})
12
+ super path, client, params
13
+ @submodule = :TaskRouter
14
+ resource :reservations
15
+ end
10
16
  end
11
17
  end
12
18
  end
@@ -0,0 +1,129 @@
1
+ require 'json'
2
+
3
+ module Twilio
4
+ module TaskRouter
5
+ class WorkflowConfiguration
6
+
7
+ def initialize(rules, default_target)
8
+ @rules = rules
9
+ @default_target = default_target
10
+ end
11
+
12
+ def rules
13
+ @rules
14
+ end
15
+
16
+ def default_target
17
+ @default_target
18
+ end
19
+
20
+ def to_json
21
+ filters = Array.new
22
+ @rules.each do |rule|
23
+ filters.push(rule.to_json)
24
+ end
25
+
26
+ task_routing = {filters: filters, default_filter: @default_target.to_json}
27
+ config = {task_routing: task_routing}
28
+ config.to_json
29
+ end
30
+
31
+ def self.parse_json(json_data)
32
+ task_routing = JSON.parse(json_data)['task_routing']
33
+
34
+ filters = task_routing['filters']
35
+ rules = Array.new
36
+ filters.each do |rule|
37
+ rules.push(Twilio::TaskRouter::WorkflowRule.parse_json(rule))
38
+ end
39
+
40
+ default_filter = Twilio::TaskRouter::WorkflowRuleTarget.parse_json(task_routing['default_filter'])
41
+ Twilio::TaskRouter::WorkflowConfiguration.new rules, default_filter
42
+ end
43
+ end
44
+ class WorkflowRule
45
+
46
+ def initialize(expression, targets, friendly_name=nil)
47
+ @expression = expression
48
+ @targets = targets
49
+ @friendly_name = friendly_name
50
+ end
51
+
52
+ def expression
53
+ @expression
54
+ end
55
+
56
+ def targets
57
+ @targets
58
+ end
59
+
60
+ def friendly_name
61
+ @friendly_name
62
+ end
63
+
64
+ def to_json
65
+ rule = {expression: @expression}
66
+ ruleTargets = Array.new
67
+ @targets.each do |target|
68
+ ruleTargets.push(target.to_json)
69
+ end
70
+ rule['targets'] = ruleTargets
71
+ unless @friendly_name.nil?
72
+ rule['friendly_name'] = @friendly_name
73
+ end
74
+ rule
75
+ end
76
+
77
+ def self.parse_json(rule)
78
+ targets = Array.new
79
+ rule['targets'].each do |target|
80
+ targets.push(Twilio::TaskRouter::WorkflowRuleTarget.parse_json(target))
81
+ end
82
+ Twilio::TaskRouter::WorkflowRule.new rule['expression'], targets, rule['friendly_name']
83
+ end
84
+ end
85
+ class WorkflowRuleTarget
86
+
87
+ def initialize(queue, priority=nil, timeout=nil, expression=nil)
88
+ @queue = queue
89
+ @priority = priority
90
+ @timeout = timeout
91
+ @expression = expression
92
+ end
93
+
94
+ def queue
95
+ @queue
96
+ end
97
+
98
+ def priority
99
+ @priority
100
+ end
101
+
102
+ def timeout
103
+ @timeout
104
+ end
105
+
106
+ def expression
107
+ @expression
108
+ end
109
+
110
+ def to_json
111
+ target = {queue: @queue}
112
+ unless @priority.nil?
113
+ target['priority'] = @priority
114
+ end
115
+ unless @timeout.nil?
116
+ target['timeout'] = @timeout
117
+ end
118
+ unless @expression.nil?
119
+ target['expression'] = @expression
120
+ end
121
+ target
122
+ end
123
+
124
+ def self.parse_json(target)
125
+ Twilio::TaskRouter::WorkflowRuleTarget.new target['queue'], target['priority'], target['timeout'], target['expression']
126
+ end
127
+ end
128
+ end
129
+ end
@@ -1,3 +1,3 @@
1
1
  module Twilio
2
- VERSION = '4.3.0'
2
+ VERSION = '4.4.0'
3
3
  end
@@ -6,4 +6,9 @@ describe Twilio::REST::TaskRouter::Reservations do
6
6
  expect(task).to respond_to(:reservations)
7
7
  expect(task.reservations.instance_variable_get('@path')).to eq('someUri/Reservations')
8
8
  end
9
+ it 'gets a reservation object' do
10
+ worker = Twilio::REST::TaskRouter::Worker.new('someUri', 'someClient')
11
+ expect(worker).to respond_to(:reservations)
12
+ expect(worker.reservations.instance_variable_get('@path')).to eq('someUri/Reservations')
13
+ end
9
14
  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: 4.3.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Benton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-11 00:00:00.000000000 Z
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -129,6 +129,7 @@ files:
129
129
  - docs/usage/validation.rst
130
130
  - examples/examples.rb
131
131
  - examples/print-call-log.rb
132
+ - examples/taskrouter.examples.rb
132
133
  - lib/rack/twilio_webhook_authentication.rb
133
134
  - lib/twilio-ruby.rb
134
135
  - lib/twilio-ruby/rest/accounts.rb
@@ -209,6 +210,7 @@ files:
209
210
  - lib/twilio-ruby/rest/utils.rb
210
211
  - lib/twilio-ruby/task_router.rb
211
212
  - lib/twilio-ruby/task_router/capability.rb
213
+ - lib/twilio-ruby/task_router/workflow_builder.rb
212
214
  - lib/twilio-ruby/twiml/response.rb
213
215
  - lib/twilio-ruby/util.rb
214
216
  - lib/twilio-ruby/util/capability.rb