Use HTTParty to make connection logic much simpler
This commit is contained in:
parent
a4eb07a4a8
commit
e99d9ac054
3 changed files with 43 additions and 25 deletions
3
Gemfile
3
Gemfile
|
|
@ -221,6 +221,9 @@ gem 'dalli'
|
||||||
|
|
||||||
gem 'icalendar'
|
gem 'icalendar'
|
||||||
|
|
||||||
|
# for making external requests easier
|
||||||
|
gem 'httparty'
|
||||||
|
|
||||||
# Use guard and spring for testing in development
|
# Use guard and spring for testing in development
|
||||||
group :development do
|
group :development do
|
||||||
# to launch specs when files are modified
|
# to launch specs when files are modified
|
||||||
|
|
|
||||||
|
|
@ -714,6 +714,7 @@ DEPENDENCIES
|
||||||
guard-rspec
|
guard-rspec
|
||||||
haml-lint
|
haml-lint
|
||||||
haml-rails
|
haml-rails
|
||||||
|
httparty
|
||||||
icalendar
|
icalendar
|
||||||
iso-639
|
iso-639
|
||||||
jquery-datatables
|
jquery-datatables
|
||||||
|
|
|
||||||
64
app/helpers/external/mailbluster_helper.rb
vendored
64
app/helpers/external/mailbluster_helper.rb
vendored
|
|
@ -1,4 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
require 'httparty'
|
||||||
|
|
||||||
module External
|
module External
|
||||||
module MailblusterHelper
|
module MailblusterHelper
|
||||||
|
|
@ -9,40 +10,53 @@ module External
|
||||||
# end
|
# end
|
||||||
|
|
||||||
def create_lead(user)
|
def create_lead(user)
|
||||||
uri = URI(MAILBLUSTER_URL)
|
options = {
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
headers: {
|
||||||
http.use_ssl = true
|
'Content-Type' => 'application/json',
|
||||||
request = Net::HTTP::Post.new(uri.path,
|
'Authorization' => ENV['MAILBLUSTER_API_KEY']
|
||||||
'Authorization' => ENV['MAILBLUSTER_API_KEY'])
|
},
|
||||||
request['Content-Type'] = 'application/json'
|
body: {
|
||||||
request.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
|
||||||
}.to_json
|
}
|
||||||
response = http.request(request)
|
HTTParty.post(MAILBLUSTER_URL, options).parsed_response
|
||||||
response.body
|
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
puts "ERROR #{e}"
|
puts "ERROR #{e}"
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit_lead(user, add_tags, remove_tags)
|
def edit_lead(user, add_tags: [], remove_tags: [], old_email: nil)
|
||||||
puts 'PENDING'
|
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
|
end
|
||||||
|
|
||||||
def delete_lead(user)
|
def delete_lead(user)
|
||||||
email_hash = Digest::MD5.hexdigest user.email
|
email_hash = Digest::MD5.hexdigest user.email
|
||||||
uri = URI(MAILBLUSTER_URL + email_hash)
|
options = {
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
headers: {
|
||||||
http.use_ssl = true
|
'Content-Type' => 'application/json',
|
||||||
request = Net::HTTP::Delete.new(uri.path,
|
'Authorization' => ENV['MAILBLUSTER_API_KEY']
|
||||||
'Authorization' => ENV['MAILBLUSTER_API_KEY'])
|
}
|
||||||
request['Content-Type'] = 'application/json'
|
}
|
||||||
response = http.request(request)
|
HTTParty.delete(MAILBLUSTER_URL + email_hash, options).parsed_response
|
||||||
response.body
|
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
puts "ERROR #{e}"
|
puts "ERROR #{e}"
|
||||||
nil
|
nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue