Use HTTParty to make connection logic much simpler
This commit is contained in:
parent
5ff93061d9
commit
92926effa5
3 changed files with 43 additions and 25 deletions
3
Gemfile
3
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
|
||||
|
|
|
|||
|
|
@ -714,6 +714,7 @@ DEPENDENCIES
|
|||
guard-rspec
|
||||
haml-lint
|
||||
haml-rails
|
||||
httparty
|
||||
icalendar
|
||||
iso-639
|
||||
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
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue