DRY query_api in mailbluster_manager

This commit is contained in:
Ziyi 2021-04-01 21:55:16 -07:00 committed by CactusPuppy
parent a553a53b51
commit 6b108043e1
No known key found for this signature in database
GPG key ID: 4B33B0A3E15E2C82

View file

@ -8,39 +8,36 @@ class MailblusterManager
}
}
def query_api(method, user, body)
def query_api(method, path, body)
#TODO
options = @@auth_headers.merge({
body: body.to_json
})
method(path, options).parsed_response
end
def self.create_lead(user)
options = @@auth_headers.merge({
body: {
'email' => user.email,
'firstName' => user.name,
'overrideExisting' => true,
'subscribed' => true,
'tags' => [ENV['OSEM_NAME'] || 'snapcon']
}.to_json
query_api(post, '/', {
'email' => user.email,
'firstName' => user.name,
'overrideExisting' => true,
'subscribed' => true,
'tags' => [ENV['OSEM_NAME'] || 'snapcon']
})
post('/', options).parsed_response
end
def self.edit_lead(user, add_tags: [], remove_tags: [], old_email: nil)
options = @@auth_headers.merge({
body: {
email_hash = Digest::MD5.hexdigest(old_email.presence || user.email)
query_api(put, "/#{email_hash}", {
'email' => user.email,
'firstName' => user.name,
'addTags' => add_tags,
'removeTags' => remove_tags
}.to_json
})
email_hash = Digest::MD5.hexdigest(old_email.presence || user.email)
put("/#{email_hash}", options).parsed_response
end
def self.delete_lead(user)
email_hash = Digest::MD5.hexdigest user.email
options = @@auth_headers
delete("/#{email_hash}", options).parsed_response
query_api(delete, "/#{email_hash}", {})
end
end