Class: Cordial::Contacts

Inherits:
Object
  • Object
show all
Extended by:
Client
Includes:
HTTParty
Defined in:
lib/cordial/contacts.rb

Overview

Wraps all interaction with the Contact resource.

Class Method Summary collapse

Methods included from Client

client

Class Method Details

.create(email:, attribute_list: {}, subscribe_status: nil) ⇒ Object

Create a new contact.

If the contact already exists it will fail.

Examples:

Usage.

Cordial::Contacts.create(
  email: '[email protected]',
  attribute_list: {
    some_attribute: 'your-custom-value'
  },
  subscribe_status: 'subscribed'
)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cordial/contacts.rb', line 52

def self.create(email:, attribute_list: {}, subscribe_status: nil)
  client.post('/contacts', body: {
    channels: {
      email: {
        address: email,
        subscribeStatus: subscribe_status
      }.compact
    },
    forceSubscribe: subscribe_status == 'subscribed' || nil
  }.compact.merge(attribute_list).to_json)
end

.create_cart(email, options) ⇒ {"success"=>true}, {"error"=>true, "messages"=>"..."}

Create a new contact cart.

Examples:

Usage.

Cordial::Contacts.create_cart({...})

Returns:

  • ({"success"=>true})
  • ({"error"=>true, "messages"=>"..."})


130
131
132
133
# File 'lib/cordial/contacts.rb', line 130

def self.create_cart(email, options)
  cart = Cordial::Cart.new(options)
  client.post("/contacts/#{email}/cart", body: cart.to_json)
end

.find(email:) ⇒ Object

Find a contact.

Examples:

Usage

Cordial::Contacts.find(email: '[email protected]')

Response when the contact was found.

{
  "_id" => "111122223334444",
  "channels" => {
    "email" => {
      "address" => "[email protected]",
      "subscribeStatus" => "unsubscribed",
      "unsubscribedAt" => "2018-05-02T23:35:38+0000"
    }
  },
  "lastModified" => "2018-05-16T22:57:16+0000",
  "createdAt" => "2018-04-25T19:55:45+0000",
  "lists" => ["test-list"],
  "lj" => {
    "14854" => {
      "sec" => 1524897139,
      "usec" => 0
     }
   },
   "listJoinDate" => {
     "test-list" => "2018-04-28T06:32:19+0000"
   }
 }

Response when the contact was not found.

{"error"=>true, "message"=>"record not found"}


37
38
39
# File 'lib/cordial/contacts.rb', line 37

def self.find(email:)
  client.get("/contacts/#{email}")
end

.unsubscribe(email:, channel: '', mc_id: '') ⇒ Object

Unsubscribe a contact.

Examples:

Usage. Default without channel.

Cordial::Contacts.unsubscribe(
  email: '[email protected]'
)

Usage. with channel and mcID.

Cordial::Contacts.unsubscribe(
  email: '[email protected]',
  channel: 'email'
  mc_id: '645:5b6a9f26esb828b63c2a7946:ot:8ama709bbb3dc2f9bc27158f:1'
)

Parameters:

  • channel (String) (defaults to: '')

    The channel to unsubscribe`

  • mc_id (String) (defaults to: '')

    The Message Contact ID

See Also:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cordial/contacts.rb', line 104

def self.unsubscribe(email:, channel: '', mc_id: '')
  if channel.empty? && mc_id.empty?
    url = "/contacts/#{email}"
    body = {
      channels: {
        email: {
          address: email,
          subscribeStatus: 'unsubscribed'
        }
      }
    }
  else
    url = "/contacts/#{email}/unsubscribe/#{channel}"
    body = { mcID: mc_id }
  end

  client.put(url, body: body.to_json)
end

.update(email:, attribute_list: {}, subscribe_status: nil) ⇒ Object

Update an existing contact.

If the contact doesn’t exist it will fail.

Examples:

Usage.

Cordial::Contacts.update(
  email: '[email protected]',
  attribute_list: {
    some_attribute: 'your-custom-value'
  },
  subscribe_status: 'subscribed'
)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cordial/contacts.rb', line 75

def self.update(email:, attribute_list: {}, subscribe_status: nil)
  client.put("/contacts/email:#{email}", body: {
    channels: {
      email: {
        address: email,
        subscribeStatus: subscribe_status
      }.compact
    },
    forceSubscribe: subscribe_status == 'subscribed' || nil
  }.compact.merge(attribute_list).to_json)
end