Ruby Requires Chilkat v11.0.0+
Ruby
Constant Contact V3: Create a contact
See more Constant Contact Examples
Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same 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()
body = Chilkat::CkJsonObject.new()
body.UpdateString("create_source","Account")
body.UpdateString("first_name","Alex")
body.UpdateString("last_name","Rivera")
body.UpdateString("email_address.address","reader+newsletter@example.com")
# This sample assumes the account has recorded a qualifying existing relationship.
body.UpdateString("email_address.permission_to_send","implicit")
body.UpdateString("street_addresses[0].kind","work")
body.UpdateString("street_addresses[0].street","100 Main Street")
body.UpdateString("street_addresses[0].city","Boston")
body.UpdateString("street_addresses[0].state","MA")
body.UpdateString("street_addresses[0].country","United States")
success = http.HttpJson("POST","https://api.cc.email/v3/contacts",body,"application/json",resp)
if (resp.get_StatusCode() == 409)
print "Contact already exists. Look up the email address to obtain its ID." + "\n";
exit
end
if ((success != true) or (resp.get_StatusCode() != 201))
print "Expected HTTP 201. Received " + resp.get_StatusCode().to_s() + "\n";
if (resp.get_StatusCode() == 401)
print "Refresh the saved access token or authorize again." + "\n";
end
exit
end
created = Chilkat::CkJsonObject.new()
success = created.Load(resp.bodyStr())
if (success != true)
print "Expected a JSON response." + "\n";
exit
end
if ((created.JsonTypeOf("contact_id") != 1) or (created.StringOfEquals("contact_id","",true) == true))
print "Missing contact_id in the response." + "\n";
exit
end
print "Created contact ID: " + created.stringOf("contact_id") + "\n";