notifications-ruby-client 2.8.0 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7ebfa7945a861630914de5251f291bb5fac91f6d594b819364ca745072e6129
4
- data.tar.gz: 8374e982bd9a519d85446dae5a4cb4808308ba80443f431ec1841f7ddbea54cb
3
+ metadata.gz: 0ee6c91540f71825cf6b96f6eb7e170383c2841bdc499bf45b625c2b0d48525c
4
+ data.tar.gz: eec0b7042a42330d7ab776eb39815928359a07ac732504738a64db5f2f06bf54
5
5
  SHA512:
6
- metadata.gz: eb25a50ca0b7b0571f6908711ede2a3212fad478f7243538a88acc8c6bd7254eec1d670ba54dd6953281bf353d85b94ca05ecd99aa9de89bb4386306c74bdb18
7
- data.tar.gz: 1e94b34627e331bb61e4e549efec4b8a6a50318665d3639b75b021f0d5b080f40ec7d73507d21b73c87db686d4f934016287ab65fd1ed0c0a92280eca91571d8
6
+ metadata.gz: e310f4d25c85ac58d741076fd586b0b6720e49f916ad500d47dbc781dc0e5023375231f1b8ff121523b0a154b34bf00ef1f8a27b3e62347c8b0fb6402889f04e
7
+ data.tar.gz: 97c23e6f67ccee843ae3534bd209e7a295322c87c43381109291bd8b78dc1b7dbe4f9b86d465ecfd1b51fc05be49c3af93c64acca9b786b6a2f6d80c4088df2d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 2.9.0
2
+
3
+ * Added the `send_precompiled_letter` method which allows the client to send letters as PDF files.
4
+ * This requires two arguments - a reference for the letter and the PDF letter file. The file must conform to the Notify printing template.
5
+ * Added support for document uploads using the `send_email` method.
6
+
1
7
  ## 2.8.0
2
8
 
3
9
  * Updated the Template class to have a `name` property, which is the name of the template as set in Notify.
data/DOCUMENTATION.md CHANGED
@@ -207,6 +207,36 @@ email_reply_to_id: '8e222534-7f05-4972-86e3-17c5d9f894e2'
207
207
 
208
208
  You can leave out this argument if your service only has one email reply-to address, or you want to use the default email address.
209
209
 
210
+ ### Send a document by email
211
+ Send files without the need for email attachments.
212
+
213
+ To send a document by email, add a placeholder field to the template then upload a file. The placeholder field will contain a secure link to download the document.
214
+
215
+ [Contact the GOV.UK Notify team](https://www.notifications.service.gov.uk/support) to enable this function for your service.
216
+
217
+ #### Add a placeholder field to the template
218
+
219
+ In Notify, use double brackets to add a placeholder field to the email template. For example:
220
+
221
+ "Download your document at: ((link_to_document))"
222
+
223
+ #### Upload your document
224
+
225
+ The document you upload must be a PDF file smaller than 2MB.
226
+
227
+ Pass the file object as an argument to the `Notifications.prepare_upload` helper method. Then pass the result into the personalisation argument. For example:
228
+
229
+ ```ruby
230
+ File.open("file.pdf", "rb") do |f|
231
+ ...
232
+ personalisation: {
233
+ first_name: "Amala",
234
+ application_date: "2018-01-01",
235
+ link_to_document: Notifications.prepare_upload(f),
236
+ }
237
+ end
238
+ ```
239
+
210
240
  ### Response
211
241
 
212
242
  If the request to the client is successful, the client returns a `Notifications::Client:ResponseNotification` object. In the example shown in the [Method section](/ruby.html#send-an-email-method), the object is named `emailresponse`.
@@ -229,11 +259,14 @@ If the request is not successful, the client returns a `Notifications::Client::R
229
259
  |:--- |:---|:---|
230
260
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient using a team-only API key"`<br>`]}`|Use the correct type of [API key](/ruby.html#api-keys)|
231
261
  |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
262
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported document type '{}'. Supported types are: {}"`<br>`}]`|The document you upload must be a PDF file|
263
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Document didn't pass the virus scan"`<br>`}]`|The document you upload must be virus free|
232
264
  |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
233
265
  |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: signature, api token not found"`<br>`}]`|Use the correct API key. Refer to [API keys](/ruby.html#api-keys) for more information|
234
266
  |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](/ruby.html#api-rate-limits) for more information|
235
267
  |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](/ruby.html#service-limits) for the limit number|
236
268
  |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification|
269
+ |-|`[{`<br>`"error": "ArgumentError",`<br>`"message": "Document is larger than 2MB")`<br>`}]`|Document size was too large, upload a smaller document|
237
270
 
238
271
  ## Send a letter
239
272
 
@@ -338,6 +371,56 @@ If the request is not successful, the client returns a `Notifications::Client::R
338
371
  |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](/ruby.html#service-limits) for the limit number|
339
372
  |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification|
340
373
 
374
+ ## Send a pre-compiled letter
375
+ This is an invitation-only feature. Contact the GOV.UK Notify team on the [support page](https://www.notifications.service.gov.uk/support) or through the [Slack channel](https://ukgovernmentdigital.slack.com/messages/govuk-notify) for more information.
376
+
377
+ ### Method
378
+ ```ruby
379
+ precompiled_letter = client.send_precompiled_letter(reference, pdf_file)
380
+ ```
381
+
382
+ ### Arguments
383
+
384
+ #### reference (required)
385
+ A unique identifier you create. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address.
386
+
387
+ #### pdf_file (required)
388
+ The pre-compiled letter must be a PDF file.
389
+
390
+ ```ruby
391
+ File.open("path/to/pdf_file", "rb") do |pdf_file|
392
+ client.send_precompiled_letter("your reference", pdf_file)
393
+ end
394
+ ```
395
+
396
+ ### Response
397
+
398
+ If the request to the client is successful, the client returns a `Notifications::Client:ResponseNotification` object. In the example shown in the [Method section](/ruby.html#send-a-pre-compiled-letter-method), the object is named `precompiled_letter`.
399
+
400
+ You can then call different methods on this object to return the requested information.
401
+
402
+ |Method|Information|Type|
403
+ |:---|:---|:---|
404
+ |`precompiled_letter.id`|Notification UUID|String|
405
+ |`precompiled_letter.reference`|`reference` argument|String|
406
+ |`precompiled_letter.content`|Always `nil`|nil|
407
+ |`precompiled_letter.template`|Always `nil`|nil|
408
+ |`precompiled_letter.uri`|Always `nil`|nil|
409
+
410
+ ### Error codes
411
+
412
+ If the request is not successful, the client returns a `Notifications::Client::RequestError` and an error code.
413
+
414
+ |error.status_code|error.message|How to fix|
415
+ |:---|:---|:---|
416
+ |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type live of 10 requests per 20 seconds"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
417
+ |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (50) for today"`<br>`}]`|Refer to [service limits](#service-limits) for the limit number|
418
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters with a team api key"`<br>`]}`|Use the correct type of [API key](#api-keys)|
419
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send precompiled letters"`<br>`]}`|This is an invitation-only feature. Contact the GOV.UK Notify team on the [support page](https://www.notifications.service.gov.uk/support) or through the [Slack channel](https://ukgovernmentdigital.slack.com/messages/govuk-notify) for more information|
420
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Letter content is not a valid PDF"`<br>`]}`|PDF file format is required|
421
+ |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
422
+ |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "reference is a required property"`<br>`}]`|Add a `reference` argument to the method call|
423
+
341
424
  # Get message status
342
425
 
343
426
  Message status depends on the type of message that you have sent.
data/bin/test_client.rb CHANGED
@@ -9,11 +9,15 @@ def main
9
9
  test_get_all_templates_filter_by_type(client)
10
10
  test_generate_template_preview(client, ENV['EMAIL_TEMPLATE_ID'])
11
11
  email_notification = test_send_email_endpoint(client)
12
+ email_notification_with_document = test_send_email_endpoint_with_document(client)
12
13
  sms_notification = test_send_sms_endpoint(client)
13
14
  letter_notification = test_send_letter_endpoint(client)
15
+ precompiled_letter_notification = test_send_precompiled_letter_endpoint(client)
14
16
  test_get_notification_by_id_endpoint(client, email_notification.id, 'email')
17
+ test_get_notification_by_id_endpoint(client, email_notification_with_document.id, 'email')
15
18
  test_get_notification_by_id_endpoint(client, sms_notification.id, 'sms')
16
19
  test_get_notification_by_id_endpoint(client, letter_notification.id, 'letter')
20
+ test_get_notification_by_id_endpoint(client, precompiled_letter_notification.id, 'precompiled_letter')
17
21
  test_get_all_notifications(client)
18
22
  test_get_received_texts
19
23
  p 'ruby client integration tests pass'
@@ -97,6 +101,19 @@ def test_send_email_endpoint(client)
97
101
  email_resp
98
102
  end
99
103
 
104
+ def test_send_email_endpoint_with_document(client)
105
+ email_resp = File.open('spec/test_files/test_pdf.pdf', 'rb') do |f|
106
+ client.send_email(email_address: ENV['FUNCTIONAL_TEST_EMAIL'],
107
+ template_id: ENV['EMAIL_TEMPLATE_ID'],
108
+ personalisation: { name: Notifications.prepare_upload(f) },
109
+ reference: "some reference",
110
+ email_reply_to_id: ENV['EMAIL_REPLY_TO_ID'])
111
+ end
112
+
113
+ test_notification_response_data_type(email_resp, 'email')
114
+ email_resp
115
+ end
116
+
100
117
  def test_send_sms_endpoint(client)
101
118
  sms_resp = client.send_sms(phone_number: ENV['FUNCTIONAL_TEST_NUMBER'], template_id: ENV['SMS_TEMPLATE_ID'],
102
119
  personalisation: { "name" => "some name" },
@@ -120,6 +137,16 @@ def test_send_letter_endpoint(client)
120
137
  letter_resp
121
138
  end
122
139
 
140
+ def test_send_precompiled_letter_endpoint(client)
141
+ precompiled_letter_resp = File.open('spec/test_files/test_pdf.pdf', 'rb') do |file|
142
+ client.send_precompiled_letter("some reference", file)
143
+ end
144
+
145
+ test_notification_response_data_type(precompiled_letter_resp, 'precompiled_letter')
146
+
147
+ precompiled_letter_resp
148
+ end
149
+
123
150
  def test_notification_response_data_type(notification, message_type)
124
151
  unless notification.is_a?(Notifications::Client::ResponseNotification)
125
152
  p 'failed ' + message_type + ' response is not a Notifications::Client::ResponseNotification'
@@ -129,6 +156,12 @@ def test_notification_response_data_type(notification, message_type)
129
156
  p 'failed ' + message_type + 'id is not a String'
130
157
  exit 1
131
158
  end
159
+
160
+ if message_type == 'precompiled_letter'
161
+ field_should_not_be_nil(expected_fields_in_precompiled_letter_response, notification, 'send_precompiled_letter')
162
+ return
163
+ end
164
+
132
165
  field_should_not_be_nil(expected_fields_in_notification_response, notification, 'send_' + message_type)
133
166
  hash_key_should_not_be_nil(expected_fields_in_template, notification.send('template'), 'send_' + message_type + '.template')
134
167
 
@@ -161,6 +194,10 @@ def test_get_notification_by_id_endpoint(client, id, message_type)
161
194
  field_should_not_be_nil(expected_fields_in_letter_notification, get_notification_response, 'Notifications::Client::Notification for type letter')
162
195
  field_should_be_nil(expected_fields_in_letter_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type letter')
163
196
  hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type letter')
197
+ elsif message_type == 'precompiled_letter'
198
+ field_should_not_be_nil(expected_fields_in_precompiled_letter_notification, get_notification_response, 'Notifications::Client::Notification for type precompiled letter')
199
+ field_should_be_nil(expected_fields_in_precompiled_letter_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type precompiled letter')
200
+ hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type precompiled letter')
164
201
  end
165
202
  end
166
203
 
@@ -216,6 +253,11 @@ def expected_fields_in_notification_response
216
253
  uri)
217
254
  end
218
255
 
256
+ def expected_fields_in_precompiled_letter_response
257
+ %w(id
258
+ reference)
259
+ end
260
+
219
261
  def expected_fields_in_email_content
220
262
  %w(from_email
221
263
  body
@@ -223,7 +265,8 @@ def expected_fields_in_email_content
223
265
  end
224
266
 
225
267
  def expected_fields_in_sms_content
226
- %w(body)
268
+ %w(body
269
+ from_number)
227
270
  end
228
271
 
229
272
  def expected_fields_in_letter_content
@@ -309,6 +352,36 @@ def expected_fields_in_letter_notification_that_are_nil
309
352
  )
310
353
  end
311
354
 
355
+ def expected_fields_in_precompiled_letter_notification
356
+ %w(
357
+ body
358
+ created_at
359
+ id
360
+ line_1
361
+ reference
362
+ status
363
+ subject
364
+ template
365
+ type
366
+ )
367
+ end
368
+
369
+ def expected_fields_in_precompiled_letter_notification_that_are_nil
370
+ %w(
371
+ completed_at
372
+ created_by_name
373
+ email_address
374
+ line_2
375
+ line_3
376
+ line_4
377
+ line_5
378
+ line_6
379
+ phone_number
380
+ postcode
381
+ sent_at
382
+ )
383
+ end
384
+
312
385
  def expected_fields_in_template
313
386
  %w(id
314
387
  version
@@ -9,6 +9,7 @@ require_relative "client/response_template"
9
9
  require_relative "client/template_collection"
10
10
  require_relative "client/template_preview"
11
11
  require_relative "client/uuid_validator"
12
+ require_relative "client/helper_methods"
12
13
  require "forwardable"
13
14
 
14
15
  module Notifications
@@ -16,6 +17,7 @@ module Notifications
16
17
  attr_reader :speaker
17
18
 
18
19
  PRODUCTION_BASE_URL = "https://api.notifications.service.gov.uk".freeze
20
+ MAX_FILE_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB limit on uploaded documents
19
21
 
20
22
  extend Forwardable
21
23
  def_delegators :speaker, :service_id, :secret_token, :base_url, :base_url=
@@ -53,6 +55,17 @@ module Notifications
53
55
  )
54
56
  end
55
57
 
58
+ ##
59
+ # @param reference [String]
60
+ # @param pdf_file [File]
61
+ # @see Notifications::Client::Speaker#post_precompiled_letter
62
+ # @return [ResponseNotification]
63
+ def send_precompiled_letter(reference, pdf_file)
64
+ ResponseNotification.new(
65
+ speaker.post_precompiled_letter(reference, pdf_file)
66
+ )
67
+ end
68
+
56
69
  ##
57
70
  # @param id [String]
58
71
  # @see Notifications::Client::Speaker#get
@@ -0,0 +1,9 @@
1
+ require "base64"
2
+
3
+ module Notifications
4
+ def self.prepare_upload(file)
5
+ raise ArgumentError.new("Document is larger than 2MB") if file.size > Client::MAX_FILE_UPLOAD_SIZE
6
+
7
+ { file: Base64.strict_encode64(file.read) }
8
+ end
9
+ end
@@ -1,3 +1,4 @@
1
+ require "base64"
1
2
  require "net/https"
2
3
  require "uri"
3
4
  require "jwt"
@@ -95,6 +96,22 @@ module Notifications
95
96
  perform_request!(request)
96
97
  end
97
98
 
99
+ ##
100
+ # @param reference [String] reference of the notification
101
+ # @param pdf_file [File] PDF file opened for reading
102
+ # @see #perform_request!
103
+ def post_precompiled_letter(reference, pdf_file)
104
+ content = Base64.strict_encode64(pdf_file.read)
105
+ form_data = { reference: reference, content: content }
106
+
107
+ request = Net::HTTP::Post.new(
108
+ "#{BASE_PATH}/letter",
109
+ headers
110
+ )
111
+ request.body = form_data.to_json
112
+ perform_request!(request)
113
+ end
114
+
98
115
  private
99
116
 
100
117
  ##
@@ -103,7 +120,7 @@ module Notifications
103
120
  # not successful
104
121
  def perform_request!(request)
105
122
  response = open(request)
106
- if response.is_a?(Net::HTTPClientError)
123
+ if response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
107
124
  raise RequestError.new(response)
108
125
  else
109
126
  JSON.parse(response.body)
@@ -23,4 +23,3 @@ module Notifications
23
23
  end
24
24
  end
25
25
  end
26
-
@@ -9,6 +9,6 @@
9
9
 
10
10
  module Notifications
11
11
  class Client
12
- VERSION = "2.8.0".freeze
12
+ VERSION = "2.9.0".freeze
13
13
  end
14
14
  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.8.0
4
+ version: 2.9.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: 2018-09-05 00:00:00.000000000 Z
11
+ date: 2018-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -140,6 +140,7 @@ files:
140
140
  - docker/Dockerfile
141
141
  - docker/Makefile
142
142
  - lib/notifications/client.rb
143
+ - lib/notifications/client/helper_methods.rb
143
144
  - lib/notifications/client/notification.rb
144
145
  - lib/notifications/client/notifications_collection.rb
145
146
  - lib/notifications/client/received_text.rb