delete_lead WIP
This commit is contained in:
parent
df9187a344
commit
d4f823e7f4
3 changed files with 26 additions and 12 deletions
23
app/helpers/external/mailbluster_helper.rb
vendored
23
app/helpers/external/mailbluster_helper.rb
vendored
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
module External
|
||||
module MailblusterHelper
|
||||
MAILBLUSTER_URL = 'https://api.mailbluster.com/api/leads'
|
||||
MAILBLUSTER_URL = 'https://api.mailbluster.com/api/leads/'
|
||||
|
||||
def query_api; end
|
||||
# def query_api(user, method)
|
||||
# TODO? General helper for all queries
|
||||
# end
|
||||
|
||||
def create_lead(user)
|
||||
# TODO
|
||||
|
||||
uri = URI(MAILBLUSTER_URL)
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
request = Net::HTTP::Post.new(uri.path,
|
||||
'Authorization' => ENV['MAILBLUSTER_API_KEY']) # TODO: Authorization=APIKEY
|
||||
'Authorization' => ENV['MAILBLUSTER_API_KEY'])
|
||||
request.body = {
|
||||
'email' => user.email,
|
||||
'firstName' => user.name,
|
||||
|
|
@ -21,8 +21,6 @@ module External
|
|||
'tags' => ['snapcon']
|
||||
}.to_json
|
||||
response = http.request(request)
|
||||
# puts request.body.to_json
|
||||
# puts response.body.to_json
|
||||
response.to_json
|
||||
rescue StandardError => e
|
||||
puts "ERROR #{e}"
|
||||
|
|
@ -30,7 +28,16 @@ module External
|
|||
end
|
||||
|
||||
def delete_lead(user)
|
||||
# TODO
|
||||
email_hash = Digest::MD5.hexdigest user.email
|
||||
uri = URI(MAILBLUSTER_URL + email_hash)
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
request = Net::HTTP::Delete.new(uri.path,
|
||||
'Authorization' => ENV['MAILBLUSTER_API_KEY'])
|
||||
response = http.request(request)
|
||||
response.to_json
|
||||
rescue StandardError => e
|
||||
puts "ERROR #{e}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -361,6 +361,8 @@ class User < ApplicationRecord
|
|||
User.count == 1 && User.first.email == 'deleted@localhost.osem'
|
||||
end
|
||||
|
||||
# TODO email_hash function for mailbluster
|
||||
|
||||
private
|
||||
|
||||
def setup_role
|
||||
|
|
|
|||
13
spec/helpers/external/mailbluster_helper_spec.rb
vendored
13
spec/helpers/external/mailbluster_helper_spec.rb
vendored
|
|
@ -5,6 +5,7 @@ require 'webmock/rspec'
|
|||
|
||||
describe External::MailblusterHelper, type: :helper do
|
||||
let!(:user) { create(:user) }
|
||||
url = 'https://api.mailbluster.com/api/leads/'
|
||||
|
||||
describe 'create_lead' do
|
||||
it 'makes a post request to Mailbluster\'s API and gets the correct response' do
|
||||
|
|
@ -22,7 +23,7 @@ describe External::MailblusterHelper, type: :helper do
|
|||
],
|
||||
}
|
||||
}"
|
||||
stub_request(:post, 'https://api.mailbluster.com/api/leads')
|
||||
stub_request(:post, url)
|
||||
.to_return(body: response_body, status: 200)
|
||||
response = create_lead(user)
|
||||
|
||||
|
|
@ -40,12 +41,16 @@ describe External::MailblusterHelper, type: :helper do
|
|||
describe 'delete_lead' do
|
||||
it 'correctly requests the right URL and gets a valid response' do
|
||||
email_hash = Digest::MD5.hexdigest user.email
|
||||
response_body = "{\"message\":\"Lead deleted\",\"leadHash\":\"#{email_hash}\"}"
|
||||
stub_request(:delete, "https://api.mailbluster.com/api/leads/#{email_hash}")
|
||||
response_body = "{
|
||||
\"message\":\"Lead deleted\",
|
||||
\"leadHash\":\"#{email_hash}\"
|
||||
}"
|
||||
lead_url = url + email_hash.to_s
|
||||
stub_request(:delete, lead_url)
|
||||
.to_return(body: response_body)
|
||||
response = delete_lead(user)
|
||||
|
||||
expect(WebMock).to have_requested(:delete, "api.mailbluster.com/api/leads/#{email_hash}")
|
||||
expect(WebMock).to have_requested(:delete, lead_url)
|
||||
expect(response).to eq(response_body)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue