class PostageApp::Request

A set of methods that are useful when request needs to behave as Mail

Constants

API_VERSION

Constants ============================================================

HEADERS_DEFAULT
TimeoutError

Attributes

api_key[RW]

Assigns the API key to be used for the request

arguments[RW]

A list of arguments in a Hash format passed along with the request

delivery_handler[RW]
method[RW]

The API method being called (example: send_message) This controls the url of the request (example: api.postageapp.com/v.1.0/send_message.json)

perform_deliveries[RW]
raise_delivery_errors[RW]
uid[W]

Unique ID (UID) for the request

Public Class Methods

new(method, arguments = nil) click to toggle source

Creates a new Request with the given API call method and arguments.

# File lib/postageapp/request.rb, line 43
def initialize(method, arguments = nil)
  @method = method
  @arguments = arguments ? arguments.dup : { }

  @uid = @arguments.delete('uid')
  @api_key = @arguments.delete('api_key') || PostageApp.configuration.api_key
end
user_agent() click to toggle source

Returns a user-agent string used for identification when making API calls.

# File lib/postageapp/request.rb, line 31
def self.user_agent
  @user_agent ||=
    "PostageApp (Gem %s, Ruby %s, %s)" % [
      PostageApp::VERSION,
      RUBY_VERSION,
      PostageApp.configuration.framework
    ]
end

Public Instance Methods

[](key) click to toggle source
# File lib/postageapp/request.rb, line 189
def [](key)
  case (key)
  when :to, 'to'
    self.to
  when :from, 'from'
    self.from
  when :bcc, 'bcc'
    # Not supported via API at this time
    [ ]
  end
end
arguments_to_send() click to toggle source

Returns the arguments that will be used to send this request.

# File lib/postageapp/request.rb, line 105
def arguments_to_send
  hash = {
    'uid' => self.uid,
    'api_key' => self.api_key
  }

  if (self.arguments && !self.arguments.empty?)
    case (self.method.to_sym)
    when :send_message
      if (PostageApp.configuration.recipient_override)
        self.arguments.merge!(
          'recipient_override' => PostageApp.configuration.recipient_override
        )
      end
    end

    hash.merge!(
      'arguments' => self.arguments.recursive_stringify_keys!
    )
  end
  
  hash
end
attachments() click to toggle source
# File lib/postageapp/request.rb, line 163
def attachments
  self.arguments['attachments']
end
bcc() click to toggle source
# File lib/postageapp/request.rb, line 226
def bcc
  # Not supported natively via API at this time
  [ ]
end
bcc=(list) click to toggle source
# File lib/postageapp/request.rb, line 231
def bcc=(list)
  # Not supported natively via API at this time
end
body() click to toggle source
_content and (_content['text/html'] or _content['text/plain'])

end

# File lib/postageapp/request.rb, line 252
def body
  out = self.arguments_to_send.dig('arguments', 'content')
  out.is_a?(Hash) ? out.values.join("\n\n") : out.to_s
end
cc() click to toggle source
# File lib/postageapp/request.rb, line 159
def cc
  self.header['cc']
end
content() click to toggle source
# File lib/postageapp/request.rb, line 129
def content
  self.arguments['content'] ||= { }
end
deliver()
Alias for: deliver_now
deliver_now() click to toggle source

Either doing an actual send, or passing it along to Mail::TestMailer Probably not the best way as we're skipping way too many intermediate methods

# File lib/postageapp/mailer/mailer_4.rb, line 214
def deliver_now
  inform_interceptors

  return unless (perform_deliveries)

  if (@delivery_method == Mail::TestMailer)
    @delivery_method.deliveries << self
  else
    self.send
  end
end
Also aliased as: deliver
delivery_method(method = nil, settings = nil) click to toggle source

Allows overriding the delivery method setting

# File lib/postageapp/mailer/mailer_4.rb, line 228
def delivery_method(method = nil, settings = nil)
  @delivery_method = method
end
find_first_mime_type(type) click to toggle source
# File lib/postageapp/request.rb, line 143
def find_first_mime_type(type)
  self.content[type]
end
from() click to toggle source
# File lib/postageapp/request.rb, line 216
def from
  [ self.arguments_to_send.dig('arguments', 'headers', 'from') ].flatten
end
from=(address) click to toggle source
# File lib/postageapp/request.rb, line 220
def from=(address)
  _headers = self.arguments['headers'] ||= { }

  _headers['from'] = address.to_s
end
header() click to toggle source
# File lib/postageapp/request.rb, line 151
def header
  self.arguments['headers'] ||= { }
end
headers(value = nil) click to toggle source

Getter and setter for headers. You can specify headers in the following formats:

headers['Custom-Header'] = 'Custom Value'
headers 'Custom-Header-1' => 'Custom Value 1',
        'Custom-Header-2' => 'Custom Value 2'
# File lib/postageapp/request.rb, line 176
def headers(value = nil)
  _headers = self.arguments['headers'] ||= { }

  case (value)
  when Hash
    value.each do |k, v|
      _headers[k.to_s] = v.to_s
    end
  end

  _headers
end
html_part() click to toggle source

– Mail::Message Emulation ———————————————-

# File lib/postageapp/request.rb, line 135
def html_part
  self.content['text/html']
end
inform_interceptors() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 208
def inform_interceptors
  Mail.inform_interceptors(self)
end
mime_type() click to toggle source
# File lib/postageapp/request.rb, line 147
def mime_type
  self.content.keys.first
end
multipart?() click to toggle source
# File lib/postageapp/request.rb, line 167
def multipart?
  self.content.keys.length > 1
end
reply_to() click to toggle source
# File lib/postageapp/request.rb, line 155
def reply_to
  self.header['reply-to']
end
send(skip_failed_requests_processing = false) click to toggle source

Skipping resend doesn't trigger PostageApp::FailedRequest.resend_all it's needed so the request being resend doesn't create duplicate queue

# File lib/postageapp/request.rb, line 53
def send(skip_failed_requests_processing = false)
  http = PostageApp.configuration.http

  PostageApp.logger.info(self)

  if (ENV['DEBUG'])
    puts "// #{url}"
    puts JSON.pretty_generate(self.arguments_to_send)
  end
  
  http_response =
    begin
      http.post(
        url.path, 
        self.arguments_to_send.to_json, 
        HEADERS_DEFAULT.merge(
          'User-Agent' => self.class.user_agent
        )
      )

    rescue TimeoutError, Errno::ECONNREFUSED => e
      e
    end
  
  response = PostageApp::Response.new(http_response)
  
  PostageApp.logger.info(response)
  
  unless (skip_failed_requests_processing)
    if (response.fail?)
      PostageApp::FailedRequest.store(self)
    elsif (response.ok?)
      PostageApp::FailedRequest.resend_all
    end
  end
  
  response
end
subject() click to toggle source
# File lib/postageapp/request.rb, line 235
def subject
  self.arguments_to_send.dig('arguments', 'headers', 'subject')
end
subject=(subject) click to toggle source
# File lib/postageapp/request.rb, line 239
def subject=(subject)
  _headers = self.arguments['headers'] ||= { }

  _headers['subject'] = subject.to_s
end
text_part() click to toggle source
# File lib/postageapp/request.rb, line 139
def text_part
  self.content['text/plain']
end
to() click to toggle source
# File lib/postageapp/request.rb, line 201
def to
  out = self.arguments_to_send.dig('arguments', 'recipients')

  case (out)
  when Hash
    out
  else
    [ out ].flatten
  end
end
to=(list) click to toggle source
# File lib/postageapp/request.rb, line 212
def to=(list)
  self.arguments['recipients'] = list
end
uid(reload = false) click to toggle source

Unique ID of the request

# File lib/postageapp/request.rb, line 98
def uid(reload = false)
  @uid = nil if (reload)

  @uid ||= Digest::SHA1.hexdigest("#{rand}#{Time.now.to_f}#{self.arguments}")
end
url() click to toggle source

URL of the where PostageApp::Request will be directed at

# File lib/postageapp/request.rb, line 93
def url
  URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json")
end