class Maropost::Api

Public Class Methods

change_email(old_email, new_email) click to toggle source
# File lib/maropost/api.rb, line 56
def change_email(old_email, new_email)
  old_email_contact = find(old_email)
  new_email_contact = find(new_email)

  contact = update_email(old_email_contact, new_email_contact, new_email)

  update_do_not_mail_list(contact) if contact

  contact
end
create(contact) click to toggle source
# File lib/maropost/api.rb, line 30
def create(contact)
  service = Service.new(
    method: :post,
    path: 'contacts.json',
    payload: create_or_update_payload(contact)
  )
  response = JSON.parse(service.execute!.body)
  Maropost::Contact.new(response)
rescue RestClient::UnprocessableEntity, RestClient::BadRequest
  contact.errors << 'Unable to create or update contact'
  contact
end
find(email) click to toggle source
# File lib/maropost/api.rb, line 6
def find(email)
  raise ArgumentError, 'must provide email' if email.blank?
  service = Service.new(
    method: :get,
    path: 'contacts/email.json',
    query: {
      'contact[email]': email,
      'auth_token': Maropost.configuration.auth_token
    }
  )
  response = JSON.parse(service.execute!.body)
  Maropost::Contact.new(response)
rescue RestClient::ResourceNotFound
  nil
end
update(contact) click to toggle source
# File lib/maropost/api.rb, line 43
def update(contact)
  service = Service.new(
    method: :put,
    path: "contacts/#{contact.id}.json",
    payload: create_or_update_payload(contact)
  )
  response = JSON.parse(service.execute!.body)
  Maropost::Contact.new(response)
rescue RestClient::UnprocessableEntity, RestClient::BadRequest
  contact.errors << 'Unable to update contact'
  contact
end
update_subscriptions(contact) click to toggle source
# File lib/maropost/api.rb, line 22
def update_subscriptions(contact)
  update_do_not_mail_list(contact)
  to_maropost(
    find(contact.email),
    contact
  )
end

Private Class Methods

create_or_update_payload(contact) click to toggle source
# File lib/maropost/api.rb, line 86
def create_or_update_payload(contact)
  payload(contact).tap do |p|
    p[:contact][:first_name] = contact.try(:first_name) if contact.try(:first_name)
    p[:contact][:last_name] = contact.try(:last_name) if contact.try(:last_name)
  end
end
payload(contact) click to toggle source
# File lib/maropost/api.rb, line 93
def payload(contact)
  {
    contact: {
      email: contact.email,
      phone: contact.phone_number,
      custom_field: contact.list_parameters
    }
  }
end
to_maropost(existing, contact) click to toggle source
# File lib/maropost/api.rb, line 103
def to_maropost(existing, contact)
  if existing
    contact.id = existing.id
    update(contact)
  else
    create(contact)
  end
end
update_do_not_mail_list(contact) click to toggle source
# File lib/maropost/api.rb, line 112
def update_do_not_mail_list(contact)
  if contact.allow_emails?
    DoNotMailList.delete(contact)
  else
    DoNotMailList.create(contact)
  end
end
update_email(old_contact, new_contact, new_email) click to toggle source
# File lib/maropost/api.rb, line 69
def update_email(old_contact, new_contact, new_email)
  contact = nil

  if old_contact.present? && new_contact.present?
    new_contact = new_contact.merge_settings(old_contact)
    contact = update(new_contact)
    Maropost::DoNotMailList.create(old_contact)
  elsif old_contact.present?
    old_contact.email = new_email
    contact = update(old_contact)
  elsif new_contact.present?
    contact = update(new_contact)
  end

  contact
end