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

DEFAULTS =
{
  host: 'api.postmarkapp.com',
  read_timeout: 60,
  open_timeout: 60
}.freeze

Instance Attribute Summary collapse

Attributes inherited from MailHandler::Sender

#dispatcher, #sending, #validate_response

Instance Method Summary collapse

Methods inherited from MailHandler::Sender

#dispatcher_client, #send_email

Constructor Details

#initialize(api_token = nil) ⇒ PostmarkAPISender

Returns a new instance of PostmarkAPISender.



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

def initialize(api_token = nil)
  super(:postmark_api)
  @host = DEFAULTS[:host]
  @api_token = api_token
  @use_ssl = true

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

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



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

def api_token
  @api_token
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#http_open_timeoutObject

Returns the value of attribute http_open_timeout.



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

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutObject

Returns the value of attribute http_read_timeout.



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

def http_read_timeout
  @http_read_timeout
end

#use_sslObject

Returns the value of attribute use_ssl.



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

def use_ssl
  @use_ssl
end

Instance Method Details

#clientObject



34
35
36
# File 'lib/mailhandler/sending/api.rb', line 34

def client
  init_client
end

#init_clientObject



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

def init_client
  @client = setup_sending_client
end

#send(email) ⇒ Object



27
28
29
30
31
32
# 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_clientObject



42
43
44
45
46
47
# 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) ⇒ Object



53
54
55
56
# File 'lib/mailhandler/sending/api.rb', line 53

def timeout=(value)
  @http_open_timeout = value
  @http_read_timeout = value
end

#valid_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# 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