File: api_client.rb

package info (click to toggle)
ruby-postmark 1.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 5,413; makefile: 4
file content (407 lines) | stat: -rw-r--r-- 12,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
module Postmark
  class ApiClient < Client
    attr_accessor :max_batch_size

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

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

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

    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

    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

    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

    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

    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

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

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

      response
    end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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

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

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

    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

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

    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

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

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

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

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

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

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

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

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

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

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

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

    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

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

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

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

    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

    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

    protected

    def in_batches(messages)
      r = messages.each_slice(max_batch_size).each_with_index.map do |batch, i|
        yield batch, i * max_batch_size
      end

      format_response r.flatten
    end

    def update_message(message, response)
      response ||= {}
      message['X-PM-Message-Id'] = response['MessageID']
      message.delivered = response['ErrorCode'] && response['ErrorCode'].zero?
      message.postmark_response = response
    end

    def get_for_message(action, id, options = {})
      path, _, params = extract_messages_path_and_params(options)
      format_response http_client.get("#{path}/#{id}/#{action}", params)
    end

    def extract_messages_path_and_params(options = {})
      options = options.dup
      messages_key = options[:inbound] ? 'InboundMessages' : 'Messages'
      path = options.delete(:inbound) ? 'messages/inbound' : 'messages/outbound'
      [path, messages_key, options]
    end
  end
end