Class: ConstantContact::Services::ContactService

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

Class Method Summary collapse

Class 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)


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

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)


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

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)


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

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)


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

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)


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

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>)


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

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)


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

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