Class: RelayGrid::Resources::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/relaygrid/resources/users.rb

Overview

Recipient management. notify upserts the recipient for you, so these are only needed to manage users outside a send -- backfilling, renaming, or honouring a deletion request.

Everywhere here, id means your identifier for the user (the API's external_user_id). RelayGrid's internal ids stay internal.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Users

Returns a new instance of Users.



12
13
14
# File 'lib/relaygrid/resources/users.rb', line 12

def initialize(client)
  @client = client
end

Instance Method Details

#contacts(id) ⇒ Array<Hash>

Returns the user's live contacts.

Parameters:

  • id (String)

    your identifier for the user

Returns:

  • (Array<Hash>)

    the user's live contacts

Raises:



74
75
76
# File 'lib/relaygrid/resources/users.rb', line 74

def contacts(id)
  Array(@client.get("#{contacts_path(id)}")["contacts"])
end

#delete(id) ⇒ Boolean

Soft-delete the recipient. Their message history is retained.

Returns:

  • (Boolean)

    false when there was no such user



53
54
55
56
57
58
59
# File 'lib/relaygrid/resources/users.rb', line 53

def delete(id)
  existing = get(id)
  return false unless existing

  @client.delete("/api/v1/notification_users/#{existing['id']}")
  true
end

#delete_contact(id, channel_type:) ⇒ Boolean

Returns false when there was no such contact.

Returns:

  • (Boolean)

    false when there was no such contact



97
98
99
100
101
102
103
# File 'lib/relaygrid/resources/users.rb', line 97

def delete_contact(id, channel_type:)
  existing = contacts(id).find { |contact| contact["channel_type"] == channel_type.to_s }
  return false unless existing

  @client.delete("#{contacts_path(id)}/#{existing['id']}")
  true
end

#get(id) ⇒ Hash?

Parameters:

  • id (String)

    your identifier for the user

Returns:

  • (Hash, nil)


23
24
25
# File 'lib/relaygrid/resources/users.rb', line 23

def get(id)
  list.find { |user| user["external_user_id"] == id.to_s }
end

#listArray<Hash>

Returns every recipient in the account.

Returns:

  • (Array<Hash>)

    every recipient in the account



17
18
19
# File 'lib/relaygrid/resources/users.rb', line 17

def list
  Array(@client.get("/api/v1/notification_users")["notification_users"])
end

#set_contact(id, channel_type:, value:) ⇒ Hash

Create or repoint the user's contact for one channel type.

Parameters:

  • channel_type (String)

    "email"

  • value (String)

    the address deliveries on that channel are sent to

Returns:

  • (Hash)

    the contact

Raises:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/relaygrid/resources/users.rb', line 84

def set_contact(id, channel_type:, value:)
  path = contacts_path(id)
  existing = contacts(id).find { |contact| contact["channel_type"] == channel_type.to_s }
  body = { contact: { channel_type: channel_type.to_s, value: value } }

  if existing
    @client.patch("#{path}/#{existing['id']}", body: body)["contact"]
  else
    @client.post(path, body: body)["contact"]
  end
end

#upsert(id:, first_name:, last_name:, middle_name: nil) ⇒ Hash

Create the recipient, or update their names if they already exist.

Returns:

  • (Hash)

    the notification user



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/relaygrid/resources/users.rb', line 30

def upsert(id:, first_name:, last_name:, middle_name: nil)
  existing = get(id)
  attributes = { first_name: first_name, last_name: last_name }
  attributes[:middle_name] = middle_name unless middle_name.nil?

  if existing
    body = @client.patch(
      "/api/v1/notification_users/#{existing['id']}",
      body: { notification_user: attributes }
    )
  else
    body = @client.post(
      "/api/v1/notification_users",
      body: { notification_user: attributes.merge(external_user_id: id.to_s) }
    )
  end

  body["notification_user"]
end