Class: ConstantContact::Services::ListService

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

Class Method Summary collapse

Class Method Details

.add_list(list) ⇒ ContactList

Add a new list to the Constant Contact account

Parameters:

  • list (ContactList)

Returns:

  • (ContactList)


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

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


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

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)


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

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


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

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)


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

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