Class: GdsApi::EmailAlertApi

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/email_alert_api.rb

Overview

Adapter for the Email Alert API

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Instance Method Details

#change_subscriber(id:, new_address:) ⇒ Hash

Patch a Subscriber

Parameters:

  • Subscriber (integer)

    id

  • Subscriber (string)

    new_address

Returns:

  • (Hash)

    subscriber



203
204
205
206
207
208
# File 'lib/gds_api/email_alert_api.rb', line 203

def change_subscriber(id:, new_address:)
  patch_json(
    "#{endpoint}/subscribers/#{id}",
    new_address: new_address,
  )
end

#change_subscription(id:, frequency:) ⇒ Hash

Patch a Subscription

Parameters:

  • Subscription (string)

    id

  • Subscription (string)

    frequency

Returns:

  • (Hash)

    subscription



216
217
218
219
220
221
# File 'lib/gds_api/email_alert_api.rb', line 216

def change_subscription(id:, frequency:)
  patch_json(
    "#{endpoint}/subscriptions/#{id}",
    frequency: frequency,
  )
end

#create_content_change(content_change, headers = {}) ⇒ Object

Post a content change

Parameters:

  • content_change (Hash)

    Valid content change attributes



58
59
60
# File 'lib/gds_api/email_alert_api.rb', line 58

def create_content_change(content_change, headers = {})
  post_json("#{endpoint}/content-changes", content_change, headers)
end

#create_email(email_params) ⇒ Object

Send email

Parameters:

  • email_params (Hash)

    address, subject, body



72
73
74
# File 'lib/gds_api/email_alert_api.rb', line 72

def create_email(email_params)
  post_json("#{endpoint}/emails", email_params)
end

#create_message(message, headers = {}) ⇒ Object

Post a message

Parameters:

  • message (Hash)

    Valid message attributes



65
66
67
# File 'lib/gds_api/email_alert_api.rb', line 65

def create_message(message, headers = {})
  post_json("#{endpoint}/messages", message, headers)
end

#create_subscriber_list(attributes) ⇒ Object

Post a subscriber list

Parameters:

  • attributes (Hash)

    document_type, links, tags used to search existing subscriber lists



51
52
53
# File 'lib/gds_api/email_alert_api.rb', line 51

def create_subscriber_list(attributes)
  post_json("#{endpoint}/subscriber-lists", attributes)
end

#find_or_create_subscriber_list(attributes) ⇒ Object

Get or Post subscriber list

Parameters:

  • attributes (Hash)

    document_type, links, tags used to search existing subscriber lists



12
13
14
15
16
# File 'lib/gds_api/email_alert_api.rb', line 12

def find_or_create_subscriber_list(attributes)
  find_subscriber_list(attributes)
rescue GdsApi::HTTPNotFound
  create_subscriber_list(attributes)
end

#find_subscriber_list(attributes) ⇒ Object

Get a subscriber list

Parameters:

  • attributes (Hash)

    document_type, links, tags used to search existing subscriber lists



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gds_api/email_alert_api.rb', line 21

def find_subscriber_list(attributes)
  tags = attributes["tags"]
  links = attributes["links"]
  document_type = attributes["document_type"]
  email_document_supertype = attributes["email_document_supertype"]
  government_document_supertype = attributes["government_document_supertype"]
  gov_delivery_id = attributes["gov_delivery_id"]
  combine_mode = attributes["combine_mode"]

  if tags && links
    message = "please provide either tags or links (or neither), but not both"
    raise ArgumentError, message
  end

  params = {}
  params[:tags] = tags if tags
  params[:links] = links if links
  params[:document_type] = document_type if document_type
  params[:email_document_supertype] = email_document_supertype if email_document_supertype
  params[:government_document_supertype] = government_document_supertype if government_document_supertype
  params[:gov_delivery_id] = gov_delivery_id if gov_delivery_id
  params[:combine_mode] = combine_mode if combine_mode

  query_string = nested_query_string(params)
  get_json("#{endpoint}/subscriber-lists?" + query_string)
end

#get_latest_matching_subscription(id) ⇒ Hash

Get the latest Subscription that has the same subscriber_list and email as the Subscription associated with the ‘id` passed. This may or may not be the same Subscription.

}

Returns:

  • (Hash)

    subscription: { id subscriber_list subscriber created_at updated_at ended_at ended_reason frequency source



179
180
181
# File 'lib/gds_api/email_alert_api.rb', line 179

def get_latest_matching_subscription(id)
  get_json("#{endpoint}/subscriptions/#{id}/latest")
end

#get_subscriber_list(slug:) ⇒ Hash

Get a Subscriber List

}

Returns:

  • (Hash)

    subscriber_list: { id title gov_delivery_id created_at updated_at document_type tags links email_document_supertype government_document_supertype subscriber_count



143
144
145
# File 'lib/gds_api/email_alert_api.rb', line 143

def get_subscriber_list(slug:)
  get_json("#{endpoint}/subscriber-lists/#{slug}")
end

#get_subscription(id) ⇒ Hash

Get a Subscription

}

Returns:

  • (Hash)

    subscription: { id subscriber_list subscriber created_at updated_at ended_at ended_reason frequency source



160
161
162
# File 'lib/gds_api/email_alert_api.rb', line 160

def get_subscription(id)
  get_json("#{endpoint}/subscriptions/#{id}")
end

#get_subscriptions(id:, order: nil) ⇒ Hash

Get Subscriptions for a Subscriber

Parameters:

  • Subscriber (integer)

    id

  • Subscription (string)

    order - title, created_at

Returns:

  • (Hash)

    subscriber, subscriptions



189
190
191
192
193
194
195
# File 'lib/gds_api/email_alert_api.rb', line 189

def get_subscriptions(id:, order: nil)
  if order
    get_json("#{endpoint}/subscribers/#{id}/subscriptions?order=#{order}")
  else
    get_json("#{endpoint}/subscribers/#{id}/subscriptions")
  end
end

#send_subscriber_verification_email(address:, destination:, redirect: nil) ⇒ Hash

Verify a subscriber has control of a provided email

Parameters:

  • address (string)

    Address to send verification email to

  • destination (string)

    Path on GOV.UK that subscriber will be emailed

  • redirect (string, nil) (defaults to: nil)

    Path on GOV.UK to be encoded into the token for redirecting

Returns:

  • (Hash)

    subscriber



231
232
233
234
235
236
237
238
# File 'lib/gds_api/email_alert_api.rb', line 231

def send_subscriber_verification_email(address:, destination:, redirect: nil)
  post_json(
    "#{endpoint}/subscribers/auth-token",
    address: address,
    destination: destination,
    redirect: redirect,
  )
end

#send_subscription_verification_email(address:, frequency:, topic_id:) ⇒ Object

Verify a subscriber intends to be added to a subscription

return [Hash] subscription

Parameters:

  • address (string)

    Address to send verification email to

  • frequency (string)

    How often the subscriber wishes to be notified of new items

  • topic_id (string)

    The slugs/ID for the topic being subscribed to



248
249
250
251
252
253
254
255
# File 'lib/gds_api/email_alert_api.rb', line 248

def send_subscription_verification_email(address:, frequency:, topic_id:)
  post_json(
    "#{endpoint}/subscriptions/auth-token",
    address: address,
    frequency: frequency,
    topic_id: topic_id,
  )
end

#send_unpublish_message(message) ⇒ Object

Unpublishing alert

Used by email-alert-service to send a message to email-alert-api when an unpublishing message is put on the Rabbitmq queue by publishing-api

Parameters:

  • message (Hash)

    content_id



83
84
85
# File 'lib/gds_api/email_alert_api.rb', line 83

def send_unpublish_message(message)
  post_json("#{endpoint}/unpublish-messages", message)
end

#subscribe(subscriber_list_id:, address:, frequency: "immediately") ⇒ Hash

Subscribe

Returns:

  • (Hash)

    subscription_id



119
120
121
122
123
124
125
126
# File 'lib/gds_api/email_alert_api.rb', line 119

def subscribe(subscriber_list_id:, address:, frequency: "immediately")
  post_json(
    "#{endpoint}/subscriptions",
    subscriber_list_id: subscriber_list_id,
    address: address,
    frequency: frequency,
  )
end

#topic_matches(attributes) ⇒ Hash

Get topic matches

email_document_supertype, government_document_supertype

Parameters:

  • attributes (Hash)

    tags, links, document_type,

Returns:

  • (Hash)

    topics, enabled, disabled



93
94
95
96
# File 'lib/gds_api/email_alert_api.rb', line 93

def topic_matches(attributes)
  query_string = nested_query_string(attributes)
  get_json("#{endpoint}/topic-matches.json?#{query_string}")
end

#unsubscribe(uuid) ⇒ nil

Unsubscribe subscriber from subscription

Parameters:

  • Subscription (string)

    uuid

Returns:

  • (nil)


103
104
105
# File 'lib/gds_api/email_alert_api.rb', line 103

def unsubscribe(uuid)
  post_json("#{endpoint}/unsubscribe/#{uuid}")
end

#unsubscribe_subscriber(id) ⇒ nil

Unsubscribe subscriber from everything

Parameters:

  • Subscriber (integer)

    id

Returns:

  • (nil)


112
113
114
# File 'lib/gds_api/email_alert_api.rb', line 112

def unsubscribe_subscriber(id)
  delete_json("#{endpoint}/subscribers/#{id}")
end