Ruby Requires Chilkat v11.0.0+
Ruby
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
# Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY")
resp = Chilkat::CkHttpResponse.new()
# Take the address from the authenticated account and the group ID from configuration.
address = "reader@example.com"
http.SetUrlVar("group","12345")
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",resp)
if (success != true)
if (resp.get_StatusCode() == 0)
print http.lastErrorText() + "\n";
else
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
end
exit
end
if (resp.get_StatusCode() != 200)
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
exit
end
groupInfo = Chilkat::CkJsonObject.new()
success = groupInfo.Load(resp.bodyStr())
if (success != true)
print "Unexpected response: expected JSON." + "\n";
exit
end
if (groupInfo.StringOfEquals("name","Product updates",true) != true)
print "Unexpected unsubscribe group; no preference was changed." + "\n";
exit
end
addresses = Chilkat::CkJsonObject.new()
addresses.UpdateString("recipient_emails[0]",address)
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",addresses,"application/json",resp)
if (success != true)
if (resp.get_StatusCode() == 0)
print http.lastErrorText() + "\n";
else
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
end
exit
end
if (resp.get_StatusCode() != 201)
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
exit
end
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"application/json",resp)
if (success != true)
if (resp.get_StatusCode() == 0)
print http.lastErrorText() + "\n";
else
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
end
exit
end
if (resp.get_StatusCode() != 200)
print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
exit
end
matches = Chilkat::CkJsonArray.new()
success = matches.Load(resp.bodyStr())
if (success != true)
print "Unexpected response: expected JSON." + "\n";
exit
end
if (matches.get_Size() == 1)
print "Confirmed opt-out: " + matches.stringAt(0) + "\n";
else
print "Opt-out not confirmed. Recheck this group." + "\n";
end