Module: Postmark

Defined in:
lib/postmark/response_parsers/active_support.rb,
lib/postmark.rb,
lib/postmark/json.rb,
lib/postmark/bounce.rb,
lib/postmark/http_client.rb,
lib/postmark/response_parsers/json.rb,
lib/postmark/response_parsers/yajl.rb,
lib/postmark/attachments_fix_for_mail.rb,
lib/postmark/message_extensions/shared.rb

Overview

assume activesupport is already loaded

Defined Under Namespace

Modules: AttachmentsFixForMail, HttpClient, Json, ResponseParsers, SharedMessageExtensions Classes: Bounce, InternalServerError, InvalidApiKeyError, InvalidMessageError, UnknownError, UnknownMessageType

Constant Summary

HEADERS =
{
  'Content-type' => 'application/json',
  'Accept'       => 'application/json'
}
MAX_RETRIES =
2

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Object) api_key

Returns the value of attribute api_key



39
40
41
# File 'lib/postmark.rb', line 39

def api_key
  @api_key
end

+ (Object) host

The host to connect to.



54
55
56
# File 'lib/postmark.rb', line 54

def host
  @host ||= 'api.postmarkapp.com'
end

+ (Object) http_open_timeout

Returns the value of attribute http_open_timeout



39
40
41
# File 'lib/postmark.rb', line 39

def http_open_timeout
  @http_open_timeout ||= 5
end

+ (Object) http_read_timeout

Returns the value of attribute http_read_timeout



39
40
41
# File 'lib/postmark.rb', line 39

def http_read_timeout
  @http_read_timeout ||= 15
end

+ (Object) max_retries

Returns the value of attribute max_retries



39
40
41
# File 'lib/postmark.rb', line 39

def max_retries
  @max_retries ||= 3
end

+ (Object) path_prefix

The path of the listener



59
60
61
# File 'lib/postmark.rb', line 59

def path_prefix
  @path_prefix ||= '/'
end

+ (Object) port

The port on which your Postmark server runs.



49
50
51
# File 'lib/postmark.rb', line 49

def port
  @port || (secure ? 443 : 80)
end

+ (Object) proxy_host

Returns the value of attribute proxy_host



39
40
41
# File 'lib/postmark.rb', line 39

def proxy_host
  @proxy_host
end

+ (Object) proxy_pass

Returns the value of attribute proxy_pass



39
40
41
# File 'lib/postmark.rb', line 39

def proxy_pass
  @proxy_pass
end

+ (Object) proxy_port

Returns the value of attribute proxy_port



39
40
41
# File 'lib/postmark.rb', line 39

def proxy_port
  @proxy_port
end

+ (Object) proxy_user

Returns the value of attribute proxy_user



39
40
41
# File 'lib/postmark.rb', line 39

def proxy_user
  @proxy_user
end

+ (Object) response_parser_class



44
45
46
# File 'lib/postmark.rb', line 44

def response_parser_class
  @response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
end

+ (Object) secure

Returns the value of attribute secure



39
40
41
# File 'lib/postmark.rb', line 39

def secure
  @secure
end

+ (Object) sleep_between_retries

Returns the value of attribute sleep_between_retries



39
40
41
# File 'lib/postmark.rb', line 39

def sleep_between_retries
  @sleep_between_retries ||= 10
end

Class Method Details

+ (Object) configure {|_self| ... }

Yields:

  • (_self)

Yield Parameters:

  • _self (Postmark)

    the object that the method was called on



79
80
81
# File 'lib/postmark.rb', line 79

def configure
  yield self
end

+ (Object) convert_message_to_options_hash(message)



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
# File 'lib/postmark.rb', line 97

def convert_message_to_options_hash(message)
  options = Hash.new
  headers = extract_headers_according_to_message_format(message)

  options["From"]        = message['from'].to_s        if message.from
  options["ReplyTo"]     = message.reply_to.join(", ") if message.reply_to
  options["To"]          = message.to.join(", ")       if message.to
  options["Cc"]          = message.cc.join(", ")       if message.cc
  options["Bcc"]         = message.bcc.join(", ")      if message.bcc
  options["Subject"]     = message.subject
  options["Attachments"] = message.postmark_attachments
  options["Tag"]         = message.tag.to_s            if message.tag
  options["Headers"]     = headers                     if headers.size > 0
  
  options = options.delete_if{|k,v| v.nil?}

  html = message.body_html
  text = message.body_text
  
  if message.multipart?
    options["HtmlBody"] = html
    options["TextBody"] = text
  elsif html
    options["HtmlBody"] = html
  else
    options["TextBody"] = text
  end
  
  options
end

+ (Object) delivery_stats



128
129
130
# File 'lib/postmark.rb', line 128

def delivery_stats
  HttpClient.get("deliverystats")
end

+ (Object) send_through_postmark(message)

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/postmark.rb', line 83

def send_through_postmark(message) #:nodoc:
  @retries = 0
  begin
    HttpClient.post("email", Postmark::Json.encode(convert_message_to_options_hash(message)))
  rescue Exception => e
    if @retries < max_retries
       @retries += 1
       retry
    else
      raise
    end
  end
end