gds-api-adapters 78.0.0 → 79.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
  SHA256:
3
- metadata.gz: 5a423ee043ccb33b30d2d1a0f12b0a5f43d14018b774afa6270f24626062f696
4
- data.tar.gz: e18aa2ed41343e430781063d9a8e85584213781758a245f8c818849c9d51e30d
3
+ metadata.gz: be2d6d94c4da753ae82eabe4728e9e21d385c8d7332baa5682eed54ee4d41d37
4
+ data.tar.gz: c0435733dd835d528ae596002542aa426030b494bcb7c839ba80b8020322e089
5
5
  SHA512:
6
- metadata.gz: 2113447106f41212696c5daf6e048c1bf0df7feac8b524d63a437f8113d8cb4cf9e2a9910e898c55a6501d7495548ff131142a4cdccf3e4287b327be54320a4d
7
- data.tar.gz: 74a1d841906f75f5f9f045f8157e8bf40ace3a4e12e6b30226d07e3e1959267d4b66821e72e2d0812cb1ee951b1a99724e71b60bbf8e057bd8aea7ee60622153
6
+ metadata.gz: f731412f3b2172d700bae052a934b30051213c28dd8b6217154196193b5c6c246efeac1b682b199cce2f8f1d38974c7864fb3aca682219492da8ccf92f41b2b0
7
+ data.tar.gz: 4e61e4360f445fa5c37e364303893874eedc3004e8e53d6dc0df3ffffbb08e1f0316882860bf9c389bdc12a425cbd2e93aa6ef7823aaa8ed300ae60dc28446f8
@@ -251,15 +251,30 @@ class GdsApi::EmailAlertApi < GdsApi::Base
251
251
  # optionally send a notification email explaining the reason
252
252
  #
253
253
  # @param [string] slug Identifier for the subscription list
254
- # @param [string] (optional) body Optional email body to send to alert users they are being unsubscribed
255
- # @param [string] (optional) sender_message_id A UUID to prevent multiple emails for the same event
256
- def bulk_unsubscribe(slug:, body: nil, sender_message_id: nil)
254
+ # @param [string] (optional) govuk_request_id An ID allowing us to trace requests across our infra. Required if you want to send an email.
255
+ # @param [string] (optional) body Optional email body to send to alert users they are being unsubscribed. Required if you want to send an email
256
+ # @param [string] (optional) sender_message_id A UUID to prevent multiple emails for the same event. Required if you want to send an email.
257
+ def bulk_unsubscribe(slug:, govuk_request_id: nil, body: nil, sender_message_id: nil)
257
258
  post_json(
258
259
  "#{endpoint}/subscriber-lists/#{slug}/bulk-unsubscribe",
259
260
  {
260
261
  body: body,
261
262
  sender_message_id: sender_message_id,
262
263
  }.compact,
264
+ {
265
+ "Govuk-Request-Id" => govuk_request_id,
266
+ }.compact,
267
+ )
268
+ end
269
+
270
+ # Update subscriber list details, such as title
271
+ # @param [Hash] params A hash of detail paramaters that can be updated. For example title.
272
+ # For allowed parameters see:
273
+ # https://github.com/alphagov/email-alert-api/blob/main/docs/api.md#patch-subscriber-listsxxx
274
+ def update_subscriber_list_details(slug:, params: {})
275
+ patch_json(
276
+ "#{endpoint}/subscriber-lists/#{slug}",
277
+ params,
263
278
  )
264
279
  end
265
280
 
@@ -0,0 +1,28 @@
1
+ require_relative "base"
2
+ require_relative "exceptions"
3
+
4
+ class GdsApi::LocationsApi < GdsApi::Base
5
+ # Get a list of local custodian codes for a postcode
6
+ #
7
+ # @param [String, nil] postcode The postcode for which the custodian codes are requested
8
+ #
9
+ # @return [Array] All local custodian codes for a specific postcode
10
+ def local_custodian_code_for_postcode(postcode)
11
+ response = get_json("#{endpoint}/locations?postcode=#{postcode}.json")
12
+
13
+ return [] if response["results"].nil?
14
+
15
+ response["results"].map { |r| r["local_custodian_code"] }.uniq
16
+ end
17
+
18
+ # Get the average coordinates for a postcode
19
+ #
20
+ # @param [String, nil] postcode The postcode for which the coordinates are requested
21
+ #
22
+ # @return [Hash] The average coordinates (two fields, "latitude" and "longitude") for a specific postcode
23
+ def coordinates_for_postcode(postcode)
24
+ response = get_json("#{endpoint}/locations?postcode=#{postcode}.json")
25
+
26
+ { "latitude" => response["average_latitude"], "longitude" => response["average_longitude"] } unless response["results"].nil?
27
+ end
28
+ end
@@ -413,13 +413,14 @@ module GdsApi
413
413
  .to_return(status: 202)
414
414
  end
415
415
 
416
- def stub_email_alert_api_bulk_unsubscribe_with_message(slug:, body:, sender_message_id:)
416
+ def stub_email_alert_api_bulk_unsubscribe_with_message(slug:, govuk_request_id:, body:, sender_message_id:)
417
417
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
418
418
  .with(
419
419
  body: {
420
420
  body: body,
421
421
  sender_message_id: sender_message_id,
422
422
  }.to_json,
423
+ headers: { "Govuk-Request-Id" => govuk_request_id },
423
424
  ).to_return(status: 202)
424
425
  end
425
426
 
@@ -428,33 +429,70 @@ module GdsApi
428
429
  .to_return(status: 404)
429
430
  end
430
431
 
431
- def stub_email_alert_api_bulk_unsubscribe_not_found_with_message(slug:, body:, sender_message_id:)
432
+ def stub_email_alert_api_bulk_unsubscribe_not_found_with_message(slug:, govuk_request_id:, body:, sender_message_id:)
432
433
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
433
434
  .with(
434
435
  body: {
435
436
  body: body,
436
437
  sender_message_id: sender_message_id,
437
438
  }.to_json,
439
+ headers: { "Govuk-Request-Id" => govuk_request_id },
438
440
  ).to_return(status: 404)
439
441
  end
440
442
 
441
- def stub_email_alert_api_bulk_unsubscribe_conflict_with_message(slug:, body:, sender_message_id:)
443
+ def stub_email_alert_api_bulk_unsubscribe_conflict(slug:)
444
+ stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
445
+ .to_return(status: 409)
446
+ end
447
+
448
+ def stub_email_alert_api_bulk_unsubscribe_conflict_with_message(slug:, govuk_request_id:, body:, sender_message_id:)
442
449
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
443
450
  .with(
444
451
  body: {
445
452
  body: body,
446
453
  sender_message_id: sender_message_id,
447
454
  }.to_json,
455
+ headers: { "Govuk-Request-Id" => govuk_request_id },
448
456
  ).to_return(status: 409)
449
457
  end
450
458
 
451
- def stub_email_alert_api_bulk_unsubscribe_bad_request(slug:, body:)
459
+ def stub_email_alert_api_bulk_unsubscribe_bad_request(slug:)
460
+ stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
461
+ .to_return(status: 422)
462
+ end
463
+
464
+ def stub_email_alert_api_bulk_unsubscribe_bad_request_with_message(slug:, govuk_request_id:, body:, sender_message_id:)
452
465
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
453
466
  .with(
454
- body: { body: body }.to_json,
467
+ body: {
468
+ body: body,
469
+ sender_message_id: sender_message_id,
470
+ }.to_json,
471
+ headers: { "Govuk-Request-Id" => govuk_request_id },
455
472
  ).to_return(status: 422)
456
473
  end
457
474
 
475
+ def stub_update_subscriber_list_details(slug:, params:)
476
+ stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
477
+ .with(body: params.to_json)
478
+ .to_return(
479
+ status: 200,
480
+ body: get_subscriber_list_response(params).to_json,
481
+ )
482
+ end
483
+
484
+ def stub_update_subscriber_list_details_not_found(slug:, params:)
485
+ stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
486
+ .with(body: params.to_json)
487
+ .to_return(status: 404)
488
+ end
489
+
490
+ def stub_update_subscriber_list_details_unprocessible_entity(slug:, params: {})
491
+ stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
492
+ .with(body: params.to_json)
493
+ .to_return(status: 422)
494
+ end
495
+
458
496
  private
459
497
 
460
498
  def get_subscriber_response(id, address, govuk_account_id)
@@ -534,6 +572,7 @@ module GdsApi
534
572
  if attributes
535
573
  tags = attributes["tags"]
536
574
  links = attributes["links"]
575
+ content_id = attributes["content_id"]
537
576
  document_type = attributes["document_type"]
538
577
  email_document_supertype = attributes["email_document_supertype"]
539
578
  government_document_supertype = attributes["government_document_supertype"]
@@ -543,6 +582,7 @@ module GdsApi
543
582
  params = {}
544
583
  params[:tags] = tags if tags
545
584
  params[:links] = links if links
585
+ params[:content_id] = content_id if content_id
546
586
  params[:document_type] = document_type if document_type
547
587
  params[:email_document_supertype] = email_document_supertype if email_document_supertype
548
588
  params[:government_document_supertype] = government_document_supertype if government_document_supertype
@@ -0,0 +1,38 @@
1
+ module GdsApi
2
+ module TestHelpers
3
+ module LocationsApi
4
+ LOCATIONS_API_ENDPOINT = Plek.current.find("locations-api")
5
+
6
+ def stub_locations_api_has_location(postcode, locations)
7
+ results = []
8
+ locations.each do |l|
9
+ results << {
10
+ "latitude" => l["latitude"],
11
+ "longitude" => l["longitude"],
12
+ "postcode" => postcode,
13
+ "local_custodian_code" => l["local_custodian_code"],
14
+ }
15
+ end
16
+
17
+ response = {
18
+ "average_latitude" => results.sum { |r| r["latitude"] } / results.size.to_f,
19
+ "average_longitude" => results.sum { |r| r["longitude"] } / results.size.to_f,
20
+ "results" => results,
21
+ }
22
+
23
+ stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/locations?postcode=#{postcode}.json")
24
+ .to_return(body: response.to_json, status: 200)
25
+ end
26
+
27
+ def stub_locations_api_has_no_location(postcode)
28
+ stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/locations?postcode=#{postcode}.json")
29
+ .to_return(body: { "results" => nil }.to_json, status: 200)
30
+ end
31
+
32
+ def stub_locations_api_does_not_have_a_bad_postcode(postcode)
33
+ stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/locations?postcode=#{postcode}.json")
34
+ .to_return(body: { "code" => 400, "error" => "Postcode '#{postcode}' is not valid." }.to_json, status: 400)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = "78.0.0".freeze
2
+ VERSION = "79.1.0".freeze
3
3
  end
data/lib/gds_api.rb CHANGED
@@ -10,6 +10,7 @@ require "gds_api/imminence"
10
10
  require "gds_api/licence_application"
11
11
  require "gds_api/link_checker_api"
12
12
  require "gds_api/local_links_manager"
13
+ require "gds_api/locations_api"
13
14
  require "gds_api/mapit"
14
15
  require "gds_api/maslow"
15
16
  require "gds_api/organisations"
@@ -201,4 +202,11 @@ module GdsApi
201
202
  def self.worldwide(options = {})
202
203
  GdsApi::Worldwide.new(Plek.new.website_root, options)
203
204
  end
205
+
206
+ # Creates a GdsApi::LocationsApi adapter
207
+ #
208
+ # @return [GdsApi::LocationsApi]
209
+ def self.locations_api(options = {})
210
+ GdsApi::LocationsApi.new(Plek.find("locations-api"), options)
211
+ end
204
212
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 78.0.0
4
+ version: 79.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-18 00:00:00.000000000 Z
11
+ date: 2022-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -385,6 +385,7 @@ files:
385
385
  - lib/gds_api/link_checker_api.rb
386
386
  - lib/gds_api/list_response.rb
387
387
  - lib/gds_api/local_links_manager.rb
388
+ - lib/gds_api/locations_api.rb
388
389
  - lib/gds_api/mapit.rb
389
390
  - lib/gds_api/maslow.rb
390
391
  - lib/gds_api/middleware/govuk_header_sniffer.rb
@@ -409,6 +410,7 @@ files:
409
410
  - lib/gds_api/test_helpers/licence_application.rb
410
411
  - lib/gds_api/test_helpers/link_checker_api.rb
411
412
  - lib/gds_api/test_helpers/local_links_manager.rb
413
+ - lib/gds_api/test_helpers/locations_api.rb
412
414
  - lib/gds_api/test_helpers/mapit.rb
413
415
  - lib/gds_api/test_helpers/organisations.rb
414
416
  - lib/gds_api/test_helpers/publishing_api.rb