class Postmark::ApiClient

Attributes

max_batch_size[RW]

Public Class Methods

new(api_token, options = {}) click to toggle source
Calls superclass method
# 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

Public Instance Methods

activate_bounce(id) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# File lib/postmark/api_client.rb, line 130
def bounces(options = {})
  find_each('bounces', 'Bounces', options)
end
clicks(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/postmark/api_client.rb, line 250
def delete_template(id)
  format_response http_client.delete("templates/#{id}")
end
delete_trigger(type, id) click to toggle source
# 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) click to toggle source
# File lib/postmark/api_client.rb, line 328
def delete_webhook(id)
  format_response http_client.delete("webhooks/#{id}")
end
deliver(message_hash = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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_stats() click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/postmark/api_client.rb, line 139
def get_bounce(id)
  format_response http_client.get("bounces/#{id}")
end
get_bounces(options = {}) click to toggle source
# File lib/postmark/api_client.rb, line 134
def get_bounces(options = {})
  _, batch = load_batch('bounces', 'Bounces', options)
  batch
end
get_clicks(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/postmark/api_client.rb, line 122
def get_message(id, options = {})
  get_for_message('details', id, options)
end
get_message_stream(id) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/postmark/api_client.rb, line 234
def get_template(id)
  format_response http_client.get("templates/#{id}")
end
get_templates(options = {}) click to toggle source
# File lib/postmark/api_client.rb, line 226
def get_templates(options = {})
  load_batch('templates', 'Templates', options)
end
get_trigger(type, id) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/postmark/api_client.rb, line 312
def get_webhook(id)
  format_response http_client.get("webhooks/#{id}")
end
get_webhooks(options = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/postmark/api_client.rb, line 337
def message_streams(options = {})
  find_each('message-streams', 'MessageStreams', options)
end
messages(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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_info() click to toggle source
# File lib/postmark/api_client.rb, line 217
def server_info
  format_response http_client.get("server")
end
templates(options = {}) click to toggle source
# File lib/postmark/api_client.rb, line 230
def templates(options = {})
  find_each('templates', 'Templates', options)
end
triggers(type, options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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

Protected Instance Methods

extract_messages_path_and_params(options = {}) click to toggle source
# File lib/postmark/api_client.rb, line 400
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
get_for_message(action, id, options = {}) click to toggle source
# File lib/postmark/api_client.rb, line 395
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
in_batches(messages) { |batch, i * max_batch_size| ... } click to toggle source
# File lib/postmark/api_client.rb, line 380
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
update_message(message, response) click to toggle source
# File lib/postmark/api_client.rb, line 388
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