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
Assigns the API key to be used for the request
A list of arguments in a Hash
format passed along with the request
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)
Unique ID (UID) for the request
Public Class Methods
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
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
# 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
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
# File lib/postageapp/request.rb, line 163 def attachments self.arguments['attachments'] end
# File lib/postageapp/request.rb, line 226 def bcc # Not supported natively via API at this time [ ] end
# File lib/postageapp/request.rb, line 231 def bcc=(list) # Not supported natively via API at this time end
_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
# File lib/postageapp/request.rb, line 159 def cc self.header['cc'] end
# File lib/postageapp/request.rb, line 129 def content self.arguments['content'] ||= { } end
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
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
# File lib/postageapp/request.rb, line 143 def find_first_mime_type(type) self.content[type] end
# File lib/postageapp/request.rb, line 216 def from [ self.arguments_to_send.dig('arguments', 'headers', 'from') ].flatten end
# File lib/postageapp/request.rb, line 220 def from=(address) _headers = self.arguments['headers'] ||= { } _headers['from'] = address.to_s end
# File lib/postageapp/request.rb, line 151 def header self.arguments['headers'] ||= { } end
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
– Mail::Message Emulation ———————————————-
# File lib/postageapp/request.rb, line 135 def html_part self.content['text/html'] end
# File lib/postageapp/mailer/mailer_4.rb, line 208 def inform_interceptors Mail.inform_interceptors(self) end
# File lib/postageapp/request.rb, line 147 def mime_type self.content.keys.first end
# File lib/postageapp/request.rb, line 167 def multipart? self.content.keys.length > 1 end
# File lib/postageapp/request.rb, line 155 def reply_to self.header['reply-to'] end
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
# File lib/postageapp/request.rb, line 235 def subject self.arguments_to_send.dig('arguments', 'headers', 'subject') end
# File lib/postageapp/request.rb, line 239 def subject=(subject) _headers = self.arguments['headers'] ||= { } _headers['subject'] = subject.to_s end
# File lib/postageapp/request.rb, line 139 def text_part self.content['text/plain'] end
# 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
# File lib/postageapp/request.rb, line 212 def to=(list) self.arguments['recipients'] = list end
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 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