Class: MailHandler::Sending::PostmarkAPISender

Inherits:
MailHandler::Sender show all
Defined in:
lib/mailhandler/sending/api.rb

Overview

sending email by Postmark API

Direct Known Subclasses

PostmarkBatchAPISender

Constant Summary collapse

DEFAULT_HOST =
'api.postmarkapp.com'.freeze

Instance Attribute Summary collapse

Attributes inherited from MailHandler::Sender

#dispatcher, #sending

Instance Method Summary collapse

Methods inherited from MailHandler::Sender

#send_email

Constructor Details

#initialize(api_token = nil) ⇒ PostmarkAPISender

Returns a new instance of PostmarkAPISender.



16
17
18
19
20
21
22
23
24
# File 'lib/mailhandler/sending/api.rb', line 16

def initialize(api_token = nil)
  @type = :postmark_api
  @host = DEFAULT_HOST
  @api_token = api_token
  @use_ssl = false

  @http_open_timeout = 15
  @http_read_timeout = 15
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def api_token
  @api_token
end

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def client
  @client
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def host
  @host
end

#http_open_timeoutObject

Returns the value of attribute http_open_timeout.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutObject

Returns the value of attribute http_read_timeout.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def http_read_timeout
  @http_read_timeout
end

#use_sslObject

Returns the value of attribute use_ssl.



9
10
11
# File 'lib/mailhandler/sending/api.rb', line 9

def use_ssl
  @use_ssl
end

Instance Method Details

#init_clientObject



32
33
34
# File 'lib/mailhandler/sending/api.rb', line 32

def init_client
  @client = setup_sending_client
end

#send(email) ⇒ Object



26
27
28
29
30
# File 'lib/mailhandler/sending/api.rb', line 26

def send(email)
  verify_email(email)
  init_client
  client.deliver_message(email)
end

#setup_sending_clientObject



36
37
38
39
40
41
# File 'lib/mailhandler/sending/api.rb', line 36

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