Avoid repeating headers

This commit is contained in:
CactusPuppy 2021-04-01 21:47:28 -07:00
parent 7a5777c4f7
commit a553a53b51
No known key found for this signature in database
GPG key ID: 4B33B0A3E15E2C82

View file

@ -1,13 +1,19 @@
class MailblusterManager
include HTTParty
base_uri 'https://api.mailbluster.com/api/leads/'
@@auth_headers = {
headers: {
'Content-Type' => 'application/json',
'Authorization' => ENV['MAILBLUSTER_API_KEY']
}
}
def query_api(method, user, body)
#TODO
end
def self.create_lead(user)
options = {
headers: {
'Content-Type' => 'application/json',
'Authorization' => ENV['MAILBLUSTER_API_KEY']
},
options = @@auth_headers.merge({
body: {
'email' => user.email,
'firstName' => user.name,
@ -15,35 +21,26 @@ class MailblusterManager
'subscribed' => true,
'tags' => [ENV['OSEM_NAME'] || 'snapcon']
}.to_json
}
})
post('/', options).parsed_response
end
def self.edit_lead(user, add_tags: [], remove_tags: [], old_email: nil)
options = {
headers: {
'Content-Type' => 'application/json',
'Authorization' => ENV['MAILBLUSTER_API_KEY']
},
options = @@auth_headers.merge({
body: {
'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 = {
headers: {
'Content-Type' => 'application/json',
'Authorization' => ENV['MAILBLUSTER_API_KEY']
}
}
options = @@auth_headers
delete("/#{email_hash}", options).parsed_response
end
end