Created
June 12, 2016 22:47
-
-
Save dcuadraq/519b88b60c047fa5832824fc5b65e6dd to your computer and use it in GitHub Desktop.
Wrapper for Sendgrid API v3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sendgrid | |
class << self | |
@@LISTS = { | |
newsletter: ENV.fetch('SENDGRID_LIST_NEWSLETTER_ID'), | |
}.with_indifferent_access | |
def add_mail_to_list(list_name, email, args={}) | |
Rails.logger.info("SubscriptionWorker: Adding '#{email}' to sengrid list '#{list_name}'") | |
contact_name = args[:contact_name] || '' | |
subscribe_to_sendgrid(list_name, email, contact_name) | |
end | |
def list_lists | |
url = "https://api.sendgrid.com/v3/contactdb/lists" | |
response = http_request(url, :get) | |
response['lists'] | |
end | |
def count_recipients | |
url = "https://api.sendgrid.com/v3/contactdb/recipients/count" | |
response = http_request(url, :get) | |
response['recipient_count'] | |
end | |
def list_recipients(list_id, page=1, page_size=100) | |
url = "https://api.sendgrid.com/v3/contactdb/lists/#{list_id}/recipients?page_size=#{page_size}&page=#{page}" | |
response = http_request(url, :get) | |
response['recipients'].map {|r| r['email']} | |
end | |
def search_recipient(email) | |
url = "https://api.sendgrid.com/v3/contactdb/recipients/search?email=#{email}" | |
response = http_request(url, :get) | |
return response["recipients"].first if response["recipients"].any? | |
nil | |
end | |
def find_recipient(recipient_id) | |
url = "https://api.sendgrid.com/v3/contactdb/recipients/#{recipient_id}" | |
http_request(url, :get) | |
end | |
def delete_recipient_from_list(recipient_id, list_id) | |
url ="https://api.sendgrid.com/v3/contactdb/lists/#{list_id}/recipients/#{recipient_id}" | |
http_request(url, :delete) | |
end | |
def empty_list(list_id) | |
recipients_emails = list_recipients(list_id, 1, 1000) | |
contacts_id = recipients_emails.map do |email| | |
search_recipient(email)['id'] | |
end | |
contacts_id.each { |recipient_id| delete_recipient_from_list(recipient_id, list_id) } | |
end | |
def delete_recipient(recipient_id) | |
delete_recipients([recipient_id]) | |
end | |
def delete_recipients(recipients_ids) | |
url = 'https://api.sendgrid.com/v3/contactdb/recipients' | |
puts recipients_ids | |
data = recipients_ids.to_json | |
http_request(url, :delete, data) | |
end | |
# Possible types: text, number, date | |
def create_custom_field(name, type='text') | |
url ='https://api.sendgrid.com/v3/contactdb/custom_fields' | |
data = { | |
name: name, | |
type: type | |
}.to_json | |
http_request(url, :post, data) | |
end | |
def list_custom_fields | |
url ='https://api.sendgrid.com/v3/contactdb/custom_fields' | |
http_request(url, :get) | |
end | |
private | |
def subscribe_to_sendgrid(list_name, email, contact_name) | |
if list_in_api_v3?(list_name) | |
subscribe_sendgrid_api_v3(list_name, email, contact_name) | |
else | |
subscribe_sendgrid_api_v2(list_name.to_s, email, contact_name) | |
end | |
end | |
def list_in_api_v3?(list_name) | |
!!@@LISTS[list_name] | |
end | |
def subscribe_sendgrid_api_v2(list_name, email, contact_name) | |
begin | |
client.emails.add(list_name, [contact(email, contact_name)]).any? | |
rescue Sendgrid::API::REST::Errors::Unauthorized => e | |
Rails.logger.info("SubscriptionWorker: Failed to add '#{email}' to sengrid's '#{list_name}', #{e.message}") | |
false | |
end | |
end | |
def subscribe_sendgrid_api_v3(list_name, email, contact_name) | |
if (recipient = get_recipient(email, contact_name)) | |
subscribe_recipient_to_list(recipient['id'], list_name) | |
true | |
else | |
Rails.logger.info("SubscriptionWorker: Failed to add '#{email}' to sengrid's '#{list_name}'") | |
false | |
end | |
end | |
def get_recipient(email, contact_name) | |
search_recipient(email) || add_recipient(email, contact_name) | |
end | |
def add_recipient(email, contact_name) | |
data = [ | |
{ | |
email: email, | |
last_name: contact_name | |
} | |
].to_json | |
url = 'https://api.sendgrid.com/v3/contactdb/recipients' | |
response = http_request(url, :post, data) | |
if response && response.success? | |
response['persisted_recipients'].first | |
else | |
nil | |
end | |
end | |
def subscribe_recipient_to_list(recipient_id, list_name) | |
list_id = @@LISTS[list_name] | |
url = "https://api.sendgrid.com/v3/contactdb/lists/#{list_id}/recipients/#{recipient_id}" | |
response = http_request(url, :post) | |
binding.pry | |
return (response.code == 201) | |
end | |
def http_request(url, request_type, body=nil) | |
basic_auth = { | |
username: ENV['SENDGRID_USERNAME'], | |
password: ENV['SENDGRID_PASSWORD'] | |
} | |
headers = { "Content-Type" => "application/json" } | |
options = { | |
basic_auth: basic_auth, | |
headers: headers | |
} | |
options[:body] = body if body | |
case request_type | |
when :post | |
HTTParty.post(url, options) | |
when :get | |
HTTParty.get(url, options) | |
when :delete | |
HTTParty.delete(url, options) | |
end | |
end | |
def client | |
@client ||= Sendgrid::API::Client.new( | |
ENV.fetch('SENDGRID_USERNAME'), | |
ENV.fetch('SENDGRID_PASSWORD') | |
) | |
end | |
def contact(email, contact_name) | |
contact_name ||= User.find_by_email(email).try(:full_name) || '' | |
Sendgrid::API::Entities::Email.new( | |
email: email, | |
name: contact_name | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment