Skip to content

Instantly share code, notes, and snippets.

@leoiamele
Last active October 26, 2020 13:33
Show Gist options
  • Save leoiamele/3ecaab3efee46a18463e1969442d625f to your computer and use it in GitHub Desktop.
Save leoiamele/3ecaab3efee46a18463e1969442d625f to your computer and use it in GitHub Desktop.
Integracion Perfit
#Servicio de Perfit
class PerfitService
def initialize
perfit = Webhook.perfit_email.first
@basic_end_point = "https://api.myperfit.com/v2"
@api_key = perfit.app_secret
@perfit_account = perfit.app_name
end
def create_suscriber(user)
end_point = "#{@basic_end_point}/#{@perfit_account}/contacts"
uri = URI.parse("#{end_point}")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = @api_key
request.content_type = "application/json"
data = JSON.dump({
firstName: "#{user.first_name}",
lastName: "#{user.last_name}",
email: "#{user.email}"
})
request.body = data
req_options = {
use_ssl: uri.scheme == "https",
read_timeout: 7,
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.read_timeout = 2
http.request(request)
end
return JSON.parse(response.body)
end
def update_suscriber(user)
end_point = "#{@basic_end_point}/#{@perfit_account}/contacts/#{user.perfit_id}"
uri = URI.parse("#{end_point}")
request = Net::HTTP::Put.new(uri)
request["Authorization"] = @api_key
request.content_type = "application/json"
data = JSON.dump({
firstName: "#{user.first_name}",
lastName: "#{user.last_name}"
})
request.body = data
req_options = {
use_ssl: uri.scheme == "https",
read_timeout: 7,
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.read_timeout = 2
http.request(request)
end
return JSON.parse(response.body)
end
def add_user_to_list(user, list_id)
end_point = "#{@basic_end_point}/#{@perfit_account}/contacts/#{user.perfit_id}/lists/#{list_id}"
uri = URI.parse("#{end_point}")
request = Net::HTTP::Put.new(uri)
request["Authorization"] = @api_key
request.content_type = "application/json"
req_options = {
use_ssl: uri.scheme == "https",
read_timeout: 7,
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.read_timeout = 2
http.request(request)
end
return JSON.parse(response.body)
end
def get_lists
end_point = "#{@basic_end_point}/#{@perfit_account}/lists"
uri = URI.parse("#{end_point}")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = @api_key
request.content_type = "application/json"
req_options = {
use_ssl: uri.scheme == "https",
read_timeout: 7,
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.read_timeout = 2
http.request(request)
end
return JSON.parse(response.body)
end
end
#Request de creacion de usuarios y listas - En el Modelo
def add_to_perfit
if Webhook.perfit_email.present?
suscriber = PerfitService.new.create_suscriber(self)
if suscriber["success"]
self.perfit_id = suscriber["data"]["id"]
self.save
list_id = NewsletterUser.find_list_id("general")
PerfitService.new.add_user_to_list(self, list_id)
if self.main_site_suscribed
list_id = NewsletterUser.find_list_id("principal")
PerfitService.new.add_user_to_list(self, list_id)
end
if self.outlet_suscribed
list_id = NewsletterUser.find_list_id("outlet")
PerfitService.new.add_user_to_list(self, list_id)
end
end
end
end
def self.find_list_id(name)
lists = PerfitService.new.get_lists
lists["data"].each do |list|
if list["name"] == name
return list["id"]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment