notifications-ruby-client 4.0.0 → 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -4
- data/CHANGELOG.md +26 -0
- data/CONTRIBUTING.md +6 -1
- data/DOCUMENTATION.md +200 -124
- data/Dockerfile +13 -0
- data/Makefile +14 -22
- data/bin/test_client.rb +70 -18
- data/lib/notifications/client.rb +8 -0
- data/lib/notifications/client/helper_methods.rb +3 -3
- data/lib/notifications/client/request_error.rb +4 -1
- data/lib/notifications/client/response_template.rb +1 -0
- data/lib/notifications/client/speaker.rb +13 -0
- data/lib/notifications/client/version.rb +1 -1
- data/notifications-ruby-client.gemspec +3 -4
- metadata +10 -26
- data/docker/Dockerfile +0 -26
- data/docker/Makefile +0 -16
data/Dockerfile
ADDED
data/Makefile
CHANGED
@@ -32,18 +32,14 @@ generate-env-file: ## Generate the environment file for running the tests inside
|
|
32
32
|
|
33
33
|
.PHONY: prepare-docker-runner-image
|
34
34
|
prepare-docker-runner-image: ## Prepare the Docker builder image
|
35
|
-
|
35
|
+
docker pull `grep "FROM " Dockerfile | cut -d ' ' -f 2` || true
|
36
|
+
docker build -t ${DOCKER_BUILDER_IMAGE_NAME} .
|
36
37
|
|
37
38
|
.PHONY: build-with-docker
|
38
39
|
build-with-docker: prepare-docker-runner-image ## Build inside a Docker container
|
39
40
|
docker run -i --rm \
|
40
41
|
--name "${DOCKER_CONTAINER_PREFIX}-build" \
|
41
42
|
-v "`pwd`:/var/project" \
|
42
|
-
-e http_proxy="${HTTP_PROXY}" \
|
43
|
-
-e HTTP_PROXY="${HTTP_PROXY}" \
|
44
|
-
-e https_proxy="${HTTPS_PROXY}" \
|
45
|
-
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
46
|
-
-e NO_PROXY="${NO_PROXY}" \
|
47
43
|
${DOCKER_BUILDER_IMAGE_NAME} \
|
48
44
|
make build
|
49
45
|
|
@@ -52,36 +48,32 @@ test-with-docker: prepare-docker-runner-image generate-env-file ## Run tests ins
|
|
52
48
|
docker run -i --rm \
|
53
49
|
--name "${DOCKER_CONTAINER_PREFIX}-test" \
|
54
50
|
-v "`pwd`:/var/project" \
|
55
|
-
-e http_proxy="${HTTP_PROXY}" \
|
56
|
-
-e HTTP_PROXY="${HTTP_PROXY}" \
|
57
|
-
-e https_proxy="${HTTPS_PROXY}" \
|
58
|
-
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
59
|
-
-e NO_PROXY="${NO_PROXY}" \
|
60
51
|
--env-file docker.env \
|
61
52
|
${DOCKER_BUILDER_IMAGE_NAME} \
|
62
|
-
make test
|
53
|
+
make build test
|
63
54
|
|
64
55
|
.PHONY: integration-test-with-docker
|
65
56
|
integration-test-with-docker: prepare-docker-runner-image generate-env-file ## Run integration tests inside a Docker container
|
66
57
|
docker run -i --rm \
|
67
58
|
--name "${DOCKER_CONTAINER_PREFIX}-integration-test" \
|
68
59
|
-v "`pwd`:/var/project" \
|
69
|
-
-e http_proxy="${HTTP_PROXY}" \
|
70
|
-
-e HTTP_PROXY="${HTTP_PROXY}" \
|
71
|
-
-e https_proxy="${HTTPS_PROXY}" \
|
72
|
-
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
73
|
-
-e NO_PROXY="${NO_PROXY}" \
|
74
60
|
--env-file docker.env \
|
75
61
|
${DOCKER_BUILDER_IMAGE_NAME} \
|
76
|
-
make integration-test
|
62
|
+
make build integration-test
|
63
|
+
|
64
|
+
.PHONY: get-client-version
|
65
|
+
get-client-version: ## Retrieve client version number from source code
|
66
|
+
@ruby -e "require './lib/notifications/client/version'; puts Notifications::Client::VERSION"
|
67
|
+
|
68
|
+
.PHONY: publish-to-rubygems
|
69
|
+
publish-to-rubygems: ## Create gemspec file and publish to rubygems
|
70
|
+
$(if ${GEM_HOST_API_KEY},,$(error Must specify GEM_HOST_API_KEY))
|
71
|
+
gem build notifications-ruby-client.gemspec --output=release.gem
|
72
|
+
gem push release.gem
|
77
73
|
|
78
74
|
.PHONY: clean-docker-containers
|
79
75
|
clean-docker-containers: ## Clean up any remaining docker containers
|
80
76
|
docker rm -f $(shell docker ps -q -f "name=${DOCKER_CONTAINER_PREFIX}") 2> /dev/null || true
|
81
77
|
|
82
|
-
.PHONY: run-govuk-lint
|
83
|
-
run-govuk-lint: ## Runs GOVUK-lint for Ruby
|
84
|
-
bundle exec govuk-lint-ruby lib spec bin/test_client
|
85
|
-
|
86
78
|
clean:
|
87
79
|
rm -rf vendor
|
data/bin/test_client.rb
CHANGED
@@ -3,7 +3,9 @@ require './lib/notifications/client'
|
|
3
3
|
|
4
4
|
def main
|
5
5
|
client = Notifications::Client.new(ENV['API_KEY'], ENV['NOTIFY_API_URL'])
|
6
|
-
|
6
|
+
test_get_email_template_by_id(client, ENV['EMAIL_TEMPLATE_ID'])
|
7
|
+
test_get_sms_template_by_id(client, ENV['SMS_TEMPLATE_ID'])
|
8
|
+
test_get_letter_template_by_id(client, ENV['LETTER_TEMPLATE_ID'])
|
7
9
|
test_get_template_version(client, ENV['SMS_TEMPLATE_ID'], 1)
|
8
10
|
test_get_all_templates(client)
|
9
11
|
test_get_all_templates_filter_by_type(client)
|
@@ -20,18 +22,29 @@ def main
|
|
20
22
|
test_get_notification_by_id_endpoint(client, precompiled_letter_notification.id, 'precompiled_letter')
|
21
23
|
test_get_all_notifications(client)
|
22
24
|
test_get_received_texts
|
25
|
+
test_get_pdf_for_letter(client, letter_notification.id)
|
23
26
|
p 'ruby client integration tests pass'
|
24
27
|
exit 0
|
25
28
|
end
|
26
29
|
|
27
|
-
def
|
30
|
+
def test_get_email_template_by_id(client, id)
|
28
31
|
response = client.get_template_by_id(id)
|
29
|
-
test_template_response(response, '
|
32
|
+
test_template_response(response, 'email', 'test_get_email_template_by_id')
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_get_sms_template_by_id(client, id)
|
36
|
+
response = client.get_template_by_id(id)
|
37
|
+
test_template_response(response, 'sms', 'test_get_sms_template_by_id')
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_get_letter_template_by_id(client, id)
|
41
|
+
response = client.get_template_by_id(id)
|
42
|
+
test_template_response(response, 'letter', 'test_get_letter_template_by_id')
|
30
43
|
end
|
31
44
|
|
32
45
|
def test_get_template_version(client, id, version)
|
33
46
|
response = client.get_template_version(id, version)
|
34
|
-
test_template_response(response, 'test_get_template_version')
|
47
|
+
test_template_response(response, 'sms', 'test_get_template_version')
|
35
48
|
end
|
36
49
|
|
37
50
|
def test_get_all_templates(client)
|
@@ -44,9 +57,9 @@ def test_get_all_templates(client)
|
|
44
57
|
p 'failed test_get_all_templates, expected at least 3 templates returned.'
|
45
58
|
exit 1
|
46
59
|
end
|
47
|
-
test_template_response(response.collection[0], 'test_get_all_templates')
|
48
|
-
test_template_response(response.collection[1], 'test_get_all_templates')
|
49
|
-
test_template_response(response.collection[2], 'test_get_all_templates')
|
60
|
+
test_template_response(response.collection[0], 'letter', 'test_get_all_templates')
|
61
|
+
test_template_response(response.collection[1], 'email', 'test_get_all_templates')
|
62
|
+
test_template_response(response.collection[2], 'sms', 'test_get_all_templates')
|
50
63
|
end
|
51
64
|
|
52
65
|
def test_get_all_templates_filter_by_type(client)
|
@@ -59,7 +72,7 @@ def test_get_all_templates_filter_by_type(client)
|
|
59
72
|
p 'failed test_get_all_templates, expected at least 2 templates returned.'
|
60
73
|
exit 1
|
61
74
|
end
|
62
|
-
test_template_response(response.collection[0], 'test_get_all_templates')
|
75
|
+
test_template_response(response.collection[0], 'sms', 'test_get_all_templates')
|
63
76
|
end
|
64
77
|
|
65
78
|
def test_generate_template_preview(client, id)
|
@@ -67,7 +80,7 @@ def test_generate_template_preview(client, id)
|
|
67
80
|
test_template_preview(response)
|
68
81
|
end
|
69
82
|
|
70
|
-
def test_template_response(response, test_method)
|
83
|
+
def test_template_response(response, template_type, test_method)
|
71
84
|
unless response.is_a?(Notifications::Client::Template)
|
72
85
|
p 'failed test_get_template_by_id response is not a Notifications::Client::Template'
|
73
86
|
exit 1
|
@@ -76,7 +89,17 @@ def test_template_response(response, test_method)
|
|
76
89
|
p 'failed template id is not a String'
|
77
90
|
exit 1
|
78
91
|
end
|
79
|
-
|
92
|
+
|
93
|
+
field_should_not_be_nil(
|
94
|
+
expected_fields_in_template_response(template_type),
|
95
|
+
response,
|
96
|
+
test_method
|
97
|
+
)
|
98
|
+
field_should_be_nil(
|
99
|
+
expected_nil_fields_in_template_response(template_type),
|
100
|
+
response,
|
101
|
+
test_method
|
102
|
+
)
|
80
103
|
end
|
81
104
|
|
82
105
|
def test_template_preview(response)
|
@@ -205,6 +228,28 @@ def test_get_notification_by_id_endpoint(client, id, message_type)
|
|
205
228
|
end
|
206
229
|
end
|
207
230
|
|
231
|
+
def test_get_pdf_for_letter(client, id)
|
232
|
+
response = nil
|
233
|
+
|
234
|
+
# try 15 times with 3 secs sleep between each attempt, to get the PDF
|
235
|
+
15.times do
|
236
|
+
begin
|
237
|
+
response = client.get_pdf_for_letter(id)
|
238
|
+
rescue Notifications::Client::BadRequestError
|
239
|
+
sleep(3)
|
240
|
+
end
|
241
|
+
|
242
|
+
if !response.nil?
|
243
|
+
break
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
unless !response.nil? && response.start_with?("%PDF-")
|
248
|
+
p "get_pdf_for_letter response for " + id + " is not a PDF: " + response.to_s
|
249
|
+
exit 1
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
208
253
|
def hash_key_should_not_be_nil(fields, obj, method_name)
|
209
254
|
fields.each do |field|
|
210
255
|
if obj.has_value?(:"#{field}")
|
@@ -232,14 +277,21 @@ def field_should_be_nil(fields, obj, method_name)
|
|
232
277
|
end
|
233
278
|
end
|
234
279
|
|
235
|
-
def expected_fields_in_template_response
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
280
|
+
def expected_fields_in_template_response(template_type)
|
281
|
+
{
|
282
|
+
"email" => ["id", "name", "type", "created_at", "created_by", "version", "body", "subject"],
|
283
|
+
"sms" => ["id", "name", "type", "created_at", "created_by", "version", "body"],
|
284
|
+
"letter" => ["id", "name", "type", "created_at", "created_by", "version", "body", "subject",
|
285
|
+
"letter_contact_block"],
|
286
|
+
}[template_type]
|
287
|
+
end
|
288
|
+
|
289
|
+
def expected_nil_fields_in_template_response(template_type)
|
290
|
+
{
|
291
|
+
"email" => ["letter_contact_block"],
|
292
|
+
"sms" => ["subject", "letter_contact_block"],
|
293
|
+
"letter" => [],
|
294
|
+
}[template_type]
|
243
295
|
end
|
244
296
|
|
245
297
|
def expected_fields_in_template_preview
|
data/lib/notifications/client.rb
CHANGED
@@ -67,6 +67,14 @@ module Notifications
|
|
67
67
|
)
|
68
68
|
end
|
69
69
|
|
70
|
+
##
|
71
|
+
# @param id [String]
|
72
|
+
# @see Notifications::Client::Speaker#get
|
73
|
+
# @return [String]
|
74
|
+
def get_pdf_for_letter(id)
|
75
|
+
speaker.get_pdf_for_letter(id)
|
76
|
+
end
|
77
|
+
|
70
78
|
##
|
71
79
|
# @param id [String]
|
72
80
|
# @see Notifications::Client::Speaker#get
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "base64"
|
2
2
|
|
3
3
|
module Notifications
|
4
|
-
def self.prepare_upload(file)
|
5
|
-
raise ArgumentError.new("
|
4
|
+
def self.prepare_upload(file, is_csv=false)
|
5
|
+
raise ArgumentError.new("File is larger than 2MB") if file.size > Client::MAX_FILE_UPLOAD_SIZE
|
6
6
|
|
7
|
-
{ file: Base64.strict_encode64(file.read) }
|
7
|
+
{ file: Base64.strict_encode64(file.read), is_csv: is_csv }
|
8
8
|
end
|
9
9
|
end
|
@@ -6,15 +6,18 @@ module Notifications
|
|
6
6
|
def initialize(response)
|
7
7
|
@code = response.code
|
8
8
|
@body = parse_body(response.body)
|
9
|
+
super(build_message)
|
9
10
|
end
|
10
11
|
|
12
|
+
private
|
13
|
+
|
11
14
|
def parse_body(body)
|
12
15
|
JSON.parse(body)
|
13
16
|
rescue JSON::ParserError
|
14
17
|
body
|
15
18
|
end
|
16
19
|
|
17
|
-
def
|
20
|
+
def build_message
|
18
21
|
return body if body.is_a?(String)
|
19
22
|
|
20
23
|
error_messages = body.fetch('errors')
|
@@ -118,6 +118,19 @@ module Notifications
|
|
118
118
|
perform_request!(request)
|
119
119
|
end
|
120
120
|
|
121
|
+
def get_pdf_for_letter(id)
|
122
|
+
path = "/v2/notifications/" << id << "/pdf"
|
123
|
+
request = Net::HTTP::Get.new(path, headers)
|
124
|
+
|
125
|
+
# can't use `perform_request!` because we're just returning raw binary data
|
126
|
+
response = open(request)
|
127
|
+
if response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
|
128
|
+
raise build_error(response)
|
129
|
+
else
|
130
|
+
response.body
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
121
134
|
private
|
122
135
|
|
123
136
|
##
|
@@ -22,10 +22,9 @@ Gem::Specification.new do |spec|
|
|
22
22
|
|
23
23
|
spec.add_runtime_dependency "jwt", ">= 1.5", "< 3"
|
24
24
|
|
25
|
-
spec.add_development_dependency "bundler", "~> 1.
|
26
|
-
spec.add_development_dependency "rake", "~>
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
27
27
|
spec.add_development_dependency "rspec", "~> 3.7"
|
28
28
|
spec.add_development_dependency "webmock", "~> 3.4"
|
29
|
-
spec.add_development_dependency "factory_bot", "~>
|
30
|
-
spec.add_development_dependency "govuk-lint", "~> 3.8"
|
29
|
+
spec.add_development_dependency "factory_bot", "~> 6.1"
|
31
30
|
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:
|
4
|
+
version: 5.3.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:
|
11
|
+
date: 2020-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jwt
|
@@ -36,28 +36,28 @@ dependencies:
|
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '1.
|
39
|
+
version: '1.7'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '1.
|
46
|
+
version: '1.7'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '13.0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
60
|
+
version: '13.0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,28 +92,14 @@ dependencies:
|
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '
|
95
|
+
version: '6.1'
|
96
96
|
type: :development
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: '
|
103
|
-
- !ruby/object:Gem::Dependency
|
104
|
-
name: govuk-lint
|
105
|
-
requirement: !ruby/object:Gem::Requirement
|
106
|
-
requirements:
|
107
|
-
- - "~>"
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '3.8'
|
110
|
-
type: :development
|
111
|
-
prerelease: false
|
112
|
-
version_requirements: !ruby/object:Gem::Requirement
|
113
|
-
requirements:
|
114
|
-
- - "~>"
|
115
|
-
- !ruby/object:Gem::Version
|
116
|
-
version: '3.8'
|
102
|
+
version: '6.1'
|
117
103
|
description:
|
118
104
|
email:
|
119
105
|
- notify@digital.cabinet-office.gov.uk
|
@@ -128,6 +114,7 @@ files:
|
|
128
114
|
- CHANGELOG.md
|
129
115
|
- CONTRIBUTING.md
|
130
116
|
- DOCUMENTATION.md
|
117
|
+
- Dockerfile
|
131
118
|
- Gemfile
|
132
119
|
- LICENSE
|
133
120
|
- Makefile
|
@@ -137,8 +124,6 @@ files:
|
|
137
124
|
- bin/generate_docker_env.sh
|
138
125
|
- bin/setup
|
139
126
|
- bin/test_client.rb
|
140
|
-
- docker/Dockerfile
|
141
|
-
- docker/Makefile
|
142
127
|
- lib/notifications/client.rb
|
143
128
|
- lib/notifications/client/helper_methods.rb
|
144
129
|
- lib/notifications/client/notification.rb
|
@@ -173,8 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
158
|
- !ruby/object:Gem::Version
|
174
159
|
version: '0'
|
175
160
|
requirements: []
|
176
|
-
|
177
|
-
rubygems_version: 2.7.6
|
161
|
+
rubygems_version: 3.0.3
|
178
162
|
signing_key:
|
179
163
|
specification_version: 4
|
180
164
|
summary: Ruby client for GOV.UK Notifications API
|
data/docker/Dockerfile
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
FROM ruby:2.3.1-slim
|
2
|
-
|
3
|
-
ARG HTTP_PROXY
|
4
|
-
ARG HTTPS_PROXY
|
5
|
-
ARG NO_PROXY
|
6
|
-
|
7
|
-
RUN \
|
8
|
-
echo "Install Debian packages" \
|
9
|
-
&& ([ -z "$HTTP_PROXY" ] || echo "Acquire::http::Proxy \"${HTTP_PROXY}\";" > /etc/apt/apt.conf.d/99HttpProxy) \
|
10
|
-
&& apt-get update \
|
11
|
-
&& apt-get install -y --no-install-recommends \
|
12
|
-
gcc \
|
13
|
-
make \
|
14
|
-
curl \
|
15
|
-
git \
|
16
|
-
|
17
|
-
&& echo "Clean up" \
|
18
|
-
&& rm -rf /var/lib/apt/lists/* /tmp/*
|
19
|
-
|
20
|
-
ENV PATH=/var/project/vendor/bin:$PATH \
|
21
|
-
BUNDLE_PATH="/var/project/vendor/bundle" \
|
22
|
-
BUNDLE_BIN="/var/project/vendor/bin" \
|
23
|
-
BUNDLE_APP_CONFIG="/var/project/.bundle"
|
24
|
-
|
25
|
-
|
26
|
-
WORKDIR /var/project
|