Class: Postmark::ApiClient

Inherits:
Client
  • Object
show all
Defined in:
lib/postmark/api_client.rb

Instance Attribute Summary collapse

Attributes inherited from Client

#http_client, #max_retries

Instance Method Summary collapse

Methods inherited from Client

#api_token=, #find_each

Constructor Details

#initialize(api_token, options = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



5
6
7
8
9
# File 'lib/postmark/api_client.rb', line 5

def initialize(api_token, options = {})
  options = options.dup
  @max_batch_size = options.delete(:max_batch_size) || 500
  super
end

Instance Attribute Details

#max_batch_sizeObject

Returns the value of attribute max_batch_size.



3
4
5
# File 'lib/postmark/api_client.rb', line 3

def max_batch_size
  @max_batch_size
end

Instance Method Details

#activate_bounce(id) ⇒ Object



147
148
149
# File 'lib/postmark/api_client.rb', line 147

def activate_bounce(id)
  format_response http_client.put("bounces/#{id}/activate")["Bounce"]
end

#archive_message_stream(id) ⇒ Object



355
356
357
# File 'lib/postmark/api_client.rb', line 355

def archive_message_stream(id)
  format_response http_client.post("message-streams/#{id}/archive")
end

#bounces(options = {}) ⇒ Object



130
131
132
# File 'lib/postmark/api_client.rb', line 130

def bounces(options = {})
  find_each('bounces', 'Bounces', options)
end

#clicks(options = {}) ⇒ Object



155
156
157
# File 'lib/postmark/api_client.rb', line 155

def clicks(options = {})
  find_each('messages/outbound/clicks', 'Clicks', options)
end

#clicks_by_message_id(message_id, options = {}) ⇒ Object



187
188
189
# File 'lib/postmark/api_client.rb', line 187

def clicks_by_message_id(message_id, options = {})
  find_each("messages/outbound/clicks/#{message_id}", 'Clicks', options)
end

#create_message_stream(attributes = {}) ⇒ Object



345
346
347
348
# File 'lib/postmark/api_client.rb', line 345

def create_message_stream(attributes = {})
  data = serialize(HashHelper.to_postmark(attributes, :deep => true))
  format_response(http_client.post('message-streams', data), :deep => true)
end

#create_suppressions(stream_id, email_addresses) ⇒ Object



368
369
370
371
# File 'lib/postmark/api_client.rb', line 368

def create_suppressions(stream_id, email_addresses)
  data = serialize(:Suppressions => Array(email_addresses).map { |e| HashHelper.to_postmark(:email_address => e) })
  format_response(http_client.post("message-streams/#{stream_id}/suppressions", data))
end

#create_template(attributes = {}) ⇒ Object



238
239
240
241
242
# File 'lib/postmark/api_client.rb', line 238

def create_template(attributes = {})
  data = serialize(HashHelper.to_postmark(attributes))

  format_response http_client.post('templates', data)
end

#create_trigger(type, options) ⇒ Object



191
192
193
194
195
# File 'lib/postmark/api_client.rb', line 191

def create_trigger(type, options)
  type = Postmark::Inflector.to_postmark(type).downcase
  data = serialize(HashHelper.to_postmark(options))
  format_response http_client.post("triggers/#{type}", data)
end

#create_webhook(attributes = {}) ⇒ Object



316
317
318
319
320
# File 'lib/postmark/api_client.rb', line 316

def create_webhook(attributes = {})
  data = serialize(HashHelper.to_postmark(attributes))

  format_response http_client.post('webhooks', data)
end

#delete_suppressions(stream_id, email_addresses) ⇒ Object



373
374
375
376
# File 'lib/postmark/api_client.rb', line 373

def delete_suppressions(stream_id, email_addresses)
  data = serialize(:Suppressions => Array(email_addresses).map { |e| HashHelper.to_postmark(:email_address => e) })
  format_response(http_client.post("message-streams/#{stream_id}/suppressions/delete", data))
end

#delete_template(id) ⇒ Object



250
251
252
# File 'lib/postmark/api_client.rb', line 250

def delete_template(id)
  format_response http_client.delete("templates/#{id}")
end

#delete_trigger(type, id) ⇒ Object



201
202
203
204
# File 'lib/postmark/api_client.rb', line 201

def delete_trigger(type, id)
  type = Postmark::Inflector.to_postmark(type).downcase
  format_response http_client.delete("triggers/#{type}/#{id}")
end

#delete_webhook(id) ⇒ Object



328
329
330
# File 'lib/postmark/api_client.rb', line 328

def delete_webhook(id)
  format_response http_client.delete("webhooks/#{id}")
end

#deliver(message_hash = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/postmark/api_client.rb', line 11

def deliver(message_hash = {})
  data = serialize(MessageHelper.to_postmark(message_hash))

  with_retries do
    format_response http_client.post("email", data)
  end
end

#deliver_in_batches(message_hashes) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/postmark/api_client.rb', line 19

def deliver_in_batches(message_hashes)
  in_batches(message_hashes) do |batch, offset|
    data = serialize(batch.map { |h| MessageHelper.to_postmark(h) })

    with_retries do
      http_client.post("email/batch", data)
    end
  end
end

#deliver_in_batches_with_templates(message_hashes) ⇒ Object



282
283
284
285
286
287
288
289
290
291
# File 'lib/postmark/api_client.rb', line 282

def deliver_in_batches_with_templates(message_hashes)
  in_batches(message_hashes) do |batch, offset|
    mapped = batch.map { |h| MessageHelper.to_postmark(h) }
    data = serialize(:Messages => mapped)

    with_retries do
      http_client.post('email/batchWithTemplates', data)
    end
  end
end

#deliver_message(message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/postmark/api_client.rb', line 29

def deliver_message(message)
  if message.templated?
    raise ArgumentError,
          "Please use #{self.class}#deliver_message_with_template to deliver messages with templates."
  end

  data = serialize(message.to_postmark_hash)

  with_retries do
    response, error = take_response_of { http_client.post("email", data) }
    update_message(message, response)
    raise error if error
    format_response(response, :compatible => true)
  end
end

#deliver_message_with_template(message) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/postmark/api_client.rb', line 45

def deliver_message_with_template(message)
  raise ArgumentError, 'Templated delivery requested, but the template is missing.' unless message.templated?

  data = serialize(message.to_postmark_hash)

  with_retries do
    response, error = take_response_of { http_client.post("email/withTemplate", data) }
    update_message(message, response)
    raise error if error
    format_response(response, :compatible => true)
  end
end

#deliver_messages(messages) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/postmark/api_client.rb', line 58

def deliver_messages(messages)
  if messages.any? { |m| m.templated? }
    raise ArgumentError,
          "Some of the provided messages have templates. Please use " \
          "#{self.class}#deliver_messages_with_templates to deliver those."
  end

  in_batches(messages) do |batch, offset|
    data = serialize(batch.map { |m| m.to_postmark_hash })

    with_retries do
      http_client.post("email/batch", data).tap do |response|
        response.each_with_index do |r, i|
          update_message(messages[offset + i], r)
        end
      end
    end
  end
end

#deliver_messages_with_templates(messages) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/postmark/api_client.rb', line 78

def deliver_messages_with_templates(messages)
  unless messages.all? { |m| m.templated? }
    raise ArgumentError, 'Templated delivery requested, but one or more messages lack templates.'
  end

  in_batches(messages) do |batch, offset|
    mapped = batch.map { |m| m.to_postmark_hash }
    data = serialize(:Messages => mapped)

    with_retries do
      http_client.post("email/batchWithTemplates", data).tap do |response|
        response.each_with_index do |r, i|
          update_message(messages[offset + i], r)
        end
      end
    end
  end
end

#deliver_with_template(attributes = {}) ⇒ Object



274
275
276
277
278
279
280
# File 'lib/postmark/api_client.rb', line 274

def deliver_with_template(attributes = {})
  data = serialize(MessageHelper.to_postmark(attributes))

  with_retries do
    format_response http_client.post('email/withTemplate', data)
  end
end

#delivery_statsObject



97
98
99
100
101
102
103
104
105
# File 'lib/postmark/api_client.rb', line 97

def delivery_stats
  response = format_response(http_client.get("deliverystats"), :compatible => true)

  if response[:bounces]
    response[:bounces] = format_response(response[:bounces])
  end

  response
end

#dump_bounce(id) ⇒ Object



143
144
145
# File 'lib/postmark/api_client.rb', line 143

def dump_bounce(id)
  format_response http_client.get("bounces/#{id}/dump")
end

#dump_message(id, options = {}) ⇒ Object



126
127
128
# File 'lib/postmark/api_client.rb', line 126

def dump_message(id, options = {})
  get_for_message('dump', id, options)
end

#dump_suppressions(stream_id, options = {}) ⇒ Object



363
364
365
366
# File 'lib/postmark/api_client.rb', line 363

def dump_suppressions(stream_id, options = {})
  _, batch = load_batch("message-streams/#{stream_id}/suppressions/dump", 'Suppressions', options)
  batch
end

#get_bounce(id) ⇒ Object



139
140
141
# File 'lib/postmark/api_client.rb', line 139

def get_bounce(id)
  format_response http_client.get("bounces/#{id}")
end

#get_bounces(options = {}) ⇒ Object



134
135
136
137
# File 'lib/postmark/api_client.rb', line 134

def get_bounces(options = {})
  _, batch = load_batch('bounces', 'Bounces', options)
  batch
end

#get_clicks(options = {}) ⇒ Object



164
165
166
167
# File 'lib/postmark/api_client.rb', line 164

def get_clicks(options = {})
  _, batch = load_batch('messages/outbound/clicks', 'Clicks', options)
  batch
end

#get_clicks_by_message_id(message_id, options = {}) ⇒ Object



176
177
178
179
180
181
# File 'lib/postmark/api_client.rb', line 176

def get_clicks_by_message_id(message_id, options = {})
  _, batch = load_batch("messages/outbound/clicks/#{message_id}",
                        'Clicks',
                        options)
  batch
end

#get_message(id, options = {}) ⇒ Object



122
123
124
# File 'lib/postmark/api_client.rb', line 122

def get_message(id, options = {})
  get_for_message('details', id, options)
end

#get_message_stream(id) ⇒ Object



341
342
343
# File 'lib/postmark/api_client.rb', line 341

def get_message_stream(id)
  format_response(http_client.get("message-streams/#{id}"))
end

#get_message_streams(options = {}) ⇒ Object



332
333
334
335
# File 'lib/postmark/api_client.rb', line 332

def get_message_streams(options = {})
  _, batch = load_batch('message-streams', 'MessageStreams', options)
  batch
end

#get_messages(options = {}) ⇒ Object



112
113
114
115
# File 'lib/postmark/api_client.rb', line 112

def get_messages(options = {})
  path, name, params = extract_messages_path_and_params(options)
  load_batch(path, name, params).last
end

#get_messages_count(options = {}) ⇒ Object



117
118
119
120
# File 'lib/postmark/api_client.rb', line 117

def get_messages_count(options = {})
  path, _, params = extract_messages_path_and_params(options)
  get_resource_count(path, params)
end

#get_opens(options = {}) ⇒ Object



159
160
161
162
# File 'lib/postmark/api_client.rb', line 159

def get_opens(options = {})
  _, batch = load_batch('messages/outbound/opens', 'Opens', options)
  batch
end

#get_opens_by_message_id(message_id, options = {}) ⇒ Object



169
170
171
172
173
174
# File 'lib/postmark/api_client.rb', line 169

def get_opens_by_message_id(message_id, options = {})
  _, batch = load_batch("messages/outbound/opens/#{message_id}",
                        'Opens',
                        options)
  batch
end

#get_stats_counts(stat, options = {}) ⇒ Object



297
298
299
300
301
302
303
304
# File 'lib/postmark/api_client.rb', line 297

def get_stats_counts(stat, options = {})
  url = "stats/outbound/#{stat}"
  url << "/#{options[:type]}" if options.has_key?(:type)

  response = format_response(http_client.get(url, options))
  response[:days].map! { |d| HashHelper.to_ruby(d) }
  response
end

#get_stats_totals(options = {}) ⇒ Object



293
294
295
# File 'lib/postmark/api_client.rb', line 293

def get_stats_totals(options = {})
  format_response(http_client.get('stats/outbound', options))
end

#get_template(id) ⇒ Object



234
235
236
# File 'lib/postmark/api_client.rb', line 234

def get_template(id)
  format_response http_client.get("templates/#{id}")
end

#get_templates(options = {}) ⇒ Object



226
227
228
# File 'lib/postmark/api_client.rb', line 226

def get_templates(options = {})
  load_batch('templates', 'Templates', options)
end

#get_trigger(type, id) ⇒ Object



197
198
199
# File 'lib/postmark/api_client.rb', line 197

def get_trigger(type, id)
  format_response http_client.get("triggers/#{type}/#{id}")
end

#get_triggers(type, options = {}) ⇒ Object



206
207
208
209
210
# File 'lib/postmark/api_client.rb', line 206

def get_triggers(type, options = {})
  type = Postmark::Inflector.to_postmark(type)
  _, batch = load_batch("triggers/#{type.downcase}", type, options)
  batch
end

#get_webhook(id) ⇒ Object



312
313
314
# File 'lib/postmark/api_client.rb', line 312

def get_webhook(id)
  format_response http_client.get("webhooks/#{id}")
end

#get_webhooks(options = {}) ⇒ Object



306
307
308
309
310
# File 'lib/postmark/api_client.rb', line 306

def get_webhooks(options = {})
  options = HashHelper.to_postmark(options)
  _, batch = load_batch('webhooks', 'Webhooks', options)
  batch
end

#message_streams(options = {}) ⇒ Object



337
338
339
# File 'lib/postmark/api_client.rb', line 337

def message_streams(options = {})
  find_each('message-streams', 'MessageStreams', options)
end

#messages(options = {}) ⇒ Object



107
108
109
110
# File 'lib/postmark/api_client.rb', line 107

def messages(options = {})
  path, name, params = extract_messages_path_and_params(options)
  find_each(path, name, params)
end

#opens(options = {}) ⇒ Object



151
152
153
# File 'lib/postmark/api_client.rb', line 151

def opens(options = {})
  find_each('messages/outbound/opens', 'Opens', options)
end

#opens_by_message_id(message_id, options = {}) ⇒ Object



183
184
185
# File 'lib/postmark/api_client.rb', line 183

def opens_by_message_id(message_id, options = {})
  find_each("messages/outbound/opens/#{message_id}", 'Opens', options)
end

#server_infoObject



217
218
219
# File 'lib/postmark/api_client.rb', line 217

def server_info
  format_response http_client.get("server")
end

#templates(options = {}) ⇒ Object



230
231
232
# File 'lib/postmark/api_client.rb', line 230

def templates(options = {})
  find_each('templates', 'Templates', options)
end

#triggers(type, options = {}) ⇒ Object



212
213
214
215
# File 'lib/postmark/api_client.rb', line 212

def triggers(type, options = {})
  type = Postmark::Inflector.to_postmark(type)
  find_each("triggers/#{type.downcase}", type, options)
end

#unarchive_message_stream(id) ⇒ Object



359
360
361
# File 'lib/postmark/api_client.rb', line 359

def unarchive_message_stream(id)
  format_response http_client.post("message-streams/#{id}/unarchive")
end

#update_message_stream(id, attributes) ⇒ Object



350
351
352
353
# File 'lib/postmark/api_client.rb', line 350

def update_message_stream(id, attributes)
  data = serialize(HashHelper.to_postmark(attributes, :deep => true))
  format_response(http_client.patch("message-streams/#{id}", data), :deep => true)
end

#update_server_info(attributes = {}) ⇒ Object



221
222
223
224
# File 'lib/postmark/api_client.rb', line 221

def update_server_info(attributes = {})
  data = HashHelper.to_postmark(attributes)
  format_response http_client.put("server", serialize(data))
end

#update_template(id, attributes = {}) ⇒ Object



244
245
246
247
248
# File 'lib/postmark/api_client.rb', line 244

def update_template(id, attributes = {})
  data = serialize(HashHelper.to_postmark(attributes))

  format_response http_client.put("templates/#{id}", data)
end

#update_webhook(id, attributes = {}) ⇒ Object



322
323
324
325
326
# File 'lib/postmark/api_client.rb', line 322

def update_webhook(id, attributes = {})
  data = serialize(HashHelper.to_postmark(attributes))

  format_response http_client.put("webhooks/#{id}", data)
end

#validate_template(attributes = {}) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/postmark/api_client.rb', line 254

def validate_template(attributes = {})
  data = serialize(HashHelper.to_postmark(attributes))
  response = format_response(http_client.post('templates/validate', data))

  response.each do |k, v|
    next unless v.is_a?(Hash) && k != :suggested_template_model

    response[k] = HashHelper.to_ruby(v)

    if response[k].has_key?(:validation_errors)
      ruby_hashes = response[k][:validation_errors].map do |err|
        HashHelper.to_ruby(err)
      end
      response[k][:validation_errors] = ruby_hashes
    end
  end

  response
end