Ruby Requires Chilkat v11.0.0+
Ruby
Constant Contact V3: Find a contact list by name
See more Constant Contact Examples
Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = Chilkat::CkSecrets.new()
vault.put_Location("local_manager")
vault.put_AutoSplit(true)
entry = Chilkat::CkJsonObject.new()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = Chilkat::CkJsonObject.new()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != true)
print "Run the desktop authorization example using this account and store." + "\n";
exit
end
if (tokens.JsonTypeOf("access_token") != 1)
print "The saved authorization has no access token." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_AuthToken(tokens.stringOf("access_token"))
resp = Chilkat::CkHttpResponse.new()
# Use the full display name. AddParam encodes spaces and punctuation.
listName = "Workshop announcements"
query = Chilkat::CkHttpRequest.new()
query.put_HttpVerb("GET")
query.AddParam("name",listName)
query.AddParam("status","active")
query.AddParam("include_membership_count","active")
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp)
if ((success != true) and (resp.get_StatusCode() <= 0))
print "Look up list: HTTP request failed." + "\n";
exit
end
if ((success != true) or (resp.get_StatusCode() != 200))
print "Look up list: HTTP " + resp.get_StatusCode().to_s() + " " + resp.bodyStr() + "\n";
exit
end
matches = Chilkat::CkJsonObject.new()
success = matches.Load(resp.bodyStr())
if (success != true)
print "Expected a JSON object in the response." + "\n";
exit
end
if (matches.SizeOfArray("lists") == 0)
print "No active email list named " + listName + "\n";
exit
end
if ((matches.SizeOfArray("lists") != 1) or (matches.StringOfEquals("lists[0].name",listName,true) != true))
print "Expected one exact list name." + "\n";
exit
end
listId = matches.stringOf("lists[0].list_id")
if (listId == "")
print "Missing list ID." + "\n";
exit
end
print listName + " | List ID: " + listId + " | Mailable contacts: " + matches.IntOf("lists[0].membership_count").to_s() + "\n";