Module: Millionsend::Contacts

Defined in:
lib/millionsend/contacts.rb

Overview

Contacts are team-global and addressable by id or by email (email wins when an update hash carries both).

Defined Under Namespace

Modules: Batch, Segments, Topics

Class Method Summary collapse

Class Method Details

.address(id_or_email) ⇒ Object

One /contacts/batch/get entry. The wire wants { id: } and { email: } told apart, and only an email carries "@".



66
67
68
69
# File 'lib/millionsend/contacts.rb', line 66

def address(id_or_email)
  value = unwrap(id_or_email)
  value.to_s.include?("@") ? { email: value } : { id: value }
end

.create(params) ⇒ Object

POST /contacts — email, first_name, last_name, unsubscribed, properties, segments: [{ id: }], topics: [{ id:, subscription: }].



10
11
12
# File 'lib/millionsend/contacts.rb', line 10

def create(params)
  Millionsend::Request.new(method: :post, path: "/contacts", body: params).perform
end

.get(id_or_email) ⇒ Object

GET a single contact by id or email.



15
16
17
# File 'lib/millionsend/contacts.rb', line 15

def get(id_or_email)
  Millionsend::Request.new(method: :get, path: member_path(id_or_email)).perform
end

.list(options = {}) ⇒ Object

GET /contacts — accepts limit:/after:/before: and include: (["properties", "topics"]) to carry the property map and the topic subscriptions on every item, as Contacts.get and Topics.list return them.



39
40
41
42
# File 'lib/millionsend/contacts.rb', line 39

def list(options = {})
  query = Millionsend::Util.list_query(options).merge(include: Millionsend::Util.include_query(options[:include]))
  Millionsend::Request.new(method: :get, path: "/contacts", query: query).perform
end

.member_path(id_or_email) ⇒ Object

Every member method also accepts resend-ruby's addressing hash ({ id: } / { email: } / { contact_id: }) in place of the bare value.



60
61
62
# File 'lib/millionsend/contacts.rb', line 60

def member_path(id_or_email)
  "/contacts/#{Millionsend::Util.encode(unwrap(id_or_email))}"
end

POST /contacts/:id_or_email/preferences-link — the contact's hosted preference page, { object: "preferences_link", contact:, url: }. The url is a contact-scoped capability with no expiry: hand it only to that contact. 422 when the instance cannot build hosted links.



54
55
56
# File 'lib/millionsend/contacts.rb', line 54

def preferences_link(id_or_email)
  Millionsend::Request.new(method: :post, path: "#{member_path(id_or_email)}/preferences-link").perform
end

.remove(id_or_email, options = {}) ⇒ Object

DELETE a contact by id or email. The contact's emails stay in the send log; erase: true also scrubs the address from email history, event payloads and API logs (a GDPR/LGPD erasure). The flag rides flat or in resend-ruby's addressing hash: remove(email: "[email protected]", erase: true).



31
32
33
34
# File 'lib/millionsend/contacts.rb', line 31

def remove(id_or_email, options = {})
  erase = options.key?(:erase) ? options[:erase] : (id_or_email[:erase] if id_or_email.is_a?(Hash))
  Millionsend::Request.new(method: :delete, path: member_path(id_or_email), query: { erase: erase }).perform
end

.topics_update(id_or_email, topics) ⇒ Object

PATCH /contacts/:id_or_email/topics with a bare array of { id:, subscription: }. Contacts::Topics.update is the resend-ruby shape.



46
47
48
# File 'lib/millionsend/contacts.rb', line 46

def topics_update(id_or_email, topics)
  Millionsend::Request.new(method: :patch, path: "#{member_path(id_or_email)}/topics", body: topics).perform
end

.update(params) ⇒ Object

PATCH a contact. Addressing keys (:id, :email) are pulled out; everything else is the body. A nil value clears a field; omit a key to leave it unchanged.



22
23
24
25
# File 'lib/millionsend/contacts.rb', line 22

def update(params)
  body = params.reject { |k, _| [:id, :email].include?(k) }
  Millionsend::Request.new(method: :patch, path: member_path(params), body: body).perform
end