Class: PostageApp::Request
- Inherits:
-
Object
- Object
- PostageApp::Request
- Defined in:
- lib/postageapp/mailer/mailer_3.rb,
lib/postageapp/request.rb,
lib/postageapp/mailer/mailer_4.rb
Overview
A set of methods that are useful when request needs to behave as Mail
Constant Summary collapse
- API_VERSION =
'1.0'- HEADERS_DEFAULT =
{ 'Content-type' => 'application/json', 'Accept' => 'text/json, application/json' }
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
Not 100% on this, but I need to assign this so I can properly handle deliver method.
- #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.
33 34 35 36 37 38 39 |
# File 'lib/postageapp/request.rb', line 33 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
20 21 22 |
# File 'lib/postageapp/request.rb', line 20 def api_key @api_key end |
#arguments ⇒ Object
A list of arguments in a Hash format passed along with the request
17 18 19 |
# File 'lib/postageapp/request.rb', line 17 def arguments @arguments end |
#delivery_handler ⇒ Object
Returns the value of attribute delivery_handler.
164 165 166 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 164 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)
14 15 16 |
# File 'lib/postageapp/request.rb', line 14 def method @method end |
#perform_deliveries ⇒ Object
Returns the value of attribute perform_deliveries.
165 166 167 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 165 def perform_deliveries @perform_deliveries end |
#raise_delivery_errors ⇒ Object
Returns the value of attribute raise_delivery_errors.
166 167 168 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 166 def raise_delivery_errors @raise_delivery_errors end |
#uid(reload = false) ⇒ Object
Unique ID of the request
82 83 84 85 86 |
# File 'lib/postageapp/request.rb', line 82 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.
23 24 25 26 27 28 29 30 |
# File 'lib/postageapp/request.rb', line 23 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
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/postageapp/request.rb', line 173 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/postageapp/request.rb', line 89 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
147 148 149 |
# File 'lib/postageapp/request.rb', line 147 def self.arguments['attachments'] end |
#bcc ⇒ Object
210 211 212 213 |
# File 'lib/postageapp/request.rb', line 210 def bcc # Not supported natively via API at this time [ ] end |
#bcc=(list) ⇒ Object
215 216 217 |
# File 'lib/postageapp/request.rb', line 215 def bcc=(list) # Not supported natively via API at this time end |
#body ⇒ Object
236 237 238 239 |
# File 'lib/postageapp/request.rb', line 236 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
143 144 145 |
# File 'lib/postageapp/request.rb', line 143 def cc self.header['cc'] end |
#content ⇒ Object
113 114 115 |
# File 'lib/postageapp/request.rb', line 113 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
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/postageapp/mailer/mailer_4.rb', line 198 def deliver_now inform_interceptors if (perform_deliveries) if (@delivery_method == Mail::TestMailer) @delivery_method.deliveries << self else self.send end end end |
#delivery_method(method = nil, settings = nil) ⇒ Object
Not 100% on this, but I need to assign this so I can properly handle deliver method
187 188 189 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 187 def delivery_method(method = nil, settings = { }) @delivery_method = method end |
#find_first_mime_type(type) ⇒ Object
127 128 129 |
# File 'lib/postageapp/request.rb', line 127 def find_first_mime_type(type) self.content[type] end |
#from ⇒ Object
200 201 202 |
# File 'lib/postageapp/request.rb', line 200 def from [ self.arguments_to_send.dig('arguments', 'headers', 'from') ].flatten end |
#from=(address) ⇒ Object
204 205 206 207 208 |
# File 'lib/postageapp/request.rb', line 204 def from=(address) _headers = self.arguments['headers'] ||= { } _headers['from'] = address.to_s end |
#header ⇒ Object
135 136 137 |
# File 'lib/postageapp/request.rb', line 135 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'
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/postageapp/request.rb', line 160 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 ———————————————-
119 120 121 |
# File 'lib/postageapp/request.rb', line 119 def html_part self.content['text/html'] end |
#inform_interceptors ⇒ Object
168 169 170 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 168 def inform_interceptors Mail.inform_interceptors(self) end |
#mime_type ⇒ Object
131 132 133 |
# File 'lib/postageapp/request.rb', line 131 def mime_type self.content.keys.first end |
#multipart? ⇒ Boolean
151 152 153 |
# File 'lib/postageapp/request.rb', line 151 def multipart? self.content.keys.length > 1 end |
#reply_to ⇒ Object
139 140 141 |
# File 'lib/postageapp/request.rb', line 139 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/postageapp/request.rb', line 43 def send(skip_failed_requests_processing = false) http = PostageApp.configuration.http PostageApp.logger.info(self) 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 nil 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
219 220 221 |
# File 'lib/postageapp/request.rb', line 219 def subject self.arguments_to_send.dig('arguments', 'headers', 'subject') end |
#subject=(subject) ⇒ Object
223 224 225 226 227 |
# File 'lib/postageapp/request.rb', line 223 def subject=(subject) _headers = self.arguments['headers'] ||= { } _headers['subject'] = subject.to_s end |
#text_part ⇒ Object
123 124 125 |
# File 'lib/postageapp/request.rb', line 123 def text_part self.content['text/plain'] end |
#to ⇒ Object
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/postageapp/request.rb', line 185 def to out = self.arguments_to_send.dig('arguments', 'recipients') case (out) when Hash out else [ out ].flatten end end |
#to=(list) ⇒ Object
196 197 198 |
# File 'lib/postageapp/request.rb', line 196 def to=(list) self.arguments['recipients'] = list end |
#url ⇒ Object
URL of the where PostageApp::Request will be directed at
77 78 79 |
# File 'lib/postageapp/request.rb', line 77 def url URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json") end |