osem-fcy/app/services/mailbluster_manager.rb

41 lines
1.2 KiB
Ruby
Raw Normal View History

class MailblusterManager
include HTTParty
base_uri 'https://api.mailbluster.com/api/leads/'
2021-04-01 22:08:25 -07:00
@auth_headers = {
2021-04-01 21:47:28 -07:00
headers: {
'Content-Type' => 'application/json',
'Authorization' => ENV['MAILBLUSTER_API_KEY']
}
}
def self.query_api(method, path, body: {})
2021-04-01 22:08:25 -07:00
options = @auth_headers.merge(body: body.to_json)
2021-04-01 22:01:09 -07:00
send(method, path, options).parsed_response
2021-04-01 21:47:28 -07:00
end
def self.create_lead(user)
query_api(:post, '/', body: {
'email' => user.email,
'firstName' => user.name,
'overrideExisting' => true,
'subscribed' => true,
'tags' => [ENV['OSEM_NAME'] || 'snapcon']
})
end
def self.edit_lead(user, add_tags: [], remove_tags: [], old_email: nil)
2021-04-01 21:55:16 -07:00
email_hash = Digest::MD5.hexdigest(old_email.presence || user.email)
query_api(:put, "/#{email_hash}", body: {
'email' => user.email,
'firstName' => user.name,
'addTags' => add_tags,
'removeTags' => remove_tags
})
end
def self.delete_lead(user)
email_hash = Digest::MD5.hexdigest user.email
query_api(:delete, "/#{email_hash}")
end
2021-03-31 21:22:28 -07:00
end