Class: PostageApp::Request
- Inherits:
-
Object
- Object
- PostageApp::Request
- Defined in:
- lib/postageapp/mailer/mailer_4.rb,
lib/postageapp/request.rb
Overview
A set of methods that are useful when request needs to behave as Mail
Constant Summary collapse
- API_VERSION =
Constants ============================================================
'1.1'- HEADERS_DEFAULT =
{ 'Content-type' => 'application/json', 'Accept' => 'text/json, application/json' }
- TimeoutError =
defined?(::Timeout) ? ::Timeout::Error : ::TimeoutError
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Assigns the API key to be used for the request.
-
#arguments ⇒ Object
A list of arguments in a Hash format passed along with the request.
-
#delivery_handler ⇒ Object
Returns the value of attribute delivery_handler.
-
#method ⇒ Object
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 ⇒ Object
Returns the value of attribute perform_deliveries.
-
#raise_delivery_errors ⇒ Object
Returns the value of attribute raise_delivery_errors.
-
#uid(reload = false) ⇒ Object
Unique ID of the request.
Class Method Summary collapse
-
.user_agent ⇒ Object
Returns a user-agent string used for identification when making API calls.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#arguments_to_send ⇒ Object
Returns the arguments that will be used to send this request.
- #attachments ⇒ Object
- #bcc ⇒ Object
- #bcc=(list) ⇒ Object
- #body ⇒ Object
- #cc ⇒ Object
- #content ⇒ Object
-
#deliver_now ⇒ Object
(also: #deliver)
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.
-
#delivery_method(method = nil, settings = nil) ⇒ Object
Allows overriding the delivery method setting.
- #find_first_mime_type(type) ⇒ Object
- #from ⇒ Object
- #from=(address) ⇒ Object
- #header ⇒ Object
-
#headers(value = nil) ⇒ Object
Getter and setter for headers.
-
#html_part ⇒ Object
– Mail::Message Emulation ———————————————-.
- #inform_interceptors ⇒ Object
-
#initialize(method, arguments = nil) ⇒ Request
constructor
Creates a new Request with the given API call method and arguments.
- #mime_type ⇒ Object
- #multipart? ⇒ Boolean
- #reply_to ⇒ Object
-
#send(skip_failed_requests_processing = false) ⇒ Object
Skipping resend doesn’t trigger PostageApp::FailedRequest.resend_all it’s needed so the request being resend doesn’t create duplicate queue.
- #subject ⇒ Object
- #subject=(subject) ⇒ Object
- #text_part ⇒ Object
- #to ⇒ Object
- #to=(list) ⇒ Object
-
#url ⇒ Object
URL of the where PostageApp::Request will be directed at.
Constructor Details
#initialize(method, arguments = nil) ⇒ Request
Creates a new Request with the given API call method and arguments.
43 44 45 46 47 48 49 |
# 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 |
Instance Attribute Details
#api_key ⇒ Object
Assigns the API key to be used for the request
26 27 28 |
# File 'lib/postageapp/request.rb', line 26 def api_key @api_key end |
#arguments ⇒ Object
A list of arguments in a Hash format passed along with the request
23 24 25 |
# File 'lib/postageapp/request.rb', line 23 def arguments @arguments end |
#delivery_handler ⇒ Object
Returns the value of attribute delivery_handler.
204 205 206 |
# File 'lib/postageapp/mailer/mailer_4.rb', line 204 def delivery_handler @delivery_handler end |
#method ⇒ Object
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)
20 21 22 |
# File 'lib/postageapp/request.rb', line 20 def method @method end |
#perform_deliveries ⇒ Object
Returns the value of attribute perform_deliveries.
205 206 207 |
# File 'lib/postageapp/mailer/mailer_4.rb', line 205 def perform_deliveries @perform_deliveries end |
#raise_delivery_errors ⇒ Object
Returns the value of attribute raise_delivery_errors.
206 207 208 |
# File 'lib/postageapp/mailer/mailer_4.rb', line 206 def raise_delivery_errors @raise_delivery_errors end |
#uid(reload = false) ⇒ Object
Unique ID of the request
98 99 100 101 102 |
# 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 |
Class Method Details
.user_agent ⇒ Object
Returns a user-agent string used for identification when making API calls.
31 32 33 34 35 36 37 38 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# 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 ⇒ Object
Returns the arguments that will be used to send this request.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# 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 ⇒ Object
163 164 165 |
# File 'lib/postageapp/request.rb', line 163 def self.arguments['attachments'] end |
#bcc ⇒ Object
226 227 228 229 |
# File 'lib/postageapp/request.rb', line 226 def bcc # Not supported natively via API at this time [ ] end |
#bcc=(list) ⇒ Object
231 232 233 |
# File 'lib/postageapp/request.rb', line 231 def bcc=(list) # Not supported natively via API at this time end |
#body ⇒ Object
252 253 254 255 |
# 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 ⇒ Object
159 160 161 |
# File 'lib/postageapp/request.rb', line 159 def cc self.header['cc'] end |
#content ⇒ Object
129 130 131 |
# File 'lib/postageapp/request.rb', line 129 def content self.arguments['content'] ||= { } end |
#deliver_now ⇒ Object Also known as: deliver
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
214 215 216 217 218 219 220 221 222 223 224 |
# 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 |
#delivery_method(method = nil, settings = nil) ⇒ Object
Allows overriding the delivery method setting
228 229 230 |
# 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) ⇒ Object
143 144 145 |
# File 'lib/postageapp/request.rb', line 143 def find_first_mime_type(type) self.content[type] end |
#from ⇒ Object
216 217 218 |
# File 'lib/postageapp/request.rb', line 216 def from [ self.arguments_to_send.dig('arguments', 'headers', 'from') ].flatten end |
#from=(address) ⇒ Object
220 221 222 223 224 |
# File 'lib/postageapp/request.rb', line 220 def from=(address) _headers = self.arguments['headers'] ||= { } _headers['from'] = address.to_s end |
#header ⇒ Object
151 152 153 |
# File 'lib/postageapp/request.rb', line 151 def header self.arguments['headers'] ||= { } end |
#headers(value = nil) ⇒ Object
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'
176 177 178 179 180 181 182 183 184 185 186 187 |
# 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 ⇒ Object
– Mail::Message Emulation ———————————————-
135 136 137 |
# File 'lib/postageapp/request.rb', line 135 def html_part self.content['text/html'] end |
#inform_interceptors ⇒ Object
208 209 210 |
# File 'lib/postageapp/mailer/mailer_4.rb', line 208 def inform_interceptors Mail.inform_interceptors(self) end |
#mime_type ⇒ Object
147 148 149 |
# File 'lib/postageapp/request.rb', line 147 def mime_type self.content.keys.first end |
#multipart? ⇒ Boolean
167 168 169 |
# File 'lib/postageapp/request.rb', line 167 def multipart? self.content.keys.length > 1 end |
#reply_to ⇒ Object
155 156 157 |
# File 'lib/postageapp/request.rb', line 155 def reply_to self.header['reply-to'] end |
#send(skip_failed_requests_processing = false) ⇒ Object
Skipping resend doesn’t trigger PostageApp::FailedRequest.resend_all it’s needed so the request being resend doesn’t create duplicate queue
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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 ⇒ Object
235 236 237 |
# File 'lib/postageapp/request.rb', line 235 def subject self.arguments_to_send.dig('arguments', 'headers', 'subject') end |
#subject=(subject) ⇒ Object
239 240 241 242 243 |
# File 'lib/postageapp/request.rb', line 239 def subject=(subject) _headers = self.arguments['headers'] ||= { } _headers['subject'] = subject.to_s end |
#text_part ⇒ Object
139 140 141 |
# File 'lib/postageapp/request.rb', line 139 def text_part self.content['text/plain'] end |
#to ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# 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) ⇒ Object
212 213 214 |
# File 'lib/postageapp/request.rb', line 212 def to=(list) self.arguments['recipients'] = list end |
#url ⇒ Object
URL of the where PostageApp::Request will be directed at
93 94 95 |
# File 'lib/postageapp/request.rb', line 93 def url URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json") end |