class MailHandler::Sending::PostmarkAPISender

sending email by Postmark API

Constants

DEFAULTS

Attributes

api_token[RW]
host[RW]
http_open_timeout[RW]
http_read_timeout[RW]
use_ssl[RW]

Public Class Methods

new(api_token = nil) click to toggle source
# File lib/mailhandler/sending/api.rb, line 16
def initialize(api_token = nil)
  @type = :postmark_api
  @host = DEFAULTS[:host]
  @api_token = api_token
  @use_ssl = false

  @http_open_timeout = DEFAULTS[:open_timeout]
  @http_read_timeout = DEFAULTS[:read_timeout]
  init_client
end

Public Instance Methods

client() click to toggle source
# File lib/mailhandler/sending/api.rb, line 34
def client
  init_client
end
init_client() click to toggle source
# File lib/mailhandler/sending/api.rb, line 38
def init_client
  @client = setup_sending_client
end
send(email) click to toggle source
# File lib/mailhandler/sending/api.rb, line 27
def send(email)
  verify_email(email)
  init_client
  response = client.deliver_message(email)
  format_response(response)
end
setup_sending_client() click to toggle source
# File lib/mailhandler/sending/api.rb, line 42
def setup_sending_client
  # clearing cache so valid host is accepted, and not the cached one
  Postmark::HttpClient.instance_variable_set('@http', nil)
  Postmark::ApiClient.new(api_token, http_open_timeout: http_open_timeout, http_read_timeout: http_read_timeout,
                                     host: host, secure: @use_ssl)
end
timeout=(value) click to toggle source
# File lib/mailhandler/sending/api.rb, line 53
def timeout=(value)
  @http_open_timeout = value
  @http_read_timeout = value
end
valid_response?(response) click to toggle source
# File lib/mailhandler/sending/api.rb, line 49
def valid_response?(response)
  response[:message].to_s.strip.downcase == 'ok' && response[:error_code].to_s.downcase == '0'
end

Protected Instance Methods

format_response(response) click to toggle source
# File lib/mailhandler/sending/api.rb, line 66
def format_response(response)
  return response unless response.is_a? Hash
  return response if response.keys.select { |key| key.is_a? Symbol }.empty?

  response.keys.select { |key| key.is_a? String }.each { |s| response.delete(s) }
  response
end