Module: SendGrid4r::REST::MarketingCampaigns::Contacts::Recipients

Includes:
Request
Included in:
API
Defined in:
lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb

Overview

SendGrid Web API v3 Contacts - Recipients

Defined Under Namespace

Classes: Count, Error, Recipient, Recipients, Result

Constant Summary

Constants included from Request

Request::BASE_URL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#create_args, #delete, #execute, #finish, #get, #patch, #post, #process_array_params, #process_url_params, #put

Class Method Details

.create_error(resp) ⇒ Object



142
143
144
145
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 142

def self.create_error(resp)
  return resp if resp.nil?
  Error.new(resp['error_indices'], resp['message'])
end

.create_recipient(resp) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 29

def self.create_recipient(resp)
  return resp if resp.nil?
  custom_fields = []
  custom_fields = resp['custom_fields'].map do |field|
    Contacts::CustomFields.create_field(field)
  end unless resp['custom_fields'].nil?
  Recipient.new(
    Time.at(resp['created_at']), custom_fields,
    resp['email'], resp['first_name'], resp['id'],
    resp['last_clicked'], resp['last_emailed'],
    resp['last_name'], resp['last_opened'],
    Time.at(resp['updated_at'])
  )
end

.create_recipient_count(resp) ⇒ Object



52
53
54
55
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 52

def self.create_recipient_count(resp)
  return resp if resp.nil?
  Count.new(resp['recipient_count'])
end

.create_recipients(resp) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 44

def self.create_recipients(resp)
  return resp if resp.nil?
  recipients = resp['recipients'].map do |recipient|
    Contacts::Recipients.create_recipient(recipient)
  end
  Recipients.new(recipients)
end

.create_result(resp) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 129

def self.create_result(resp)
  return resp if resp.nil?
  errors = resp['errors'].map do |error|
    Contacts::Recipients.create_error(error)
  end unless resp['errors'].nil?
  Result.new(
    resp['error_count'], resp['error_indices'], resp['new_count'],
    resp['persisted_recipients'], resp['updated_count'], errors
  )
end

.url(recipient_id = nil) ⇒ Object



57
58
59
60
61
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 57

def self.url(recipient_id = nil)
  url = "#{BASE_URL}/contactdb/recipients"
  url = "#{url}/#{recipient_id}" unless recipient_id.nil?
  url
end

Instance Method Details

#delete_recipient(recipient_id:, &block) ⇒ Object



94
95
96
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 94

def delete_recipient(recipient_id:, &block)
  delete(@auth, Contacts::Recipients.url(recipient_id), &block)
end

#delete_recipients(recipient_ids:, &block) ⇒ Object



73
74
75
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 73

def delete_recipients(recipient_ids:, &block)
  delete(@auth, Contacts::Recipients.url, nil, recipient_ids, &block)
end

#get_lists_recipient_belong(recipient_id:, &block) ⇒ Object



98
99
100
101
102
103
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 98

def get_lists_recipient_belong(recipient_id:, &block)
  resp = get(
    @auth, "#{Contacts::Recipients.url(recipient_id)}/lists", &block
  )
  finish(resp, @raw_resp) { |r| Contacts::Lists.create_lists(r) }
end

#get_recipient(recipient_id:, &block) ⇒ Object



87
88
89
90
91
92
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 87

def get_recipient(recipient_id:, &block)
  resp = get(@auth, Contacts::Recipients.url(recipient_id), &block)
  finish(resp, @raw_resp) do |r|
    Contacts::Recipients.create_recipient(r)
  end
end

#get_recipients(page: nil, page_size: nil, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 77

def get_recipients(page: nil, page_size: nil, &block)
  params = {}
  params['page_size'] = page_size unless page_size.nil?
  params['page'] = page unless page.nil?
  resp = get(@auth, Contacts::Recipients.url, params, &block)
  finish(resp, @raw_resp) do |r|
    Contacts::Recipients.create_recipients(r)
  end
end

#get_recipients_count(&block) ⇒ Object



105
106
107
108
109
110
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 105

def get_recipients_count(&block)
  resp = get(@auth, "#{Contacts::Recipients.url}/count", &block)
  finish(resp, @raw_resp) do |r|
    Contacts::Recipients.create_recipient_count(r).recipient_count
  end
end

#patch_recipients(params:, &block) ⇒ Object



68
69
70
71
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 68

def patch_recipients(params:, &block)
  resp = patch(@auth, Contacts::Recipients.url, params, &block)
  finish(resp, @raw_resp) { |r| Contacts::Recipients.create_result(r) }
end

#post_recipients(params:, &block) ⇒ Object



63
64
65
66
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 63

def post_recipients(params:, &block)
  resp = post(@auth, Contacts::Recipients.url, params, &block)
  finish(resp, @raw_resp) { |r| Contacts::Recipients.create_result(r) }
end

#search_recipients(params:, &block) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/sendgrid4r/rest/marketing_campaigns/contacts/recipients.rb', line 112

def search_recipients(params:, &block)
  endpoint = "#{Contacts::Recipients.url}/search"
  resp = get(@auth, endpoint, params, &block)
  finish(resp, @raw_resp) do |r|
    Contacts::Recipients.create_recipients(r)
  end
end