Class: ConstantContact::Services::ContactService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/constantcontact/services/contact_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#api_client

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from ConstantContact::Services::BaseService

Instance Method Details

#add_contact(contact, params = {}) ⇒ Contact

Add a new contact to the Constant Contact account

Parameters:

  • contact (Contact)
    • Contact to add

  • params (Boolean) (defaults to: {})
    • query params to be appended to the request

Returns:

  • (Contact)
[View source]

46
47
48
49
50
51
52
# File 'lib/constantcontact/services/contact_service.rb', line 46

def add_contact(contact, params = {})
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.contacts')
  url = build_url(url, params)
  payload = contact.to_json
  response = RestClient.post(url, payload, get_headers())
  Components::Contact.create(JSON.parse(response.body))
end

#delete_contact(contact_id) ⇒ Boolean

Delete contact details for a specific contact

Parameters:

  • contact_id (Integer)
    • Unique contact id

Returns:

  • (Boolean)
[View source]

58
59
60
61
62
63
# File 'lib/constantcontact/services/contact_service.rb', line 58

def delete_contact(contact_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.contact'), contact_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#delete_contact_from_list(contact_id, list_id) ⇒ Boolean

Delete a contact from a specific contact list

Parameters:

  • contact_id (Integer)
    • Contact id to be removed

  • list_id (Integer)
    • ContactList id to remove the contact from

Returns:

  • (Boolean)
[View source]

81
82
83
84
85
86
# File 'lib/constantcontact/services/contact_service.rb', line 81

def delete_contact_from_list(contact_id, list_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.contact_list'), contact_id, list_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#delete_contact_from_lists(contact_id) ⇒ Boolean

Delete a contact from all contact lists

Parameters:

  • contact_id (Integer)
    • Contact id to be removed from lists

Returns:

  • (Boolean)
[View source]

69
70
71
72
73
74
# File 'lib/constantcontact/services/contact_service.rb', line 69

def delete_contact_from_lists(contact_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.contact_lists'), contact_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#get_contact(contact_id) ⇒ Contact

Get contact details for a specific contact

Parameters:

  • contact_id (Integer)
    • Unique contact id

Returns:

  • (Contact)
[View source]

33
34
35
36
37
38
39
# File 'lib/constantcontact/services/contact_service.rb', line 33

def get_contact(contact_id)
  url = Util::Config.get('endpoints.base_url') +
        sprintf(Util::Config.get('endpoints.contact'), contact_id)
  url = build_url(url)
  response = RestClient.get(url, get_headers())
  Components::Contact.create(JSON.parse(response.body))
end

#get_contacts(params = {}) ⇒ ResultSet<Contact>

Get an array of contacts

Parameters:

  • params (Hash) (defaults to: {})
    • query parameters to be appended to the request

Returns:

  • (ResultSet<Contact>)
[View source]

14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/constantcontact/services/contact_service.rb', line 14

def get_contacts(params = {})
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.contacts')
  url = build_url(url, params)

  response = RestClient.get(url, get_headers())
  body = JSON.parse(response.body)

  contacts = []
  body['results'].each do |contact|
    contacts << Components::Contact.create(contact)
  end

  Components::ResultSet.new(contacts, body['meta'])
end

#update_contact(contact, params = {}) ⇒ Contact

Update contact details for a specific contact

Parameters:

  • contact (Contact)
    • Contact to be updated

  • params (Hash) (defaults to: {})
    • query params to be appended to the request

Returns:

  • (Contact)
[View source]

93
94
95
96
97
98
99
# File 'lib/constantcontact/services/contact_service.rb', line 93

def update_contact(contact, params = {})
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.contact'), contact.id)
  url = build_url(url, params)
  payload = contact.to_json
  response = RestClient.put(url, payload, get_headers())
  Components::Contact.create(JSON.parse(response.body))
end