gds-api-adapters 59.3.0 → 59.4.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: 91b42cd68029cf84c51c7e36ba664641b7281c0b82b97bd7391721b732c22259
4
- data.tar.gz: 2245749af0e696496f34121bb239a7b1708d5aea4e171acdab957e12f5be8f1b
3
+ metadata.gz: 56d18ef98559e3640e67b7e74ea5515eaa8a7ef3c353d2b14f1175abc795eac8
4
+ data.tar.gz: a3280c7002add9a5e53dd730a7da7dcde062db85c6d8c7cf6ce7170752cc18d5
5
5
  SHA512:
6
- metadata.gz: a863fb18d176c903318166d7c0f92c059168326f3100adbbbcafa5050d8041998fc5341184936518149cb7bb0e4e3632bc053a8e59951ba3ffdd1f494d1e0d62
7
- data.tar.gz: 2189e1437e57133d4c3578bc426be5e35d320d31db462030a51aecf3d59708afa89967a55b92eb886283bdf271e90fcd8d0ee07fc0c416887652fa2584a5d90a
6
+ metadata.gz: 449724146bcd6c51ac6c71d637921216ce4bb4919401ea8bd6317bcbcd6685420edca58806acd542ac2e6d707efc025b09a323a13402909b5f6e8872e1f853e8
7
+ data.tar.gz: fe4dd739159277e71d478e4051358654c8f98cac092abe533e06b4b39190d4a461b26a05d124974b3c153654294707efb50e60e10ee6ec8eb52aecae09628310
@@ -53,18 +53,68 @@ module GdsApi
53
53
  subscriber_list_id: 1000,
54
54
  ended: false
55
55
  )
56
+ response = get_subscription_response(
57
+ id,
58
+ frequency: frequency,
59
+ title: title,
60
+ subscriber_id: subscriber_id,
61
+ subscriber_list_id: subscriber_list_id,
62
+ ended: ended,
63
+ ).to_json
64
+
56
65
  stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}")
57
- .to_return(
58
- status: 200,
59
- body: get_subscription_response(
60
- id,
61
- frequency: frequency,
62
- title: title,
63
- subscriber_id: subscriber_id,
64
- subscriber_list_id: subscriber_list_id,
65
- ended: ended,
66
- ).to_json,
67
- )
66
+ .to_return(status: 200, body: response)
67
+ stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}/latest")
68
+ .to_return(status: 200, body: response)
69
+ end
70
+
71
+ # Stubs the API responses as if each subscription happened in the order they are passed.
72
+ # Useful if you need to query the '/latest' endpoint of a subscription.
73
+ # Takes an array of hashes.
74
+ #
75
+ # @example
76
+ # stub_email_alert_api_has_subscriptions([
77
+ # {
78
+ # id: 'id-of-my-subscriber-list',
79
+ #  frequency: 'weekly',
80
+ # ended: true,
81
+ # },
82
+ # {
83
+ # id: 'id-of-my-subscriber-list',
84
+ #  frequency: 'daily',
85
+ # },
86
+ # ])
87
+ #
88
+ # @param subscriptions [Array]
89
+ def stub_email_alert_api_has_subscriptions(subscriptions)
90
+ subscriptions.map! { |subscription| apply_subscription_defaults(subscription) }
91
+ subscriptions.each do |id, params|
92
+ latest_id, latest_params = get_latest_matching(params, subscriptions)
93
+ stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}")
94
+ .to_return(status: 200, body: get_subscription_response(id, params).to_json)
95
+ stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}/latest")
96
+ .to_return(status: 200, body: get_subscription_response(latest_id, latest_params).to_json)
97
+ end
98
+ end
99
+
100
+ def apply_subscription_defaults(subscription)
101
+ parameters = {
102
+ title: "Some title",
103
+ subscriber_id: 1,
104
+ subscriber_list_id: 1000,
105
+ ended: true,
106
+ }.merge(subscription)
107
+ # Strip out ID as subsequent call to `get_subscription_response` throws `ArgumentError`
108
+ id = parameters.delete(:id)
109
+ [id, parameters]
110
+ end
111
+
112
+ def get_latest_matching(params, subscriptions)
113
+ matching = subscriptions.select do |_current_id, current_params|
114
+ params[:subscriber_id] == current_params[:subscriber_id] &&
115
+ params[:subscriber_list_id] == current_params[:subscriber_list_id]
116
+ end
117
+ matching.last
68
118
  end
69
119
 
70
120
  def stub_email_alert_api_has_subscriber_list(attributes)
@@ -227,6 +277,7 @@ module GdsApi
227
277
  alias_method :email_alert_api_has_subscriber_subscriptions, :stub_email_alert_api_has_subscriber_subscriptions
228
278
  alias_method :email_alert_api_does_not_have_subscriber_subscriptions, :stub_email_alert_api_does_not_have_subscriber_subscriptions
229
279
  alias_method :email_alert_api_has_subscription, :stub_email_alert_api_has_subscription
280
+ alias_method :email_alert_api_has_subscriptions, :stub_email_alert_api_has_subscriptions
230
281
  alias_method :email_alert_api_has_subscriber_list, :stub_email_alert_api_has_subscriber_list
231
282
  alias_method :email_alert_api_does_not_have_subscriber_list, :stub_email_alert_api_does_not_have_subscriber_list
232
283
  alias_method :email_alert_api_creates_subscriber_list, :stub_email_alert_api_creates_subscriber_list
@@ -270,7 +321,7 @@ module GdsApi
270
321
  "id" => id,
271
322
  "frequency" => frequency,
272
323
  "source" => "user_signed_up",
273
- "ended_at" => ended ? Time.now.rfc3339 : nil,
324
+ "ended_at" => ended ? Time.now.to_datetime.rfc3339 : nil,
274
325
  "ended_reason" => ended ? "unsubscribed" : nil,
275
326
  "subscriber" => {
276
327
  "id" => subscriber_id,
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '59.3.0'.freeze
2
+ VERSION = '59.4.0'.freeze
3
3
  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: 59.3.0
4
+ version: 59.4.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: 2019-05-20 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable