Class: Mailtrap::ContactsImportRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/mailtrap/contacts_import_request.rb

Overview

A builder class for creating contact import requests Allows you to build a collection of contacts with their associated fields and list memberships

Instance Method Summary collapse

Constructor Details

#initializeContactsImportRequest

Returns a new instance of ContactsImportRequest.



7
8
9
10
11
# File 'lib/mailtrap/contacts_import_request.rb', line 7

def initialize
  @data = Hash.new do |h, k|
    h[k] = { email: k, fields: {}, list_ids_included: [], list_ids_excluded: [] }
  end
end

Instance Method Details

#add_to_lists(email:, list_ids:) ⇒ ContactsImportRequest

Adds a contact to the specified lists

Parameters:

  • email (String)

    The contact’s email address

  • list_ids (Array<Integer>)

    Array of list IDs to add the contact to

Returns:



30
31
32
33
34
35
36
# File 'lib/mailtrap/contacts_import_request.rb', line 30

def add_to_lists(email:, list_ids:)
  validate_email!(email)

  append_list_ids email:, list_ids:, key: :list_ids_included

  self
end

#remove_from_lists(email:, list_ids:) ⇒ ContactsImportRequest

Removes a contact from the specified lists

Parameters:

  • email (String)

    The contact’s email address

  • list_ids (Array<Integer>)

    Array of list IDs to remove the contact from

Returns:



42
43
44
45
46
47
48
# File 'lib/mailtrap/contacts_import_request.rb', line 42

def remove_from_lists(email:, list_ids:)
  validate_email!(email)

  append_list_ids email:, list_ids:, key: :list_ids_excluded

  self
end

#to_aArray<Hash>

Returns Array of contact objects ready for import.

Returns:

  • (Array<Hash>)

    Array of contact objects ready for import



51
52
53
# File 'lib/mailtrap/contacts_import_request.rb', line 51

def to_a
  @data.values
end

#upsert(email:, fields: {}) ⇒ ContactsImportRequest

Creates or updates a contact with the provided email and fields

Parameters:

  • email (String)

    The contact’s email address

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

    Contact fields in the format: field_merge_tag => String, Integer, Float, Boolean, or ISO-8601 date string (yyyy-mm-dd)

Returns:



18
19
20
21
22
23
24
# File 'lib/mailtrap/contacts_import_request.rb', line 18

def upsert(email:, fields: {})
  validate_email!(email)

  @data[email][:fields].merge!(fields)

  self
end