notifications-ruby-client 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24ed09fc74d72afa42d20c723df34402b6d4d50d
4
- data.tar.gz: f99c8a7ee09d01734af27f95c32d76cecd416be8
3
+ metadata.gz: 2e9bb7eef63e23bb6ffd7f6f35c9659791a6501c
4
+ data.tar.gz: 540c6b9214bccc51b8c0e1ada26562b30103870d
5
5
  SHA512:
6
- metadata.gz: a2d80bf886dc3b2a846e23842e49401d28d06595eb7f734ec9849b67e8c9be0fee81ce1f88409eede2ba4dd43baced5381f29dbce59bd8bc0c859966d4f8a5f2
7
- data.tar.gz: 289479f21c94313ff77e24c5bc2e1906a9c7b71a8f7d02cc222593c8f6a872b51fdbf4039dd98fc1b3edd11baaa0c1420c9f0818f92e892242464e2118a923b4
6
+ metadata.gz: e297db99fcb11ecb4f7d88fe9670230c83fd92f891a30b3498407eb7b305d9a3333cabeec5b1413bf257d3df8e014cc035e2683b714411b0ca0ea526e8f13afe
7
+ data.tar.gz: c76a4aecc6513919f80895d45ecb8124f7c47d379120db61981059108b5cb9075ddb53f7c951a50813d7a15c2449d03e99f26b9b208957cfa743af5e77eedf88
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ##2.1.0
2
+
3
+ ###Changed
4
+ * Added methods to get templates and generate a preview of a template.
5
+ * `get_template_by_id` - get the latest version of a template by id.
6
+ * `get_template_version` - get the template by id and version.
7
+ * `get_all_templates` - get all templates, can be filtered by template type.
8
+ * `generate_template_preview` - get the contents of a template with the placeholders replaced with the given personalisation.
9
+ * See the README for more information about the new template methods.
10
+
11
+
1
12
  ##2.0.0
2
13
 
3
14
  ###Changed
data/Makefile CHANGED
@@ -38,7 +38,7 @@ prepare-docker-runner-image: ## Prepare the Docker builder image
38
38
  build-with-docker: prepare-docker-runner-image ## Build inside a Docker container
39
39
  docker run -i --rm \
40
40
  --name "${DOCKER_CONTAINER_PREFIX}-build" \
41
- -v `pwd`:/var/project \
41
+ -v "`pwd`:/var/project" \
42
42
  -e http_proxy="${HTTP_PROXY}" \
43
43
  -e HTTP_PROXY="${HTTP_PROXY}" \
44
44
  -e https_proxy="${HTTPS_PROXY}" \
@@ -51,7 +51,7 @@ build-with-docker: prepare-docker-runner-image ## Build inside a Docker containe
51
51
  test-with-docker: prepare-docker-runner-image generate-env-file ## Run tests inside a Docker container
52
52
  docker run -i --rm \
53
53
  --name "${DOCKER_CONTAINER_PREFIX}-test" \
54
- -v `pwd`:/var/project \
54
+ -v "`pwd`:/var/project" \
55
55
  -e http_proxy="${HTTP_PROXY}" \
56
56
  -e HTTP_PROXY="${HTTP_PROXY}" \
57
57
  -e https_proxy="${HTTPS_PROXY}" \
@@ -65,7 +65,7 @@ test-with-docker: prepare-docker-runner-image generate-env-file ## Run tests ins
65
65
  integration-test-with-docker: prepare-docker-runner-image generate-env-file ## Run integration tests inside a Docker container
66
66
  docker run -i --rm \
67
67
  --name "${DOCKER_CONTAINER_PREFIX}-integration-test" \
68
- -v `pwd`:/var/project \
68
+ -v "`pwd`:/var/project" \
69
69
  -e http_proxy="${HTTP_PROXY}" \
70
70
  -e HTTP_PROXY="${HTTP_PROXY}" \
71
71
  -e https_proxy="${HTTPS_PROXY}" \
data/README.md CHANGED
@@ -29,13 +29,15 @@ Text message:
29
29
 
30
30
  ```ruby
31
31
  require 'notifications/client/response_notification'
32
- sms = client.send_sms(phone_number: number,
33
- template_id: template_id,
34
- personalisation: Hash[name: "name",
35
- year: "2016",
36
- ],
37
- reference: "your_reference_string"
38
- ) # => Notifications::Client::ResponseNotification
32
+ sms = client.send_sms(
33
+ phone_number: number,
34
+ template_id: template_id,
35
+ personalisation: {
36
+ name: "name",
37
+ year: "2016",
38
+ }
39
+ reference: "your_reference_string"
40
+ ) # => Notifications::Client::ResponseNotification
39
41
  ```
40
42
 
41
43
  <details>
@@ -118,13 +120,15 @@ Email:
118
120
 
119
121
  ```ruby
120
122
  require 'notifications/client/response_notification'
121
- email = client.send_email(email_address: email_address,
122
- template: template_id,
123
- personalisation: Hash[name: "name",
124
- year: "2016"
125
- ],
126
- reference: "your_reference_string"
127
- ) # => Notifications::Client::ResponseNotification
123
+ email = client.send_email(
124
+ email_address: email_address,
125
+ template_id: template_id,
126
+ personalisation: {
127
+ name: "name",
128
+ year: "2016"
129
+ },
130
+ reference: "your_reference_string"
131
+ ) # => Notifications::Client::ResponseNotification
128
132
  ```
129
133
 
130
134
  <details>
@@ -139,7 +143,7 @@ email => Notifications::Client::ResponseNotification
139
143
 
140
144
  email.id # => uuid for the notification
141
145
  email.reference # => Reference string you supplied in the request
142
- email.type # => sms
146
+ email.type # => email
143
147
  email.status # => status of the message "created|pending|sent|delivered|permanent-failure|temporary-failure"
144
148
  email.content # => Hash containing body => the message sent to the recipient, with placeholders replaced.
145
149
  # subject => subject of the message sent to the recipient, with placeholders replaced.
@@ -222,10 +226,10 @@ You can omit this argument if you do not require a reference for the notificatio
222
226
  If the template has placeholders you need to provide their values as a Hash, for example:
223
227
 
224
228
  ```ruby
225
- personalisation=Hash[
226
- 'first_name': 'Amala',
227
- 'reference_number': '300241',
228
- ]
229
+ personalisation: {
230
+ 'first_name' => 'Amala',
231
+ 'reference_number' => '300241',
232
+ }
229
233
  ```
230
234
 
231
235
  You can omit this argument if the template does not contain placeholders.
@@ -306,12 +310,12 @@ Otherwise a `Notification::Client::RequestError` is raised
306
310
 
307
311
  ```ruby
308
312
  # See section below for a description of the arguments.
309
- args = Hash[
310
- 'template_type', 'sms',
311
- 'status', 'failed',
312
- 'reference', 'your reference string'
313
- 'olderThanId', 'e194efd1-c34d-49c9-9915-e4267e01e92e' # => Notifications::Client::Notification
314
- ]
313
+ args = {
314
+ 'template_type' => 'sms',
315
+ 'status' => 'failed',
316
+ 'reference' => 'your reference string'
317
+ 'olderThanId' => 'e194efd1-c34d-49c9-9915-e4267e01e92e' # => Notifications::Client::Notification
318
+ }
315
319
  notifications = client.get_notifications(args)
316
320
  ```
317
321
  <details>
@@ -321,10 +325,11 @@ Response
321
325
  If the request is successful a `Notifications::Client::NotificationsCollection` is returned.
322
326
 
323
327
  ```ruby
324
- notifications.links # => Hash containing current=>"/notifications?template_type=sms&status=delivered"
325
- next=>"/notifications?other_than=last_id_in_list&template_type=sms&status=delivered"}
328
+ notifications.links # => Hash containing current => "/notifications?template_type=sms&status=delivered"
329
+ # next => "/notifications?other_than=last_id_in_list&template_type=sms&status=delivered"
326
330
  notifications.collection # => [] (array of notification objects)
327
331
  ```
332
+
328
333
  Otherwise the client will raise a `Notifications::Client::RequestError`:
329
334
  <table>
330
335
  <thead>
@@ -341,7 +346,7 @@ Otherwise the client will raise a `Notifications::Client::RequestError`:
341
346
  <td>
342
347
  <pre>
343
348
  [{
344
- 'error': 'ValidationError',
349
+ 'error': 'ValidationError',
345
350
  'message': 'bad status is not one of [created, sending, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure]'
346
351
  }]
347
352
  </pre>
@@ -399,3 +404,233 @@ You can omit this argument to ignore the filter.
399
404
  #### `olderThanId`
400
405
  You can get the notifications older than a given `Notification.id`.
401
406
  You can omit this argument to ignore this filter.
407
+
408
+
409
+ ## Get a template by ID
410
+ This will return the latest version of the template. Use [getTemplateVersion](#get-a-template-by-id-and-version) to retrieve a specific template version.
411
+
412
+ ```ruby
413
+ template = client.get_template_by_id(template_id)
414
+ ```
415
+
416
+ <details>
417
+ <summary>
418
+ Response
419
+ </summary>
420
+
421
+ ```Ruby
422
+ template.id # => uuid for the template
423
+ template.type # => type of template one of email|sms|letter
424
+ template.created_at # => date and time the template was created
425
+ template.updated_at # => date and time the template was last updated, may be null if version 1
426
+ template.created_by # => email address of the person that created the template
427
+ template.version # => version of the template
428
+ template.body # => content of the template
429
+ template.subject # => subject for email templates, will be empty for other template types
430
+ ```
431
+
432
+ Otherwise the client will raise a `Notifications::Client::RequestError`.
433
+
434
+ <table>
435
+ <thead>
436
+ <tr>
437
+ <th>message</th>
438
+ </tr>
439
+ </thead>
440
+ <tbody>
441
+ <tr>
442
+ <td>
443
+ <pre>
444
+ Status code: 404 {
445
+ "errors":
446
+ [{
447
+ "error": "NoResultFound",
448
+ "message": "No result found"
449
+ }]
450
+ }
451
+ </pre>
452
+ <pre>
453
+ Status code: 400 {
454
+ "errors":
455
+ [{
456
+ "error": "ValidationError",
457
+ "message": "id is not a valid UUID"
458
+ }]
459
+ }
460
+ </pre>
461
+ </tbody>
462
+ </table>
463
+ </details>
464
+
465
+ ### Arguments
466
+
467
+ #### `templateId`
468
+ The template id is visible on the template page in the application.
469
+
470
+
471
+ ## Get a template by ID and version
472
+ This will return the template for the given id and version.
473
+
474
+ ```ruby
475
+ Template template = client.get_template_version(template_id template_id, version)
476
+ ```
477
+
478
+ <details>
479
+ <summary>
480
+ Response
481
+ </summary>
482
+
483
+ ```Ruby
484
+ template.id # => uuid for the template
485
+ template.type # => type of template one of email|sms|letter
486
+ template.created_at # => date and time the template was created
487
+ template.updated_at # => date and time the template was last updated, may be null if version 1
488
+ template.created_by # => email address of the person that created the template
489
+ template.version # => version of the template
490
+ template.body # => content of the template
491
+ template.subject # => subject for email templates, will be empty for other template types
492
+ ```
493
+
494
+ Otherwise the client will raise a `Notifications::Client::RequestError`.
495
+
496
+ <table>
497
+ <thead>
498
+ <tr>
499
+ <th>message</th>
500
+ </tr>
501
+ </thead>
502
+ <tbody>
503
+ <tr>
504
+ <td>
505
+ <pre>
506
+ Status code: 404 {
507
+ "errors":
508
+ [{
509
+ "error": "NoResultFound",
510
+ "message": "No result found"
511
+ }]
512
+ }
513
+ </pre>
514
+ <pre>
515
+ Status code: 400 {
516
+ "errors":
517
+ [{
518
+ "error": "ValidationError",
519
+ "message": "id is not a valid UUID"
520
+ }]
521
+ }
522
+ </pre>
523
+ </tbody>
524
+ </table>
525
+ </details>
526
+
527
+ ### Arguments
528
+
529
+ #### `templateId`
530
+ The template id is visible on the template page in the application.
531
+
532
+ #### `version`
533
+ A history of the template is kept. There is a link to `See previous versions` on the template page in the application.
534
+
535
+ ## Get all templates
536
+ This will return the latest version of each template for your service.
537
+
538
+ ```ruby
539
+ args = {
540
+ 'template_type' => 'sms'
541
+ }
542
+ templates = client.get_all_templates(args)
543
+ ```
544
+
545
+ <details>
546
+ <summary>
547
+ Response
548
+ </summary>
549
+
550
+ ```ruby
551
+ TemplateCollection templates;
552
+ ```
553
+ If the response is successful, a TemplateCollection is returned.
554
+
555
+ If no templates exist for a template type or there no templates for a service, the templates list will be empty.
556
+
557
+ Otherwise the client will raise a `Notifications::Client::RequestError`.
558
+
559
+
560
+ </details>
561
+
562
+ ### Arguments
563
+
564
+ #### `templateType`
565
+ You can filter the templates by the following options:
566
+
567
+ * `email`
568
+ * `sms`
569
+ * `letter`
570
+ You can omit this argument to ignore this filter.
571
+
572
+
573
+ ## Generate a preview template
574
+ This will return the contents of a template with the placeholders replaced with the given personalisation.
575
+ ```ruby
576
+ templatePreview = client.generate_template_preview(template_id: template_id,
577
+ personalisation: {
578
+ name: "name",
579
+ year: "2016",
580
+ })
581
+ ```
582
+
583
+ <details>
584
+ <summary>
585
+ Response
586
+ </summary>
587
+
588
+
589
+ ```Ruby
590
+ template.id # => uuid for the template
591
+ template.version # => version of the template
592
+ template.body # => content of the template
593
+ template.subject # => subject for email templates, will be empty for other template types
594
+ ```
595
+
596
+
597
+ Otherwise a `Notifications::Client::RequestError` is thrown.
598
+ <table>
599
+ <thead>
600
+ <tr>
601
+ <th>message</th>
602
+ </tr>
603
+ </thead>
604
+ <tbody>
605
+ <tr>
606
+ <td>
607
+ <pre>
608
+ Status code: 404 {
609
+ "errors":
610
+ [{
611
+ "error": "NoResultFound",
612
+ "message": "No result found"
613
+ }]
614
+ }
615
+ </pre>
616
+ <pre>
617
+ Status code: 400 {
618
+ "errors":
619
+ [{
620
+ "error": "ValidationError",
621
+ "message": "id is not a valid UUID"
622
+ }]
623
+ }
624
+ </pre>
625
+ </tbody>
626
+ </table>
627
+
628
+ </details>
629
+
630
+ ### Arguments
631
+
632
+ #### `templateId`
633
+ The template id is visible on the template page in the application.
634
+
635
+ #### `personalisation`
636
+ If a template has placeholders, you need to provide their values. `personalisation` can be an empty or null in which case no placeholders are provided for the notification.
data/bin/test_client.rb CHANGED
@@ -3,18 +3,92 @@ require 'notifications/client'
3
3
  require 'notifications/client/notification'
4
4
  require 'notifications/client/response_notification'
5
5
  require 'notifications/client/notification'
6
+ require 'notifications/client/response_template'
7
+ require 'notifications/client/template_collection'
6
8
 
7
9
  def main
8
10
  client = Notifications::Client.new(ENV['API_KEY'], ENV['NOTIFY_API_URL'])
11
+ test_get_template_by_id(client, ENV['EMAIL_TEMPLATE_ID'])
12
+ test_get_template_version(client, ENV['SMS_TEMPLATE_ID'], 1)
13
+ test_get_all_templates(client)
14
+ test_get_all_templates_filter_by_type(client)
15
+ test_generate_template_preview(client, ENV['EMAIL_TEMPLATE_ID'])
9
16
  email_notification = test_send_email_endpoint(client)
10
17
  sms_notification = test_send_sms_endpoint(client)
11
18
  test_get_notification_by_id_endpoint(client, email_notification.id, 'email')
12
19
  test_get_notification_by_id_endpoint(client, sms_notification.id, 'sms')
13
- test_get_all_notifications(client, sms_notification.id, email_notification.id)
20
+ test_get_all_notifications(client)
14
21
  p 'ruby client integration tests pass'
15
22
  exit 0
16
23
  end
17
24
 
25
+ def test_get_template_by_id(client, id)
26
+ response = client.get_template_by_id(id)
27
+ test_template_response(response, 'test_get_template_by_id')
28
+ end
29
+
30
+ def test_get_template_version(client, id, version)
31
+ response = client.get_template_version(id, version)
32
+ test_template_response(response, 'test_get_template_version')
33
+ end
34
+
35
+ def test_get_all_templates(client)
36
+ response = client.get_all_templates()
37
+ unless response.is_a?(Notifications::Client::TemplateCollection) then
38
+ p 'failed test_get_all_templates response is not a Notifications::Client::TemplateCollection'
39
+ exit 1
40
+ end
41
+ unless response.collection.length >= 2 then
42
+ p 'failed test_get_all_templates, expected at least 2 templates returned.'
43
+ exit 1
44
+ end
45
+ test_template_response(response.collection[0], 'test_get_all_templates')
46
+ test_template_response(response.collection[1], 'test_get_all_templates')
47
+ end
48
+
49
+ def test_get_all_templates_filter_by_type(client)
50
+ response = client.get_all_templates({'type' => 'sms'})
51
+ unless response.is_a?(Notifications::Client::TemplateCollection) then
52
+ p 'failed test_get_all_templates response is not a Notifications::Client::TemplateCollection'
53
+ exit 1
54
+ end
55
+ unless response.collection.length >= 1 then
56
+ p 'failed test_get_all_templates, expected at least 2 templates returned.'
57
+ exit 1
58
+ end
59
+ test_template_response(response.collection[0], 'test_get_all_templates')
60
+ end
61
+
62
+ def test_generate_template_preview(client, id)
63
+
64
+ response = client.generate_template_preview(id, personalisation:Hash["name", "some name"])
65
+ test_template_preview(response)
66
+ end
67
+
68
+ def test_template_response(response, test_method)
69
+ unless response.is_a?(Notifications::Client::Template) then
70
+ p 'failed test_get_template_by_id response is not a Notifications::Client::Template'
71
+ exit 1
72
+ end
73
+ unless response.id.is_a?(String) then
74
+ p 'failed template id is not a String'
75
+ exit 1
76
+ end
77
+ field_should_not_be_nil(expected_fields_in_template_response, response, test_method)
78
+ end
79
+
80
+ def test_template_preview(response)
81
+ unless response.is_a?(Notifications::Client::TemplatePreview) then
82
+ p 'failed test_generate_template_preview response is not a Notifications::Client::TemplatePreview'
83
+ exit 1
84
+ end
85
+ unless response.id.is_a?(String) then
86
+ p 'failed template id is not a String'
87
+ exit 1
88
+ end
89
+ field_should_not_be_nil(expected_fields_in_template_preview, response, 'generate_template_preview')
90
+ end
91
+
18
92
  def test_send_email_endpoint(client)
19
93
  email_resp = client.send_email(email_address: ENV['FUNCTIONAL_TEST_EMAIL'], template_id: ENV['EMAIL_TEMPLATE_ID'],
20
94
  personalisation:Hash["name", "some name"],
@@ -52,7 +126,8 @@ def test_notification_response_data_type(notification, message_type)
52
126
  end
53
127
 
54
128
  def test_get_notification_by_id_endpoint(client, id, message_type)
55
- get_notification_response = get_notification_for_id(client, id, message_type)
129
+ get_notification_response = client.get_notification(id)
130
+
56
131
  unless get_notification_response.is_a?(Notifications::Client::Notification) then
57
132
  p 'get notification is not a Notifications::Client::Notification for id ' + id
58
133
  exit 1
@@ -71,35 +146,6 @@ def test_get_notification_by_id_endpoint(client, id, message_type)
71
146
  end
72
147
 
73
148
 
74
- def get_notification_for_id(client, id, message_type)
75
- max_attempts = 0
76
- wait_for_sent_message = true
77
- while max_attempts < 3 && wait_for_sent_message
78
- begin
79
- get_notification_response = client.get_notification(id)
80
- wait_for_sent_message = false
81
- rescue Notifications::Client::RequestError => no_result
82
- if no_result.message == "No result found"
83
- max_attempts = max_attempts + 1
84
- sleep 3
85
- else
86
- p 'get_notification threw an exception for the ' + message_type + ' notification'
87
- p no_result.to_s
88
- exit 1
89
- end
90
- end
91
- if get_notification_response != nil && get_notification_response.send(:'status') == 'created'
92
- # we want to test the sent response?
93
- wait_for_sent_message = true
94
- end
95
- end
96
- if max_attempts == 3 then
97
- p 'get_notification failed because the ' + message_type + ' notification was not found'
98
- exit 1
99
- end
100
- get_notification_response
101
- end
102
-
103
149
  def hash_key_should_not_be_nil(fields, obj, method_name)
104
150
  fields.each do |field|
105
151
  if obj.has_value?(:"#{field}") then
@@ -127,6 +173,23 @@ def field_should_be_nil(fields, obj, method_name)
127
173
  end
128
174
  end
129
175
 
176
+ def expected_fields_in_template_response
177
+ %w(id
178
+ type
179
+ created_at
180
+ created_by
181
+ body
182
+ version
183
+ )
184
+ end
185
+
186
+ def expected_fields_in_template_preview
187
+ %w(id
188
+ body
189
+ version
190
+ type
191
+ )
192
+ end
130
193
 
131
194
  def expected_fields_in_notification_response
132
195
  %w(id
@@ -151,11 +214,9 @@ end
151
214
 
152
215
  def expected_fields_in_email_notification
153
216
  %w(id
154
- reference
155
217
  email_address
156
218
  type
157
219
  status
158
- sent_at
159
220
  template
160
221
  body
161
222
  subject
@@ -181,7 +242,6 @@ def expected_fields_in_sms_notification
181
242
  phone_number
182
243
  type
183
244
  status
184
- sent_at
185
245
  template
186
246
  body
187
247
  created_at
@@ -207,24 +267,13 @@ def expected_fields_in_template
207
267
  uri)
208
268
  end
209
269
 
210
- def test_get_all_notifications(client, first_id, second_id)
270
+ def test_get_all_notifications(client)
211
271
  notifications = client.get_notifications()
212
272
  unless notifications.is_a?(Notifications::Client::NotificationsCollection) then
213
273
  p 'get all notifications is not Notifications::Client::NotificationsCollection'
214
274
  exit 1
215
275
  end
216
-
217
276
  field_should_not_be_nil(expected_fields_for_get_all_notifications, notifications, 'get_notifications')
218
-
219
- notification_collection = notifications.send(:'collection')
220
- unless notification_collection[0].id == first_id then
221
- p 'first item in notification_collection is not the expected notification, last message sent'
222
- exit 0
223
- end
224
- unless notification_collection[1].id == second_id then
225
- p 'second item in notification_collection is not the expected notification, second last message sent'
226
- exit 0
227
- end
228
277
  end
229
278
 
230
279
  def expected_fields_for_get_all_notifications
data/docker/Makefile CHANGED
@@ -7,8 +7,8 @@ help:
7
7
 
8
8
  .PHONY: build
9
9
  build:
10
+ docker pull `grep "FROM " Dockerfile | cut -d ' ' -f 2` || true
10
11
  docker build \
11
- --pull \
12
12
  --build-arg HTTP_PROXY="${HTTP_PROXY}" \
13
13
  --build-arg HTTPS_PROXY="${HTTP_PROXY}" \
14
14
  --build-arg NO_PROXY="${NO_PROXY}" \
@@ -3,6 +3,9 @@ require "notifications/client/speaker"
3
3
  require "notifications/client/notification"
4
4
  require "notifications/client/response_notification"
5
5
  require "notifications/client/notifications_collection"
6
+ require "notifications/client/response_template"
7
+ require "notifications/client/template_collection"
8
+ require "notifications/client/template_preview"
6
9
  require "forwardable"
7
10
 
8
11
  module Notifications
@@ -66,5 +69,49 @@ module Notifications
66
69
  speaker.get(nil, options)
67
70
  )
68
71
  end
72
+
73
+ ##
74
+ # @param id [String]
75
+ # @return [Template]
76
+ def get_template_by_id(id, options = {})
77
+ path = "/v2/template/" << id
78
+ Template.new(
79
+ speaker.get_with_url(path, options)
80
+ )
81
+ end
82
+
83
+ ##
84
+ # @param id [String]
85
+ # @param version [int]
86
+ # @return [Template]
87
+ def get_template_version(id, version, options = {})
88
+ path = "/v2/template/" << id << "/version/" << version.to_s
89
+ Template.new(
90
+ speaker.get_with_url(path, options)
91
+ )
92
+ end
93
+
94
+ ##
95
+ # @option options [String] :type
96
+ # email, sms, letter
97
+ # @return [TemplateCollection]
98
+ def get_all_templates(options = {})
99
+ path = "/v2/templates"
100
+ TemplateCollection.new(
101
+ speaker.get_with_url(path, options)
102
+ )
103
+ end
104
+
105
+ ##
106
+ # @param options [String]
107
+ # @option personalisation [Hash]
108
+ # @return [TemplatePreview]
109
+ def generate_template_preview(id, options = {})
110
+ path = "/v2/template/" << id << "/preview"
111
+ TemplatePreview.new(
112
+ speaker.post_with_url(path, options)
113
+ )
114
+ end
115
+
69
116
  end
70
117
  end
@@ -0,0 +1,41 @@
1
+ module Notifications
2
+ class Client
3
+ class Template
4
+ FIELDS = [
5
+ :id,
6
+ :type,
7
+ :created_at,
8
+ :updated_at,
9
+ :created_by,
10
+ :version,
11
+ :body,
12
+ :subject
13
+ ].freeze
14
+
15
+ attr_reader(*FIELDS)
16
+
17
+ def initialize(notification)
18
+
19
+ FIELDS.each do |field|
20
+ instance_variable_set(:"@#{field}", notification.fetch(field.to_s, nil)
21
+ )
22
+ end
23
+ end
24
+
25
+ [
26
+ :created_at,
27
+ :updated_at
28
+ ].each do |field|
29
+ define_method field do
30
+ begin
31
+ value = instance_variable_get(:"@#{field}")
32
+ Time.parse value
33
+ rescue
34
+ value
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -60,6 +60,35 @@ module Notifications
60
60
  perform_request!(request)
61
61
  end
62
62
 
63
+ ##
64
+ # @param url path of endpoint
65
+ # @param id [String]
66
+ # @param options [Hash] query
67
+ # @see #perform_request!
68
+ def get_with_url(url, options = {})
69
+ path = url
70
+ path << "?" << URI.encode_www_form(options) if options.any?
71
+ request = Net::HTTP::Get.new(path, headers)
72
+ perform_request!(request)
73
+ end
74
+
75
+ ##
76
+ # @param url [String] path of the endpoint
77
+ # @param form_data [Hash]
78
+ # @option form_data [String] :template_id
79
+ # id of the template to render
80
+ # @option form_data [Hash] :personalisation
81
+ # fields to use in the template
82
+ # @see #perform_request!
83
+ def post_with_url(url, form_data)
84
+ request = Net::HTTP::Post.new(
85
+ url,
86
+ headers
87
+ )
88
+ request.body = form_data.is_a?(Hash) ? form_data.to_json : form_data
89
+ perform_request!(request)
90
+ end
91
+
63
92
  private
64
93
 
65
94
  ##
@@ -0,0 +1,16 @@
1
+ module Notifications
2
+ class Client
3
+ class TemplateCollection
4
+ attr_reader :collection
5
+ def initialize(response)
6
+ @collection = collection_from(response["templates"])
7
+ end
8
+
9
+ def collection_from(templates)
10
+ templates.map do |template|
11
+ Template.new(template)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Notifications
2
+ class Client
3
+ class TemplatePreview
4
+ FIELDS = [
5
+ :id,
6
+ :version,
7
+ :body,
8
+ :subject,
9
+ :type
10
+ ].freeze
11
+
12
+ attr_reader(*FIELDS)
13
+
14
+ def initialize(notification)
15
+
16
+ FIELDS.each do |field|
17
+ instance_variable_set(:"@#{field}", notification.fetch(field.to_s, nil)
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module Notifications
2
2
  class Client
3
- VERSION = "2.0.0".freeze
3
+ VERSION = "2.1.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifications-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Government Digital Service
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-06 00:00:00.000000000 Z
11
+ date: 2017-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -134,7 +134,10 @@ files:
134
134
  - lib/notifications/client/notifications_collection.rb
135
135
  - lib/notifications/client/request_error.rb
136
136
  - lib/notifications/client/response_notification.rb
137
+ - lib/notifications/client/response_template.rb
137
138
  - lib/notifications/client/speaker.rb
139
+ - lib/notifications/client/template_collection.rb
140
+ - lib/notifications/client/template_preview.rb
138
141
  - lib/notifications/client/version.rb
139
142
  - notifications-ruby-client.gemspec
140
143
  homepage: https://github.com/alphagov/notifications-ruby-client