From ba6d0ab1b8e3b3523dbfa2f7ebf67073fbddf61e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 27 Dec 2023 15:05:18 +0000 Subject: [PATCH 1/3] Allow setting custom filenames for documents Update client for the new document download feature which allows specifying a download filename for files sent by email. This supercedes the old `is_csv` parameter that was inconsistent at best. Given this, we are dropping support for `is_csv` altogether. --- CHANGELOG.md | 5 +++++ DOCUMENTATION.md | 14 +++++++++++--- lib/notifications/client/helper_methods.rb | 5 +++-- lib/notifications/client/version.rb | 2 +- spec/notifications/client/helper_methods_spec.rb | 12 ++++++------ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50d8089..d960459 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 6.0.0 + +* Removes the `is_csv` parameter from `prepare_upload` +* Adds a `filename` parameter to `prepare_upload` to set the filename of the document upon download. See the documentation for more information. + ## 5.4.0 * Add support for new security features when sending a file by email: diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index f0bdb0c..2cc4550 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -272,9 +272,17 @@ File.open("file.pdf", "rb") do |f| end ``` -##### CSV Files +#### Set the filename -Uploads for CSV files should use the `is_csv` parameter on the `prepare_upload()` helper method. For example: +To do this you will need version 6.0.0 of the Ruby client library, or a more recent version. + +You can provide a filename to set when the recipient downloads the file. If this is not provided, a random filename will be generated. + +Choosing a sensible filename can help users understand what the file contains, and find it again later. + +You do not have to set this, but we strongly recommend it. + +The filename must include the correct file extension, such as `.csv` for a CSV file. If you include the wrong file extension, users may not be able to open your file. ```ruby File.open("file.csv", "rb") do |f| @@ -282,7 +290,7 @@ File.open("file.csv", "rb") do |f| personalisation: { first_name: "Amala", application_date: "2018-01-01", - link_to_file: Notifications.prepare_upload(f, is_csv=true), + link_to_file: Notifications.prepare_upload(f, filename="2023-12-25-daily-report.csv"), } end ``` diff --git a/lib/notifications/client/helper_methods.rb b/lib/notifications/client/helper_methods.rb index cb52daa..76ebeec 100644 --- a/lib/notifications/client/helper_methods.rb +++ b/lib/notifications/client/helper_methods.rb @@ -1,11 +1,12 @@ require "base64" module Notifications - def self.prepare_upload(file, is_csv=false, confirm_email_before_download: nil, retention_period: nil) + def self.prepare_upload(file, filename: nil, confirm_email_before_download: nil, retention_period: nil) raise ArgumentError.new("File is larger than 2MB") if file.size > Client::MAX_FILE_UPLOAD_SIZE - data = { file: Base64.strict_encode64(file.read), is_csv: is_csv } + data = { file: Base64.strict_encode64(file.read) } + data[:filename] = filename data[:confirm_email_before_download] = confirm_email_before_download data[:retention_period] = retention_period diff --git a/lib/notifications/client/version.rb b/lib/notifications/client/version.rb index 958f246..ec34e3a 100644 --- a/lib/notifications/client/version.rb +++ b/lib/notifications/client/version.rb @@ -9,6 +9,6 @@ module Notifications class Client - VERSION = "5.4.0".freeze + VERSION = "6.0.0".freeze end end diff --git a/spec/notifications/client/helper_methods_spec.rb b/spec/notifications/client/helper_methods_spec.rb index ff4b406..a641786 100644 --- a/spec/notifications/client/helper_methods_spec.rb +++ b/spec/notifications/client/helper_methods_spec.rb @@ -10,29 +10,29 @@ result = Notifications.prepare_upload(f) f.rewind - expect(result).to eq(file: encoded_content, is_csv: false, confirm_email_before_download: nil, retention_period: nil) + expect(result).to eq(file: encoded_content, filename: nil, confirm_email_before_download: nil, retention_period: nil) expect(Base64.strict_decode64(encoded_content)).to eq(f.read) end end it "encodes a StringIO object" do input_string = StringIO.new("My document to send") - expect(Notifications.prepare_upload(input_string)).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", is_csv: false, confirm_email_before_download: nil, retention_period: nil) + expect(Notifications.prepare_upload(input_string)).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", filename: nil, confirm_email_before_download: nil, retention_period: nil) end - it "allows is_csv to be set to true" do + it "allows filename to be set" do input_string = StringIO.new("My document to send") - expect(Notifications.prepare_upload(input_string, true)).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", is_csv: true, confirm_email_before_download: nil, retention_period: nil) + expect(Notifications.prepare_upload(input_string, filename: "report.csv")).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", filename: "report.csv", confirm_email_before_download: nil, retention_period: nil) end it "allows confirm_email_before_download to be set to true" do input_string = StringIO.new("My document to send") - expect(Notifications.prepare_upload(input_string, confirm_email_before_download: true)).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", is_csv: false, confirm_email_before_download: true, retention_period: nil) + expect(Notifications.prepare_upload(input_string, confirm_email_before_download: true)).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", filename: nil, confirm_email_before_download: true, retention_period: nil) end it "allows retention_period to be set" do input_string = StringIO.new("My document to send") - expect(Notifications.prepare_upload(input_string, retention_period: '1 weeks')).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", is_csv: false, confirm_email_before_download: nil, retention_period: '1 weeks') + expect(Notifications.prepare_upload(input_string, retention_period: '1 weeks')).to eq(file: "TXkgZG9jdW1lbnQgdG8gc2VuZA==", filename: nil, confirm_email_before_download: nil, retention_period: '1 weeks') end it "raises an error when the file size is too large" do From 55aaf2dd3e91b75b23cd129a7538fa34caa66578 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 3 Jan 2024 13:39:37 +0000 Subject: [PATCH 2/3] Documentation updates from tech docs --- DOCUMENTATION.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 2cc4550..cf09299 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -276,13 +276,18 @@ end To do this you will need version 6.0.0 of the Ruby client library, or a more recent version. -You can provide a filename to set when the recipient downloads the file. If this is not provided, a random filename will be generated. +You should provide a filename when you upload your file. -Choosing a sensible filename can help users understand what the file contains, and find it again later. +The filename should tell the recipient what the file contains. A memorable filename can help the recipient to find the file again later. -You do not have to set this, but we strongly recommend it. +The filename must end with a file extension. For example, `.csv` for a CSV file. If you include the wrong file extension, recipients may not be able to open your file. -The filename must include the correct file extension, such as `.csv` for a CSV file. If you include the wrong file extension, users may not be able to open your file. +If you do not provide a filename for your file, Notify will: + +* generate a random filename +* try to add the correct file extension + +If Notify cannot add the correct file extension, recipients may not be able to open your file. ```ruby File.open("file.csv", "rb") do |f| From 7d8a6acafe11a9ae66b629071c6c4a66a379647e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 4 Jan 2024 10:19:16 +0000 Subject: [PATCH 3/3] Pin factory_bot below 6.4.5 https://github.com/thoughtbot/factory_bot/issues/1614 This version introduced a breaking change for Ruby 2 when trying to add Ruby 3 compatibility (and it didn't note that it was no longer compatible with ruby 2). Pinning for now, but should be able to be removed if they jank the version and release new versions with a requirement on Ruby 3+ --- notifications-ruby-client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifications-ruby-client.gemspec b/notifications-ruby-client.gemspec index d80a828..cebd87d 100644 --- a/notifications-ruby-client.gemspec +++ b/notifications-ruby-client.gemspec @@ -26,5 +26,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "rspec", "~> 3.7" spec.add_development_dependency "webmock", "~> 3.4" - spec.add_development_dependency "factory_bot", "~> 6.1" + spec.add_development_dependency "factory_bot", "~> 6.1", "<6.4.5" end