Class: SendWithUs::Api

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

—————————— Instance Methods ——————————



22
23
24
25
# File 'lib/send_with_us/api.rb', line 22

def initialize(options = {})
  settings = SendWithUs::Api.configuration.settings.merge(options)
  @configuration = SendWithUs::Config.new(settings)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/send_with_us/api.rb', line 8

def configuration
  @configuration
end

Class Method Details

.configurationObject

—————————— Class Methods ——————————



12
13
14
# File 'lib/send_with_us/api.rb', line 12

def self.configuration
  @configuration ||= SendWithUs::Config.new
end

.configure {|self.configuration| ... } ⇒ Object

Yields:



16
17
18
# File 'lib/send_with_us/api.rb', line 16

def self.configure
  yield self.configuration if block_given?
end

Instance Method Details

#create_template(name, subject, html, text, preheader = '', amp_html = '') ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/send_with_us/api.rb', line 163

def create_template(name, subject, html, text, preheader='', amp_html='')
  payload = {
    name: name,
    subject: subject,
    html: html,
    text: text,
    preheader: preheader,
    amp_html: amp_html
  }

  payload = payload.to_json
  SendWithUs::ApiRequest.new(@configuration).post(:emails, payload)
end

#create_template_version(template_id, name, subject, html, text, preheader = '', amp_html = '') ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/send_with_us/api.rb', line 289

def create_template_version(template_id, name, subject, html, text, preheader='', amp_html='')
  payload = {
    name: name,
    subject: subject,
    html: html,
    text: text,
    preheader: preheader,
    amp_html: amp_html
  }

  endpoint = "templates/#{template_id}/versions"
  SendWithUs::ApiRequest.new(@configuration).post(endpoint, payload.to_json)
end

#customer_create(email, data = {}, locale = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/send_with_us/api.rb', line 216

def customer_create(email, data = {}, locale = nil)
  payload = {email: email}

  payload[:data] = data if data && data.any?
  payload[:locale] = locale if locale

  payload = payload.to_json
  endpoint = "customers"
  SendWithUs::ApiRequest.new(@configuration).post(endpoint, payload)
end

#customer_delete(email_address) ⇒ Object



227
228
229
230
# File 'lib/send_with_us/api.rb', line 227

def customer_delete(email_address)
  endpoint = "customers/#{email_address}"
  SendWithUs::ApiRequest.new(@configuration).delete(endpoint)
end

#customer_email_log(email_address, options = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/send_with_us/api.rb', line 232

def customer_email_log(email_address, options = {})
  endpoint = "customers/#{email_address}/logs"
  params   = {}

  params[:count]       = options[:count]       unless options[:count].nil?
  params[:created_gt]  = options[:created_gt]  unless options[:created_gt].nil?
  params[:created_lt]  = options[:created_lt]  unless options[:created_lt].nil?

  unless params.empty?
    params   = URI.encode_www_form(params)
    endpoint = endpoint + '?' + params
  end

  SendWithUs::ApiRequest.new(@configuration).get(endpoint)
end

#customer_get(email) ⇒ Object



212
213
214
# File 'lib/send_with_us/api.rb', line 212

def customer_get(email)
  SendWithUs::ApiRequest.new(@configuration).get("customers/#{email}")
end

#delete_template(template_id) ⇒ Object



260
261
262
263
# File 'lib/send_with_us/api.rb', line 260

def delete_template(template_id)
  endpoint = "templates/#{template_id}"
  SendWithUs::ApiRequest.new(@configuration).delete(endpoint)
end

#drip_campaign_details(drip_campaign_id) ⇒ Object



208
209
210
# File 'lib/send_with_us/api.rb', line 208

def drip_campaign_details(drip_campaign_id)
  SendWithUs::ApiRequest.new(@configuration).get("drip_campaigns/#{drip_campaign_id}")
end

#drips_unsubscribe(email_address) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/send_with_us/api.rb', line 130

def drips_unsubscribe(email_address)
  if email_address.nil?
    raise SendWithUs::ApiNilEmailId, 'email_address cannot be nil'
  end

  payload = {email_address: email_address}

  payload = payload.to_json
  SendWithUs::ApiRequest.new(@configuration).post(:'drips/unsubscribe', payload)
end

#emailsObject Also known as: list_templates



141
142
143
# File 'lib/send_with_us/api.rb', line 141

def emails()
  SendWithUs::ApiRequest.new(@configuration).get(:emails)
end

#get_template_version(template_id, version_id) ⇒ Object



270
271
272
273
# File 'lib/send_with_us/api.rb', line 270

def get_template_version(template_id, version_id)
  endpoint = "templates/#{template_id}/versions/#{version_id}"
  SendWithUs::ApiRequest.new(@configuration).get(endpoint)
end

#list_drip_campaignsObject



177
178
179
# File 'lib/send_with_us/api.rb', line 177

def list_drip_campaigns()
  SendWithUs::ApiRequest.new(@configuration).get(:drip_campaigns)
end

#list_template_versions(template_id) ⇒ Object



265
266
267
268
# File 'lib/send_with_us/api.rb', line 265

def list_template_versions(template_id)
  endpoint = "templates/#{template_id}/versions"
  SendWithUs::ApiRequest.new(@configuration).get(endpoint)
end

#log(log_id) ⇒ Object



248
249
250
251
252
# File 'lib/send_with_us/api.rb', line 248

def log(log_id)
  endpoint = "logs/#{log_id}"

  SendWithUs::ApiRequest.new(@configuration).get(endpoint)
end

#log_events(log_id) ⇒ Object



254
255
256
257
258
# File 'lib/send_with_us/api.rb', line 254

def log_events(log_id)
  endpoint = "logs/#{log_id}/events"

  SendWithUs::ApiRequest.new(@configuration).get(endpoint)
end

#remove_from_drip_campaign(recipient_address, drip_campaign_id) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/send_with_us/api.rb', line 199

def remove_from_drip_campaign(recipient_address, drip_campaign_id)
  payload = {
    recipient_address: recipient_address
  }

  payload = payload.to_json
  SendWithUs::ApiRequest.new(@configuration).post("drip_campaigns/#{drip_campaign_id}/deactivate", payload)
end

#render(template_id, version_id = nil, template_data = {}, strict = false) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/send_with_us/api.rb', line 147

def render(template_id, version_id = nil, template_data = {}, strict = false)
  locale = template_data[:locale]

  payload = {
    template: template_id,
    template_data: template_data,
    strict: strict,
  }

  payload[:version_id] = version_id if version_id
  payload[:locale] = locale if locale

  payload = payload.to_json
  SendWithUs::ApiRequest.new(@configuration).post(:'render', payload)
end

#send_email(email_id, to, options = {}) ⇒ Object

Sends the specified email with any optional arguments

  • Args :

    • email_id -> ID of the email to send

    • to -> Hash of recipient details

  • Options :

    • :data -> Hash of email data

    • :from -> Hash of sender details

    • :cc -> Array of CC recipients

    • :bcc -> Array of BCC recipients

    • :files -> Array of attachments

    • :esp_account -> The ESP account to use

    • :version_name -> The specific email version to use

    • :headers -> Hash of headers

    • :tags -> Array of tags

    • :locale -> Localization string

  • Notes :

    • “send” is already a ruby-defined method on all classes



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/send_with_us/api.rb', line 83

def send_email(email_id, to, options = {})
  if email_id.nil?
    raise SendWithUs::ApiNilEmailId, 'email_id cannot be nil'
  end

  payload = {
    email_id: email_id,
    recipient: to
  }

  if options[:data] && options[:data].any?
    payload[:email_data] = options[:data]
  end
  if options[:from] && options[:from].any?
    payload[:sender] = options[:from]
  end
  if options[:cc] && options[:cc].any?
    payload[:cc] = options[:cc]
  end
  if options[:bcc] && options[:bcc].any?
    payload[:bcc] = options[:bcc]
  end
  if options[:esp_account]
    payload[:esp_account] = options[:esp_account]
  end
  if options[:version_name]
    payload[:version_name] = options[:version_name]
  end
  if options[:headers] && options[:headers].any?
    payload[:headers] = options[:headers]
  end
  if options[:tags] && options[:tags].any?
    payload[:tags] = options[:tags]
  end
  if options[:locale]
    payload[:locale] = options[:locale]
  end

  if options[:files] && options[:files].any?
    payload[:files] = options[:files].map do |file_data|
      SendWithUs::File.new(file_data).to_h
    end
  end

  SendWithUs::ApiRequest.new(@configuration).post(:send, payload.to_json)
end

#send_with(email_id, to, data = {}, from = {}, cc = [], bcc = [], files = [], esp_account = '', version_name = '', headers = {}, tags = []) ⇒ Object

DEPRECATED: Please use ‘send_email’ instead.

Sends the specified email with any arguments.

  • Args :

    • email_id -> ID of the email to send

    • to -> Hash of recipient details

    • data -> Hash of email data

    • from -> Hash of sender details

    • cc -> Array of CC recipients

    • bcc -> Array of BCC recipients

    • files -> Array of attachments

    • esp_account -> The ESP account to use

    • version_name -> The specific email version to use

    • headers -> Hash of headers

    • tags -> Array of tags

  • Notes :

    • “send” is already a ruby-defined method on all classes



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/send_with_us/api.rb', line 46

def send_with(email_id, to, data = {}, from = {}, cc = [], bcc = [], files = [],  = '', version_name = '', headers = {}, tags = [])
  warn "[DEPRECATED] 'send_with' is deprecated.  Please use 'send_email' instead."

  send_email(
    email_id,
    to,
    data: data,
    from: from,
    cc: cc,
    bcc: bcc,
    files: files,
    esp_account: ,
    version_name: version_name,
    headers: headers,
    tags: tags
  )
end

#start_on_drip_campaign(recipient_address, drip_campaign_id, email_data = {}, locale = nil, tags = []) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/send_with_us/api.rb', line 181

def start_on_drip_campaign(recipient_address, drip_campaign_id, email_data={}, locale=nil, tags=[])
  payload = {recipient_address: recipient_address}

  if email_data && email_data.any?
    payload[:email_data] = email_data
  end
  if tags.any?
    payload[:tags] = tags
  end
  if locale
    payload[:locale] = locale
  end

  payload = payload.to_json
  endpoint = "drip_campaigns/#{drip_campaign_id}/activate"
  SendWithUs::ApiRequest.new(@configuration).post(endpoint, payload)
end

#update_template_version(template_id, version_id, name, subject, html, text, preheader = '', amp_html = '') ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/send_with_us/api.rb', line 275

def update_template_version(template_id, version_id, name, subject, html, text, preheader='', amp_html='')
  payload = {
    name: name,
    subject: subject,
    html: html,
    text: text,
    preheader: preheader,
    amp_html: amp_html
  }

  endpoint = "templates/#{template_id}/versions/#{version_id}"
  SendWithUs::ApiRequest.new(@configuration).put(endpoint, payload.to_json)
end