diff --git a/Gemfile b/Gemfile index 05c7a4bd..4391debb 100644 --- a/Gemfile +++ b/Gemfile @@ -221,6 +221,9 @@ gem 'dalli' gem 'icalendar' +# for making external requests easier +gem 'httparty' + # Use guard and spring for testing in development group :development do # to launch specs when files are modified diff --git a/Gemfile.lock b/Gemfile.lock index 5b7365da..8d32a279 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -714,6 +714,7 @@ DEPENDENCIES guard-rspec haml-lint haml-rails + httparty icalendar iso-639 jquery-datatables diff --git a/app/helpers/external/mailbluster_helper.rb b/app/helpers/external/mailbluster_helper.rb index bc185d22..8a8d770c 100644 --- a/app/helpers/external/mailbluster_helper.rb +++ b/app/helpers/external/mailbluster_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'httparty' module External module MailblusterHelper @@ -9,40 +10,53 @@ module External # end def create_lead(user) - uri = URI(MAILBLUSTER_URL) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - request = Net::HTTP::Post.new(uri.path, - 'Authorization' => ENV['MAILBLUSTER_API_KEY']) - request['Content-Type'] = 'application/json' - request.body = { - 'email' => user.email, - 'firstName' => user.name, - 'overrideExisting' => true, - 'subscribed' => true, - 'tags' => [ENV['OSEM_NAME'] || 'snapcon'] - }.to_json - response = http.request(request) - response.body + options = { + headers: { + 'Content-Type' => 'application/json', + 'Authorization' => ENV['MAILBLUSTER_API_KEY'] + }, + body: { + 'email' => user.email, + 'firstName' => user.name, + 'overrideExisting' => true, + 'subscribed' => true, + 'tags' => [ENV['OSEM_NAME'] || 'snapcon'] + }.to_json + } + HTTParty.post(MAILBLUSTER_URL, options).parsed_response rescue StandardError => e puts "ERROR #{e}" nil end - def edit_lead(user, add_tags, remove_tags) - puts 'PENDING' + def edit_lead(user, add_tags: [], remove_tags: [], old_email: nil) + options = { + headers: { + 'Content-Type' => 'application/json', + 'Authorization' => ENV['MAILBLUSTER_API_KEY'] + }, + body: { + 'email' => user.email, + 'firstName' => user.name, + 'overrideExisting' => true, + 'subscribed' => true, + 'addTags' => add_tags, + 'removeTags' => remove_tags + }.to_json + } + email_hash = Digest::MD5.hexdigest(old_email.presence || user.email) + HTTParty.put(MAILBLUSTER_URL + email_hash, options).parsed_response end def delete_lead(user) email_hash = Digest::MD5.hexdigest user.email - uri = URI(MAILBLUSTER_URL + email_hash) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - request = Net::HTTP::Delete.new(uri.path, - 'Authorization' => ENV['MAILBLUSTER_API_KEY']) - request['Content-Type'] = 'application/json' - response = http.request(request) - response.body + options = { + headers: { + 'Content-Type' => 'application/json', + 'Authorization' => ENV['MAILBLUSTER_API_KEY'] + } + } + HTTParty.delete(MAILBLUSTER_URL + email_hash, options).parsed_response rescue StandardError => e puts "ERROR #{e}" nil