Module: Resend::Contacts

Defined in:
lib/resend/contacts.rb,
lib/resend/contacts/topics.rb,
lib/resend/contacts/segments.rb

Overview

Contacts api wrapper

Defined Under Namespace

Modules: Segments, Topics

Class Method Summary collapse

Class Method Details

.create(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/resend/contacts.rb', line 8

def create(params)
  if params[:audience_id]
    path = "audiences/#{params[:audience_id]}/contacts"
    # Audience-scoped contacts don't support properties
    payload = params.reject { |key, _| key == :properties }
  else
    path = "contacts"
    payload = params
  end
  Resend::Request.new(path, payload, "post").perform
end

.get(params = {}) ⇒ Object

Examples:

Get contact by ID

Resend::Contacts.get(id: "contact_123")

Get contact scoped to an audience

Resend::Contacts.get(id: "contact_123", audience_id: "aud_456")

Parameters:

  • params (Hash) (defaults to: {})

    the parameters

Options Hash (params):

  • :id (String)

    either the contact id or contact’s email (required)

  • :audience_id (String)

    optional audience id to scope the operation

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/resend/contacts.rb', line 34

def get(params = {})
  raise ArgumentError, "Missing `id` or `email` field" if params[:id].nil? && params[:email].nil?

  audience_id = params[:audience_id]
  contact_id = params[:id] || params[:email]
  path = if audience_id
           "audiences/#{audience_id}/contacts/#{contact_id}"
         else
           "contacts/#{contact_id}"
         end
  Resend::Request.new(path, {}, "get").perform
end

.list(params = {}) ⇒ Object

Examples:

List all contacts

Resend::Contacts.list

List contacts with pagination

Resend::Contacts.list(limit: 10)

List contacts scoped to an audience

Resend::Contacts.list(audience_id: "aud_456", limit: 10)

Parameters:

  • params (Hash) (defaults to: {})

    optional parameters including pagination

Options Hash (params):

  • :audience_id (String)

    optional audience id to scope the operation

  • :limit (Integer)

    number of records to return

  • :cursor (String)

    pagination cursor



65
66
67
68
69
70
71
72
73
# File 'lib/resend/contacts.rb', line 65

def list(params = {})
  audience_id = params[:audience_id]
  path = if audience_id
           Resend::PaginationHelper.build_paginated_path("audiences/#{audience_id}/contacts", params)
         else
           Resend::PaginationHelper.build_paginated_path("contacts", params)
         end
  Resend::Request.new(path, {}, "get").perform
end

.remove(params = {}) ⇒ Object

Examples:

Remove contact by ID

Resend::Contacts.remove(id: "contact_123")

Remove contact scoped to an audience

Resend::Contacts.remove(id: "contact_123", audience_id: "aud_456")

Parameters:

  • params (Hash) (defaults to: {})

    the parameters

Options Hash (params):

  • :id (String)

    either the contact id or contact email (required)

  • :audience_id (String)

    optional audience id to scope the operation

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/resend/contacts.rb', line 89

def remove(params = {})
  raise ArgumentError, "Missing `id` or `email` field" if params[:id].nil? && params[:email].nil?

  audience_id = params[:audience_id]
  contact_id = params[:id] || params[:email]
  path = if audience_id
           "audiences/#{audience_id}/contacts/#{contact_id}"
         else
           "contacts/#{contact_id}"
         end
  Resend::Request.new(path, {}, "delete").perform
end

.update(params) ⇒ Object

Parameters:

  • params (Hash)

    the contact params

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/resend/contacts.rb', line 107

def update(params)
  raise ArgumentError, "Missing `id` or `email` field" if params[:id].nil? && params[:email].nil?

  contact_id = params[:id] || params[:email]
  if params[:audience_id]
    path = "audiences/#{params[:audience_id]}/contacts/#{contact_id}"
    # Audience-scoped contacts don't support properties
    payload = params.reject { |key, _| key == :properties }
  else
    path = "contacts/#{contact_id}"
    payload = params
  end
  Resend::Request.new(path, payload, "patch").perform
end