DRY query_api in mailbluster_manager

This commit is contained in:
Ziyi 2021-04-01 21:55:16 -07:00 committed by CactusPuppy
parent dcec2a6e01
commit ea681c030c
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 #TODO
options = @@auth_headers.merge({
body: body.to_json
})
method(path, options).parsed_response
end end
def self.create_lead(user) def self.create_lead(user)
options = @@auth_headers.merge({ query_api(post, '/', {
body: { 'email' => user.email,
'email' => user.email, 'firstName' => user.name,
'firstName' => user.name, 'overrideExisting' => true,
'overrideExisting' => true, 'subscribed' => true,
'subscribed' => true, 'tags' => [ENV['OSEM_NAME'] || 'snapcon']
'tags' => [ENV['OSEM_NAME'] || 'snapcon']
}.to_json
}) })
post('/', options).parsed_response
end end
def self.edit_lead(user, add_tags: [], remove_tags: [], old_email: nil) def self.edit_lead(user, add_tags: [], remove_tags: [], old_email: nil)
options = @@auth_headers.merge({ email_hash = Digest::MD5.hexdigest(old_email.presence || user.email)
body: { query_api(put, "/#{email_hash}", {
'email' => user.email, 'email' => user.email,
'firstName' => user.name, 'firstName' => user.name,
'addTags' => add_tags, 'addTags' => add_tags,
'removeTags' => remove_tags 'removeTags' => remove_tags
}.to_json
}) })
email_hash = Digest::MD5.hexdigest(old_email.presence || user.email)
put("/#{email_hash}", options).parsed_response
end end
def self.delete_lead(user) def self.delete_lead(user)
email_hash = Digest::MD5.hexdigest user.email email_hash = Digest::MD5.hexdigest user.email
options = @@auth_headers query_api(delete, "/#{email_hash}", {})
delete("/#{email_hash}", options).parsed_response
end end
end end