-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontacts_api.rb
More file actions
178 lines (152 loc) · 5.53 KB
/
contacts_api.rb
File metadata and controls
178 lines (152 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'mailtrap'
account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
contact_lists = Mailtrap::ContactListsAPI.new(account_id, client)
contacts = Mailtrap::ContactsAPI.new(account_id, client)
contact_fields = Mailtrap::ContactFieldsAPI.new(account_id, client)
contact_imports = Mailtrap::ContactImportsAPI.new(account_id, client)
# Set your API credentials as environment variables
# export MAILTRAP_API_KEY='your-api-key'
# export MAILTRAP_ACCOUNT_ID=your-account-id
# contact_lists = Mailtrap::ContactListsAPI.new
# contacts = Mailtrap::ContactsAPI.new
# contact_fields = Mailtrap::ContactFieldsAPI.new
# contact_imports = Mailtrap::ContactImportsAPI.new
# Create new contact list
list = contact_lists.create(name: 'Test List')
# => #<struct Mailtrap::ContactList id=1, name="Test List">
# Get all contact lists
contact_lists.list
# => [#<struct Mailtrap::ContactList id=1, name="Test List">]
# Update contact list
contact_lists.update(list.id, name: 'Test List Updated')
# => #<struct Mailtrap::ContactList id=1, name="Test List Updated">
# Get contact list
list = contact_lists.get(list.id)
# => #<struct Mailtrap::ContactList id=1, name="Test List Updated">
# Delete contact list
contact_lists.delete(list.id)
# Create new contact field
field = contact_fields.create(name: 'Nickname', data_type: 'text', merge_tag: 'nickname')
# => #<struct Mailtrap::ContactField id=1, name="Nickname", data_type="text", merge_tag="nickname">
# Get all contact fields
contact_fields.list
# => [#<struct Mailtrap::ContactField id=1, name="Nickname", data_type="text", merge_tag="nickname">]
# Update contact field
contact_fields.update(field.id, name: 'Nickname 2', merge_tag: 'nickname')
# => #<struct Mailtrap::ContactField id=1, name="Nickname 2", data_type="text", merge_tag="nickname">
# Get contact field
field = contact_fields.get(field.id)
# => #<struct Mailtrap::ContactField id=1, name="Nickname 2", data_type="text", merge_tag="nickname">
# Create new contact with all possible fields
contact = contacts.create(
email: 'test@example.com',
fields: { field.merge_tag => 'John Doe' },
list_ids: [list.id]
)
# => #<struct Mailtrap::Contact
# id=1,
# email="test@example.com",
# fields={ "nickname" => "John Doe" },
# list_ids=[1],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212345>
contact.newly_created? # => true
# Get contact
contact = contacts.get(contact.id)
# => #<struct Mailtrap::Contact
# id=1,
# email="test@example.com",
# fields={ "nickname" => "John Doe" },
# list_ids=[1],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212345>
# Update contact using id
updated_contact = contacts.upsert(
contact.id,
email: 'test2@example.com',
fields: { field.merge_tag => 'Jane Doe' }
)
# => #<struct Mailtrap::Contact
# id=1,
# email="test2@example.com",
# fields={ "nickname" => "Jane Doe" },
# list_ids=[1],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212350>
updated_contact.newly_created? # => false
# Update contact using email
contacts.upsert(
updated_contact.email,
email: 'test3@example.com',
fields: { field.merge_tag => 'Jane Doe' }
)
# => #<struct Mailtrap::Contact
# id=1,
# email="test3@example.com",
# fields={ "nickname" => "Jane Doe" },
# list_ids=[1],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212355>
updated_contact.newly_created? # => false
# Remove contact from lists
contacts.remove_from_lists(contact.id, [list.id])
# => #<struct Mailtrap::Contact
# id=1,
# email="test3@example.com",
# fields={ "nickname" => "Jane Doe" },
# list_ids=[],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212360>
# Add contact to lists
contacts.add_to_lists(contact.id, [list.id])
# => #<struct Mailtrap::Contact
# id=1,
# email="test3@example.com",
# fields={ "nickname" => "Jane Doe" },
# list_ids=[1],
# status="subscribed",
# created_at=1721212345,
# updated_at=1721212365>
# Delete contact
contacts.delete(contact.id)
# Delete contact field
contact_fields.delete(field.id)
# Create a new contact import using ContactsImportRequest builder
import_request = Mailtrap::ContactsImportRequest.new.tap do |req|
req.upsert(email: 'john.doe@example.com', fields: { first_name: 'John' })
.add_to_lists(email: 'john.doe@example.com', list_ids: [1])
.remove_from_lists(email: 'jane.smith@example.com', list_ids: [2])
req.upsert(email: 'jane.smith@example.com', fields: { first_name: 'Jane' })
.add_to_lists(email: 'jane.smith@example.com', list_ids: [1])
.remove_from_lists(email: 'jane.smith@example.com', list_ids: [2])
end
# Execute the import
contact_import = contact_imports.create(import_request)
# => #<struct Mailtrap::ContactImport
# id=1,
# status="created",
# created_contacts_count=nil,
# updated_contacts_count=nil,
# contacts_over_limit_count=nil>
# Wait for the import to complete (if needed)
# Get the import status
contact_imports.get(contact_import.id)
# => #<struct Mailtrap::ContactImport
# id=1,
# status="finished",
# created_contacts_count=2,
# updated_contacts_count=0,
# contacts_over_limit_count=0>
# Import using plain hash
contact_imports.create(
[
{ email: 'john@example.com', fields: { first_name: 'John' }, list_ids_included: [1], list_ids_excluded: [2] },
{ email: 'jane@example.com', fields: { first_name: 'Jane' }, list_ids_included: [1], list_ids_excluded: [2] }
]
)