Class: ConstantContact::Services::ListService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/constantcontact/services/list_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_list(list) ⇒ ContactList

Add a new list to the Constant Contact account

Parameters:

  • list (ContactList)

Returns:

  • (ContactList)


29
30
31
32
33
34
35
# File 'lib/constantcontact/services/list_service.rb', line 29

def add_list(list)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.lists')
  url = build_url(url)
  payload = list.to_json
  response = RestClient.post(url, payload, get_headers())
  Components::ContactList.create(JSON.parse(response.body))
end

#get_contacts_from_list(list_id, params = nil) ⇒ Array<Contact>

Get all contacts from an individual list

Parameters:

  • list_id (Integer)
    • list id to retrieve contacts for

  • params (Hash) (defaults to: nil)
    • query parameters to attach to request

Returns:

  • (Array<Contact>)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/constantcontact/services/list_service.rb', line 65

def get_contacts_from_list(list_id, params = nil)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.list_contacts'), list_id)
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  contacts = []
  body = JSON.parse(response.body)
  body['results'].each do |contact|
    contacts << Components::Contact.create(contact)
  end
  Components::ResultSet.new(contacts, body['meta'])
end

#get_list(list_id) ⇒ ContactList

Get an individual contact list

Parameters:

  • list_id (Integer)
    • list id

Returns:

  • (ContactList)


53
54
55
56
57
58
# File 'lib/constantcontact/services/list_service.rb', line 53

def get_list(list_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.list'), list_id)
  url = build_url(url)
  response = RestClient.get(url, get_headers())
  Components::ContactList.create(JSON.parse(response.body))
end

#get_lists(params = {}) ⇒ Array<ContactList>

Get lists within an account

Parameters:

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

Returns:

  • (Array<ContactList>)


14
15
16
17
18
19
20
21
22
23
# File 'lib/constantcontact/services/list_service.rb', line 14

def get_lists(params = {})
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.lists')
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  lists = []
  JSON.parse(response.body).each do |contact|
    lists << Components::ContactList.create(contact)
  end
  lists
end

#update_list(list) ⇒ ContactList

Update a Contact List

Parameters:

  • list (ContactList)
    • ContactList to be updated

Returns:

  • (ContactList)


41
42
43
44
45
46
47
# File 'lib/constantcontact/services/list_service.rb', line 41

def update_list(list)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.list'), list.id)
  url = build_url(url)
  payload = list.to_json
  response = RestClient.put(url, payload, get_headers())
  Components::ContactList.create(JSON.parse(response.body))
end