Class: Posthubify::ContactsResource

Inherits:
Object
  • Object
show all
Defined in:
lib/posthubify/resources/messaging.rb

Overview

Contact management — CRUD + channels + bulk import (Node sdk .contacts).

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ ContactsResource

Returns a new instance of ContactsResource.



110
111
112
# File 'lib/posthubify/resources/messaging.rb', line 110

def initialize(http)
  @http = http
end

Instance Method Details

#add_channel(id, input) ⇒ Object

Add a channel to a contact.



154
155
156
# File 'lib/posthubify/resources/messaging.rb', line 154

def add_channel(id, input)
  @http.data('POST', "/contacts/#{id}/channels", body: input)
end

#bulk(contacts) ⇒ Object

Bulk import (1-1000); channel-first upsert avoids piling up duplicates.



159
160
161
# File 'lib/posthubify/resources/messaging.rb', line 159

def bulk(contacts)
  @http.data('POST', '/contacts/bulk', body: { 'contacts' => contacts })
end

#channels(id) ⇒ Object

A contact’s channels.



149
150
151
# File 'lib/posthubify/resources/messaging.rb', line 149

def channels(id)
  @http.data('GET', "/contacts/#{id}/channels")
end

#create(input) ⇒ Object

Channel-first idempotent upsert: an existing contact is returned inside the envelope with ‘existing’ => true →unwrap the data envelope and merge the existing flag (mirrors Node async create).



128
129
130
131
132
133
134
135
136
# File 'lib/posthubify/resources/messaging.rb', line 128

def create(input)
  r = @http.req('POST', '/contacts', body: input)
  contact = r.is_a?(Hash) && r.key?('data') ? r['data'] : r
  if r.is_a?(Hash) && r['existing'] && contact.is_a?(Hash)
    contact.merge('existing' => true)
  else
    contact
  end
end

#delete(id) ⇒ Object

Delete a contact.



144
145
146
# File 'lib/posthubify/resources/messaging.rb', line 144

def delete(id)
  @http.data('DELETE', "/contacts/#{id}")
end

#get(id) ⇒ Object

A single contact.



122
123
124
# File 'lib/posthubify/resources/messaging.rb', line 122

def get(id)
  @http.data('GET', "/contacts/#{id}")
end

#list(search: nil, tag: nil, limit: nil, cursor: nil) ⇒ Object

Contact list (paginated envelope).



115
116
117
118
119
# File 'lib/posthubify/resources/messaging.rb', line 115

def list(search: nil, tag: nil, limit: nil, cursor: nil)
  @http.req('GET', '/contacts', query: {
    'search' => search, 'tag' => tag, 'limit' => limit, 'cursor' => cursor
  })
end

#update(id, input) ⇒ Object

Update a contact (partial).



139
140
141
# File 'lib/posthubify/resources/messaging.rb', line 139

def update(id, input)
  @http.data('PATCH', "/contacts/#{id}", body: input)
end