Class: Maropost::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/maropost/api.rb

Class Method Summary collapse

Class Method Details

.change_email(old_email, new_email) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/maropost/api.rb', line 56

def change_email(old_email, new_email)
  contact = find(old_email)

  if contact.present?
    contact = update(Maropost::Contact.new(id: contact.id, email: new_email))
  end

  contact
end

.create(contact) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/maropost/api.rb', line 22

def update_subscriptions(contact)
  update_do_not_mail_list(contact)
  to_maropost(
    find(contact.email),
    contact
  )
end