-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
653 lines (619 loc) · 19.8 KB
/
app.rb
File metadata and controls
653 lines (619 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# # Continuity Control API Documentation
#
# This file is automatically generated from the code publically available on [GitHub](https://github.com/ContinuityControl/control_api_reference).
#
# ---
#
# This is a [Sinatra](http://www.sinatrarb.com/) application that integrates with the Control API. Sinatra is a small Ruby web application framework that provides a DSL (domain specific language) for handling HTTP requests like `get` and `post`. It should be easy to read even if you don't read Sinatra's documentation.
require 'sinatra'
require 'sinatra/reloader' if development?
require 'httparty'
require 'json'
require 'dotenv'
require 'byebug'
Dotenv.load
# This class uses a library called HTTParty to connect to the Control API. All API responses are JSON. `HTTParty` automatically detects this and parses into a Ruby object.
#
# For more information, see the [HTTParty](http://johnnunemaker.com/httparty/) website.
class ControlAPI
include HTTParty
base_uri ENV['CONTROL_API_BASE_URI']
basic_auth ENV['CONTROL_API_KEY'], ENV['CONTROL_API_SECRET']
# Set the 'Content-Type' header to 'application/json' to ensure that the API accepts and returns JSON. This is currently the only supported format, but this will ensure you still get JSON if the API supports XML in the future.
headers 'Content-Type' => 'application/json'
end
helpers do
def parsed_body
request.body.rewind
::JSON.parse(request.body.read)
end
end
# The root path in this application simply provides navigation.
#
# `erb :view_name` renders the file in `views/view_name.erb`. For example, this will end up rendering `views/root.erb`.
get '/' do
erb :root
end
# ## GET /v1/status
#
# Check the API status. Useful for testing authentication without knowing other information.
#
# ### Example request
#
# GET /v1/status
#
# ### Example response
#
# #### HTTP 200 OK
#
# {"description":"up"}
#
# #### HTTP 401 Unauthorized
#
# The request was not properly authenticated.
#
# #### HTTP 500 Server Error
#
# ### Response fields
#
# * `description`: A text description of the API state.
# * `error`: A text description of any error in the API call.
#
get '/status' do
status = ControlAPI.get('/v1/status.json').parsed_response
if status['error']
"API error: #{status['error']}"
else
"API status: #{status['description']}"
end
end
# ## GET /v1/users
#
# Get all users, optionally filtering by emails, external employee IDs, or manager emails
#
# ### Example requests
#
# GET /v1/users
# GET /v1/users?email[]=gwashington@example.com&gbush@example.com
# GET /v1/users?employee_id[]=1234
# GET /v1/users?manager_email[]=mwashington@example.com
#
# ### Example responses
#
# #### HTTP 200 OK
#
# {
# "users": [
# {
# // Content from GET /v1/users/:email
# 'email': 'gwashington@example.com',
# ...
# },
# {
# 'email': gbush@example.com',
# ...
# }
# ]
# }
#
# #### HTTP 500 Server Error
get '/users' do
users = ControlAPI.get('/v1/users', query: params)
erb :'users/index', locals: users.to_h
end
# ## POST /v1/users
#
# Create a new user and send an invitation for setting password, etc.
#
# ### Example requests
#
# POST /v1/users
# Content-Type: application/json
#
# {
# "user": {
# "email": "abe@whitehouse.gov",
# "first_name": "Abraham",
# "last_name": "Lincoln"
# }
# }
#
# ### Request fields
#
# The following fields are allowed:
#
# * `email`: **Required**
# * `first_name`: **Required**
# * `last_name`: **Required**
# * `manager_email`
# * `title`
# * `employee_id`
# * `review_on`
# * `started_on`
#
# For field descriptions, please see `GET /v1/users/:email`.
#
# ### Example responses
#
# #### HTTP 200 OK
#
# Refer to `GET /v1/users/:email` response.
#
# #### HTTP 422 Unprocessable Entity
#
# {
# "errors": {
# "email": [
# "is invalid",
# ]
# }
# }
#
# #### HTTP 500 Server Error
get '/users/new' do
erb :'users/new'
end
post '/users' do
user = ControlAPI.post('/v1/users', body: params.to_json)
case user.response.code
when '201'
[200, erb(:'users/created', locals: { email: user['email'] })]
when '422'
[422, erb(:errors, locals: { errors: user['errors'] })]
else
[500, 'There was an error while processing your request']
end
end
# ## GET /v1/users/:email
#
# Get an individual user by their email
#
# #### Data Example
#
# {
# "path": "/v1/users/gwashington@example.com",
# "email": "gwashington@example.com",
# "first_name": "George",
# "last_name": "Washington",
# "created_at": "1732-02-22T12:34:56Z",
# "updated_at": "1799-12-14T12:34:56Z",
# "review_on": "2076-07-04",
# "started_on": "1789-04-30",
# "manager_path": "/v1/users/mwashington@example.com",
# "manager_email": "mwashington@example.com",
# "title": "President of the United States",
# "administrator": true,
# "employee_id": "1",
# }
#
# #### Data Fields
#
# * `path`: API path for this user
# * `email`: Primary email
# * `first_name`: Personal name
# * `last_name`: Surname
# * `created_at`: ISO8601 datetime of the creation of this user, in UTC
# * `updated_at`: ISO8601 datetime of the time at which this user was updated, in UTC
# * `review_on`: The ISO8601 date for the next review of this user
# * `started_on`: The ISO8601 date when this user's employment started
# * `manager_path`: The API path for the user's manager
# * `manager_email`: The email address of the user's manager
# * `title`: The job title of the user
# * `administrator`: Whether or not the user has administrator access for their organization
# * `employee_id`: (string) This is the external employee ID of the user. The value is arbitrary and is assigned by their organization. However, it must be unique to their organization.
get '/users/:email' do
user = ControlAPI.get("/v1/users/#{params[:email]}")
case user.response.code
when '200'
erb :'users/show', locals: user.to_h
when '404'
[404, 'Not found']
else
[500, 'There was an error while processing your request']
end
end
#
# ## PATCH /v1/users
#
# Updates a user. Note that changing a user's email address will affect their identification in other API calls, as well as the email they use to log in.
#
# ### Example requests
#
# Refer to `POST /v1/users` request. NOTE: you can omit keys as necessary
#
# ### Example responses
#
# Refer to `GET /v1/users/:email` response
get '/users/:email/edit' do
user = ControlAPI.get("/v1/users/#{params[:email]}")
erb :'users/edit', locals: user.to_h
end
post '/users/:email' do
user = ControlAPI.patch("/v1/users/#{params[:email]}", body: params.to_json)
case user.response.code
when '200'
erb :'users/show', locals: user.to_h
when '422'
[422, erb(:errors, locals: { errors: user['errors'] })]
when '404'
[404, 'Not found']
else
[500, 'There was an error while processing your request']
end
end
#
# ## DELETE /v1/users/:email
#
# "Soft delete" the user by disabling them. Associated records, such as completed ToDos, are not deleted.
#
# ### Example responses
#
# #### HTTP 204 No Content
#
post '/users/:email/delete' do
user = ControlAPI.delete("/v1/users/#{params[:email]}")
case user.response.code
when '204'
[200, erb(:'users/deleted')]
when '422'
[422, erb(:errors, locals: { errors: user['errors'] })]
when '404'
[404, 'Not found']
else
[500, 'There was an error while processing your request']
end
end
# ## GET /v1/template_to_dos
#
# Get all the TemplateToDos for your organization. NOTE: not filtered by enabled/disabled state.
#
# ### Example requests
#
# GET /v1/template_to_dos # GET /v1/template_to_dos?tags[]=annual&tags[]=security
#
# ### Request fields
#
# * `tags`: Only return TemplateToDos that are tagged with all of the specified tags.
#
# ### Example response
#
# #### HTTP 200 OK
#
# {
# "template_to_dos": [
# {
# "uuid": "12345678-1234-5678-1234-567812345678",
# "name": "Review server logs",
# "tags": ["security", "annual", "training"]
# }
# ]
# }
#
# #### HTTP 500 Server Error
#
# ### Response fields
#
# * `template_to_dos`: an Array of TemplateToDos, or an empty array `[]` if none match the given criteria
# * `uuid`: UUID for each TemplateToDo.
# * `name`: The human-readable name for each ToDo.
# * `tags`: The tags for each TemplateToDo, as an array of strings. If there are no tags, it will be an empty array.
#
get '/template_to_dos' do
template_to_dos = ControlAPI.get("/v1/template_to_dos", :query => params)
erb :template_to_dos, :locals => template_to_dos
end
# ## POST /v1/distributed_to_dos
#
# **Asynchronously** distribute a ToDo to the given assignees. (The work happens in a job queue.)
#
# ### Example request
#
# POST /v1/distributed_to_dos
# Content-Type: application/json
#
# {
# "distributed_to_do": {
# "template_to_do_uuid": "12345678-1234-5678-1234-567812345678",
# "due_on": "2013-11-29",
# "assignee_emails": ["bobama@example.com", "gwbush@example.com", "bclinton@example.com"],
# "field_values": {"field1": "value1", "field2": "value2"},
# }
# }
#
# ### Request fields
#
# * `distributed_to_do`: **Required**. Holds parameters for the DistributedToDo.
# * `template_to_do_uuid`: **Required**. The UUID for the TemplateToDo that will be distributed. This can be found via `GET /v1/template_to_dos`, or in Continuity Control under "Settings".
# * `due_on`: **Required**. ISO8601 date of when the DistributedToDo is due, in your configured Time Zone.
# * `assignee_emails`: **Required**. An Array of email addresses of Users that will receive the DistributedToDos.
# * `field_values`: Dictionary (Object) of values to pre-fill in the DistributedToDo. Field names are available in Continuity Control under "Settings".
#
# ### Example responses
#
# #### HTTP 202 Accepted
#
# {
# "uuid":"f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
# "path":"/v1/distributed_to_dos/f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
# }
#
# #### HTTP 401 Unauthorized
#
# The request was not properly authenticated.
#
# #### HTTP 422 Unprocessable Entity
#
# {
# "errors": {
# "template_to_do_uuid": [
# "does not exist",
# ],
# "assignee_emails": [
# "has email alice@example.com which does not exist for this organization",
# "has email bob@example.com which does not exist for this organization"
# ],
# "due_on": [
# "could not be parsed"
# ]
# }
# }
#
# #### HTTP 500 Server Error
#
# ### Response fields
#
# * `uuid`: A UUID for the DistributedToDo which will be created asynchronously. Please include it in any bug reports to Continuity.
# * `path`: The path where the DistributedToDo will be available once created.
# * `errors`: An object with one key per request field and an array of all the validation errors that apply to that field. The exact text of the error messages **is not** guaranteed and may change without warning.
#
get '/distributed_to_dos/new' do
erb :distributed_to_dos_new
end
post '/distributed_to_dos' do
distributed_to_do = ControlAPI.post('/v1/distributed_to_dos',
:body => params.to_json)
case distributed_to_do.response.code
when '202'
[200, erb(:distributed_to_dos_post, :locals => distributed_to_do)]
when '422'
[422, erb(:errors, :locals => distributed_to_do)]
else
[500, 'There was an error while processing your request']
end
end
# ## GET /v1/distributed_to_dos/:uuid
#
# Get the current state of a distributed_to_do as found by an `uuid`.
#
# ### Example requests
#
# GET /v1/distributed_to_dos/f81d4fae-7dec-11d0-a765-00a0c91e6bf6
#
# ### Example response
#
# #### HTTP 200 OK
#
# {
# "uuid": "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
# "name": "Review server logs",
# "created_at": "2013-11-02T12:34:46.000Z",
# "completed_at": null,
# "due_on": "2013-11-08",
# "tags": ["security", "annual", "training"],
# "assignments": [
# {
# "email": "bobama@example.com",
# "finished_on": "2013-11-07",
# "fields": [
# {
# "uuid": "8edfc480-a174-0135-509d-1e3560338d6d",
# "todo_script_id": null,
# "label": "I was able to generate this report.",
# "value": true,
# "disabled": false
# }
# ]
# },
# {
# "email": "bclinton@example.com",
# "finished_on": null,
# "fields": [
# {
# "uuid": "7fcfc480-a174-0135-509d-1e3560338d6d",
# "todo_script_id": null,
# "label": "I was able to generate this report.",
# "value": null,
# "disabled": false
# }
# ]
# }
# ]
# }
#
# #### HTTP 401 Unauthorized
#
# The request was not properly authenticated.
#
# #### HTTP 404 Not Found
#
# Returned when the resource does not exist. Note that after a `POST` to `/v1/distributed_to_dos` that gives a `202`, `/v1/distributed_to_dos/:uuid` would return a `404` until the resource is **asynchronously** created.
#
# #### HTTP 500 Server Error
#
# ### Response fields
#
# * `uuid`: UUID for this DistributedToDo.
# * `name`: The human-readable name for this DistributedToDo.
# * `created_at`: ISO8601 datetime of the creation of this DistributedToDo, in UTC.
# * `completed_at`: ISO8601 datetime of when the DistributedToDo was completed, in UTC. This is when all the assignments have been finished. May be `null`.
# * `due_on`: ISO8601 date of when the DistributedToDo is due, in your configured Time Zone.
# * `tags`: The tags for this DistributedToDo, as an array of strings. If there are no tags, it will be an empty array.
# * `assignments`: Array
# * `email`: User email of DistributedToDo assignment
# * `finished_on`: ISO8601 date on which the assignment was completed (in UTC), or `null` if not completed
# * `fields`: Array
# * `uuid`: UUID for the Field
# * `todo_script_id`: Friendly reference id for the Field,
# * `label`: The human-readable name for the Field
# * `value`: The value submitted by the assignee
# * `disabled`: Boolean, if the Field is disabled it is not visible to the assignee
#
get '/distributed_to_dos/:uuid' do
distributed_to_do = ControlAPI.get("/v1/distributed_to_dos/#{params[:uuid]}")
case distributed_to_do.response.code
when '200'
erb :distributed_to_do, :locals => distributed_to_do.to_h # NOTE: this gives us locals that are keys in the distributed_to_do response
when '404'
[404, 'Not found']
else
[500, 'There was an error while processing your request']
end
end
# ## GET /v1/distributed_to_dos
#
# Get all the DistributedToDos for your organization. Each DistributedToDo in this "collection" GET request is in the same format as the "member" GET request.
#
# ### Example requests
#
# GET /v1/distributed_to_dos
# GET /v1/distributed_to_dos?created_after=2013-11-02
# GET /v1/distributed_to_dos?created_after=2013-11-02&created_before=2013-11-07
# GET /v1/distributed_to_dos?created_before=2013-11-07
# GET /v1/distributed_to_dos?late=true
# GET /v1/distributed_to_dos?complete=true
# GET /v1/distributed_to_dos?created_after=2013-11-02&created_before=2013-11-07&late=true&complete=false
# GET /v1/distributed_to_dos?tags[]=annual&tags[]=security
#
# ### Request fields
#
# * `created_after`: ISO8601 date of exclusive lower bound of `created_at` time.
# * `created_before`: ISO8601 date of exclusive upper bound of `created_at` time.
# * `late`: When `true`, respond with only "late" DistributedToDos. When `false`, respond with only "on time" DistributedToDos. When not present, do not filter on lateness.
# * `complete`: When `true`, respond with only "complete" DistributedToDos. When `false`, respond with only "incomplete" DistributedToDos. When not present, do not filter on completeness.
# * `tags`: Only return DistributedToDos that are tagged with all of the specified tags.
#
# `late` and `complete` can be combined to filter:
#
# late=false&complete=false # incomplete and on time
# late=false&complete=true # completed and on time
# late=true&complete=false # incomplete and late (what most users will want)
# late=true&complete=true # completed and late
#
# ### Example response
#
# #### HTTP 200 OK
#
# {
# "distributed_to_dos": [
# // Content from GET /v1/distributed_to_dos/:uuid
# ]
# }
#
# #### HTTP 500 Server Error
#
# ### Response fields
#
# * `distributed_to_dos`: an Array of DistributedToDos, or an empty array `[]` if none match the given criteria
#
get '/distributed_to_dos' do
one_week_ago_params = { :created_after => Date.today - 7 }
filter_params = params.empty? ? one_week_ago_params : params
distributed_to_dos = ControlAPI.get('/v1/distributed_to_dos', :query => filter_params)
erb :distributed_to_dos, :locals => distributed_to_dos
end
# ## Webhooks
#
# Webhooks provide information about events in near real-time. You provide a URL, and we'll `POST` to it as events take place. When a `HTTP 5XX` response occurs, the Webhook is retried with an incremental backoff.
#
# ### Format
#
# Our webhooks have the following format:
#
# {
#
# "event": "EventName",
# "fired_at": "2014-06-24T15:32:05Z",
# "data": {
# // Depends on event, see below
# }
# }
#
# #### Fields
#
# * `event`: A named event, as documented below
# * `fired_at`: When the webhook was fired
# * `data`: an Object of data for the given event
#
# ### Implementation Requirements
#
# The receiver of a webhook **MUST** check `event` on each `POST`. An unknown `event` **MUST** be ignored.
#
# Examples of why this behavior is required:
#
# * If the `event` is not checked, and the receiver only looks at the `data.name` attribute, it could be a ToDo's name instead of a user's name.
# * If the `event` is not checked, a user could have been updated instead of created. The receiver could then take action on an "updated" event that was only intended for a "created" event (e.g., sending a welcome email).
#
# ### Example
#
# For a contrived example, if there were a `VoteCast` event, the webhook would provide data like the following in two separate `POST`s:
#
# First `POST`:
#
# {
# "event": "VoteCast",
# "fired_at": "2000-11-07T15:32:05Z",
# "data": {
# "full_name": "Al Gore",
# "party": "Democrat"
# }
# }
#
# Second `POST`:
#
# {
# "event": "VoteCast",
# "fired_at": "2000-11-07T15:42:05Z",
# "data": {
# "full_name": "George W Bush",
# "party": "Republican"
# }
# }
#
# ## Events
#
# ### UserCreated
#
# The `UserCreated` event fires when a new user is created on Control.
#
# #### Data Example
#
# {
# "full_name": "George Washington",
# "first_name": "George",
# "last_name": "Washington",
# "email": "gwashington@example.com"
# }
#
# #### Data Fields
#
# * `full_name`: Formatted name, may be `null`
# * `first_name`: Personal name, may be `null`
# * `last_name`: Surname, may be `null`
# * `email`: Primary email
#
# ### DistributedToDoCompleted
#
# The `DistributedToDoCompleted` events fires when all the assignments of a ToDo are completed. That is, when the ToDo as a whole is completed, rather than an individual user's assignment.
#
# The data is the same for `GET /v1/distributed_to_dos/:uuid`. Please refer to its documentation.
#
post '/webhook' do
event = parsed_body['event']
data = parsed_body['data']
case event
when 'DistributedToDoCompleted'
"Good job, team! We completed the #{data['name']} ToDo!"
when 'UserCreated'
"Nice to meet you, #{data['full_name']}!"
end
end