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 External
|
||||||
module MailblusterHelper
|
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)
|
def create_lead(user)
|
||||||
# TODO
|
|
||||||
|
|
||||||
uri = URI(MAILBLUSTER_URL)
|
uri = URI(MAILBLUSTER_URL)
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
request = Net::HTTP::Post.new(uri.path,
|
request = Net::HTTP::Post.new(uri.path,
|
||||||
'Authorization' => ENV['MAILBLUSTER_API_KEY']) # TODO: Authorization=APIKEY
|
'Authorization' => ENV['MAILBLUSTER_API_KEY'])
|
||||||
request.body = {
|
request.body = {
|
||||||
'email' => user.email,
|
'email' => user.email,
|
||||||
'firstName' => user.name,
|
'firstName' => user.name,
|
||||||
|
|
@ -21,8 +21,6 @@ module External
|
||||||
'tags' => ['snapcon']
|
'tags' => ['snapcon']
|
||||||
}.to_json
|
}.to_json
|
||||||
response = http.request(request)
|
response = http.request(request)
|
||||||
# puts request.body.to_json
|
|
||||||
# puts response.body.to_json
|
|
||||||
response.to_json
|
response.to_json
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
puts "ERROR #{e}"
|
puts "ERROR #{e}"
|
||||||
|
|
@ -30,7 +28,16 @@ module External
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_lead(user)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -361,6 +361,8 @@ class User < ApplicationRecord
|
||||||
User.count == 1 && User.first.email == 'deleted@localhost.osem'
|
User.count == 1 && User.first.email == 'deleted@localhost.osem'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO email_hash function for mailbluster
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def setup_role
|
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
|
describe External::MailblusterHelper, type: :helper do
|
||||||
let!(:user) { create(:user) }
|
let!(:user) { create(:user) }
|
||||||
|
url = 'https://api.mailbluster.com/api/leads/'
|
||||||
|
|
||||||
describe 'create_lead' do
|
describe 'create_lead' do
|
||||||
it 'makes a post request to Mailbluster\'s API and gets the correct response' 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)
|
.to_return(body: response_body, status: 200)
|
||||||
response = create_lead(user)
|
response = create_lead(user)
|
||||||
|
|
||||||
|
|
@ -40,12 +41,16 @@ describe External::MailblusterHelper, type: :helper do
|
||||||
describe 'delete_lead' do
|
describe 'delete_lead' do
|
||||||
it 'correctly requests the right URL and gets a valid response' do
|
it 'correctly requests the right URL and gets a valid response' do
|
||||||
email_hash = Digest::MD5.hexdigest user.email
|
email_hash = Digest::MD5.hexdigest user.email
|
||||||
response_body = "{\"message\":\"Lead deleted\",\"leadHash\":\"#{email_hash}\"}"
|
response_body = "{
|
||||||
stub_request(:delete, "https://api.mailbluster.com/api/leads/#{email_hash}")
|
\"message\":\"Lead deleted\",
|
||||||
|
\"leadHash\":\"#{email_hash}\"
|
||||||
|
}"
|
||||||
|
lead_url = url + email_hash.to_s
|
||||||
|
stub_request(:delete, lead_url)
|
||||||
.to_return(body: response_body)
|
.to_return(body: response_body)
|
||||||
response = delete_lead(user)
|
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)
|
expect(response).to eq(response_body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue