Class: MaropostApi::Contacts

Inherits:
Object
  • Object
show all
Defined in:
lib/maropost_api/contacts.rb

Overview

Contains methods that get Contacts based on provided parameters. The method names themselves reveal the type of reports they are getting.

Instance Method Summary collapse

Constructor Details

#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"]) ⇒ Contacts

Creates a new instance of Reports class.

Parameters:

  • account (Integer) (defaults to: ENV["ACCOUNT"])

    is authentic user account id (Integer) provided by maropost.com

  • api_key (String) (defaults to: ENV["API_KEY"])

    is the auth token (String) that is validated on the server to authenticate the user



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

def initialize( = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
  MaropostApi.instance_variable_set(:@api_key, api_key)
  MaropostApi.instance_variable_set(:@account, )
end

Instance Method Details

#create_contact(email, first_name, last_name, phone, fax, uid = nil, custom_field = {}, add_tags = [], remove_tags = [], remove_from_dnm = false, **options) ⇒ Object

creates a contact

Parameters:

  • email (String)

    must be an email

  • first_name (String)

    Contact First Name

  • last_name (String)

    Contact Last Name

  • phone (String)

    Contact’s phone number

  • fax (String)

    Contacts’ fax number

  • uid (String) (defaults to: nil)

    Unique identifier

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

    list of custom_fields addable for the new contact

  • add_tags (Array) (defaults to: [])

    list of tags to be added to the contact

  • remove_tags (Array) (defaults to: [])

    list of tags to be removed from the contact

  • remove_from_dnm (Boolean) (defaults to: false)

    decides whether to remove contact from dnm or not

  • options (Hash)

    Key value pairs containing other different properties for the new contact



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
# File 'lib/maropost_api/contacts.rb', line 82

def create_contact(
    email,
    first_name,
    last_name,
    phone,
    fax,
    uid = nil,
    custom_field = {},
    add_tags = [],
    remove_tags = [],
    remove_from_dnm = false,
    **options
  )
  args = method(__method__).parameters
  body = {contact: {}}
  args.each do |arg|
    k = arg[1].to_s
    v = eval(k)
    body[:contact][k] = v unless v.nil?
  end
  # p body
  path = full_resource_path()
  
  MaropostApi.post_result(path, body)
end

#create_or_update_for_list(list_id, email, first_name, last_name, phone, fax, uid = nil, custom_field = {}, add_tags = [], remove_tags = [], remove_from_dnm = true, subscribe = true) ⇒ Object

creates a contact

Parameters:

  • list_id (Integer)

    id of the list for which to update/create contact

  • email (String)

    must be an email

  • first_name (String)

    Contact First Name

  • last_name (String)

    Contact Last Name

  • phone (String)

    Contact’s phone number

  • fax (String)

    Contacts’ fax number

  • uid (String) (defaults to: nil)

    Unique identifier

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

    list of custom_fields addable for the new contact

  • add_tags (Array) (defaults to: [])

    list of tags to be added to the contact

  • remove_tags (Array) (defaults to: [])

    list of tags to be removed from the contact

  • remove_from_dnm (Boolean) (defaults to: true)

    decides whether to remove contact from dnm or not

  • subscribe (Boolean) (defaults to: true)

    Flags the new contact as subscribed



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
# File 'lib/maropost_api/contacts.rb', line 122

def create_or_update_for_list(
    list_id, 
    email, 
    first_name, 
    last_name, 
    phone, 
    fax, 
    uid = nil,
    custom_field = {},
    add_tags = [], 
    remove_tags = [], 
    remove_from_dnm = true, 
    subscribe = true
  )
  args = method(__method__).parameters
  body = {contact: {}}
  args.each do |arg|
    k = arg[1].to_s
    next if k == "list_id"
    v = eval(k)
    body[:contact][k] = v unless v.nil?
  end
  
  email_existence = get_for_email(email)
  if email_existence.success
    contact_id = email_existence.data["id"]
    full_path = full_resource_path("/#{list_id}/contacts/#{contact_id}", "lists")
    
    MaropostApi.put_result(full_path, body)
  else
    full_path = full_resource_path("/#{list_id}/contacts", "lists")
    
    MaropostApi.post_result(full_path, body)
  end
  
end

#create_or_update_for_lists_and_workflows(email, first_name, last_name, phone, fax, uid = nil, custom_field = {}, add_tags = [], remove_tags = [], remove_from_dnm = true, **options) ⇒ Object

creates a contact

Parameters:

  • email (String)

    must be an email

  • first_name (String)

    Contact First Name

  • last_name (String)

    Contact Last Name

  • phone (String)

    Contact’s phone number

  • fax (String)

    Contacts’ fax number

  • uid (String) (defaults to: nil)

    Unique identifier

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

    list of custom_fields addable for the new contact

  • add_tags (Array) (defaults to: [])

    list of tags to be added to the contact

  • remove_tags (Array) (defaults to: [])

    list of tags to be removed from the contact

  • remove_from_dnm (Boolean) (defaults to: true)

    decides whether to remove contact from dnm or not

  • options (Hash)

    Key value pairs containing other different properties for the new contact



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/maropost_api/contacts.rb', line 172

def create_or_update_for_lists_and_workflows( 
    email,
    first_name,
    last_name,
    phone,
    fax,
    uid = nil,
    custom_field = {},
    add_tags = [],
    remove_tags = [],
    remove_from_dnm = true,
    **options
  )
  args = method(__method__).parameters
  body = {contact: {}}
  args.each do |arg|
    k = arg[1].to_s
    v = eval(k)
    body[:contact][k] = v unless v.nil?
  end
  
  email_existence = get_for_email email
  if email_existence.success
    contact_id = email_existence.data["id"]
    full_path = full_resource_path("/#{contact_id}")
    body[:contact].delete("options")
    body[:contact]["subscribe"] = options[:subscribe] if options.has_key? :subscribe
    
    MaropostApi.put_result(full_path, body)
  else
    full_path = full_resource_path

    MaropostApi.post_result(full_path, body)
  end
  
end

#delete_contact_for_uid(uid) ⇒ Object

deletes contact for uid

Parameters:

  • uid (String)

    unique identifier



287
288
289
290
291
292
# File 'lib/maropost_api/contacts.rb', line 287

def delete_contact_for_uid(uid)
  full_path = full_resource_path("/delete_all")
  query_params = MaropostApi.set_query_params({"uid" => uid})
  
  MaropostApi.delete_result(full_path, query_params)
end

#delete_from_all_lists(email) ⇒ Object

deletes contact from all the lists

Parameters:

  • email (String)

    gets deleted if matches the value provided



266
267
268
269
270
271
# File 'lib/maropost_api/contacts.rb', line 266

def delete_from_all_lists(email)
  full_path = full_resource_path('/delete_all')
  query_params = MaropostApi.set_query_params({"contact[email]" => email})
  
  MaropostApi.delete_result(full_path, query_params)
end

#delete_from_lists(contact_id, lists_to_delete_from) ⇒ Object

deletes contact from list

Parameters:

  • contact_id (Integer)

    Contact id to delete

  • list_to_delete_from (Integer)

    List id



277
278
279
280
281
282
# File 'lib/maropost_api/contacts.rb', line 277

def delete_from_lists(contact_id, lists_to_delete_from)
  full_path = full_resource_path("/#{contact_id}")
  query_params = MaropostApi.set_query_params({'list_ids' => lists_to_delete_from.join(',')})
  
  MaropostApi.delete_result(full_path, query_params)
end

#delete_list_contact(list_id, contact_id) ⇒ Object

deletes list for the given contact

Parameters:

  • list_id (Integer)

    List ID

  • contact_id (Integer)

    Contact ID



298
299
300
301
302
303
# File 'lib/maropost_api/contacts.rb', line 298

def delete_list_contact(list_id, contact_id)
  full_path = full_resource_path("/#{list_id}/contacts/#{contact_id}", "lists")
  query_params = MaropostApi.set_query_params
  
  MaropostApi.delete_result(full_path, query_params)
end

#get_clicks(contact_id, page) ⇒ Object

gets clicks for the provided

Parameters:

  • contact_id (Integer)

    unique id of the contact

  • page (Integer)

    number that decides which page or result to retrieve



40
41
42
43
44
45
# File 'lib/maropost_api/contacts.rb', line 40

def get_clicks(contact_id, page)
  full_path = full_resource_path("/#{contact_id}/click_report")
  query_params = MaropostApi.set_query_params({page: page})
  
  MaropostApi.get_result(full_path, query_params)
end

#get_contact_for_list(list_id, contact_id) ⇒ Object

gets contacts for the provided

Parameters:

  • list_id (Integer)

    unique id of the list

  • contact_id (Integer)

    unique contact id



62
63
64
65
66
67
# File 'lib/maropost_api/contacts.rb', line 62

def get_contact_for_list(list_id, contact_id)
  full_path = full_resource_path("/#{list_id}/contacts/#{contact_id}", 'lists')
  query_params = MaropostApi.set_query_params
  
  MaropostApi.get_result(full_path, query_params)
end

#get_for_email(email) ⇒ Object

gets contact for the provided

Parameters:

  • email (String)

    must be an email



18
19
20
21
22
23
# File 'lib/maropost_api/contacts.rb', line 18

def get_for_email(email)
  full_path = full_resource_path("/email")
  query_params = MaropostApi.set_query_params({"contact[email]" => email})
  
  MaropostApi.get_result(full_path, query_params)
end

#get_for_list(list_id, page) ⇒ Object

gets contacts for the provided

Parameters:

  • list_id (Integer)

    unique id of the list

  • page (Integer)

    number that decides which page or result to retrieve



51
52
53
54
55
56
# File 'lib/maropost_api/contacts.rb', line 51

def get_for_list(list_id, page)
  full_path = full_resource_path("/#{list_id}/contacts", 'lists')
  query_params = MaropostApi.set_query_params({page: page})
  
  MaropostApi.get_result(full_path, query_params)
end

#get_opens(contact_id, page) ⇒ Object

gets opens for the provided

Parameters:

  • contact_id (Integer)

    unique id of the contact

  • page (Integer)

    number that decides which page or result to retrieve



29
30
31
32
33
34
# File 'lib/maropost_api/contacts.rb', line 29

def get_opens(contact_id, page)
  full_path = full_resource_path("/#{contact_id}/open_report")
  query_params = MaropostApi.set_query_params({page: page})
  
  MaropostApi.get_result(full_path, query_params)
end

#unsubscribe_all(contact_field_value, contact_field_name) ⇒ Object

unsubscribes all those contacts matching

Parameters:

  • contact_field_value (String|Integer)

    the value of the contact field name

  • contact_field_name (String|Integer)

    the name of the field to query the contact for



256
257
258
259
260
261
# File 'lib/maropost_api/contacts.rb', line 256

def unsubscribe_all(contact_field_value, contact_field_name)
  full_path = full_resource_path('/unsubscribe_all')
  query_params = MaropostApi.set_query_params({"contact[#{contact_field_name}]" => contact_field_value})
  
  MaropostApi.put_result(full_path, {}, query_params)
end

#update_for_list_and_contact(list_id, contact_id, email, first_name, last_name, phone, fax, uid = nil, custom_field = {}, add_tags = [], remove_tags = [], remove_from_dnm = true, subscribe = true) ⇒ Object

creates a contact

Parameters:

  • list_id (Integer)

    id of the list for which to update/create contact

  • email (String)

    must be an email

  • first_name (String)

    Contact First Name

  • last_name (String)

    Contact Last Name

  • phone (String)

    Contact’s phone number

  • fax (String)

    Contacts’ fax number

  • uid (String) (defaults to: nil)

    Unique identifier

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

    list of custom_fields addable for the new contact

  • add_tags (Array) (defaults to: [])

    list of tags to be added to the contact

  • remove_tags (Array) (defaults to: [])

    list of tags to be removed from the contact

  • remove_from_dnm (Boolean) (defaults to: true)

    decides whether to remove contact from dnm or not

  • subscribe (Boolean) (defaults to: true)

    Flags the new contact as subscribed



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/maropost_api/contacts.rb', line 223

def update_for_list_and_contact(
    list_id,
    contact_id,
    email,
    first_name,
    last_name,
    phone,
    fax,
    uid = nil,
    custom_field = {},
    add_tags = [],
    remove_tags = [],
    remove_from_dnm = true,
    subscribe = true
  )
  args = method(__method__).parameters
  body = {contact: {}}
  args.each do |arg|
    k = arg[1].to_s
    next if ["list_id", "contact_id"].include? k
    v = eval(k)
    body[:contact][k] = v unless v.nil?
  end
  
  full_path = full_resource_path("/#{list_id}/contacts/#{contact_id}", "lists")
  
  MaropostApi.put_result(full_path, body)
end