Class: PostageApp::Request
- Inherits:
-
Object
- Object
- PostageApp::Request
- Defined in:
- lib/postageapp/mailer.rb,
lib/postageapp/request.rb,
lib/postageapp/mailer/mailer_3.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 =
{ 'Content-type' => 'application/json', 'Accept' => 'text/json, application/json' }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Ability to set api_key with eash 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.
Instance Method Summary collapse
-
#arguments_to_send ⇒ Object
Arguments need to be appended with some some stuff before it’s ready to be send out.
- #body ⇒ Object
-
#deliver ⇒ Object
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 = {}) ⇒ Object
Not 100% on this, but I need to assign this so I can properly handle deliver method.
- #from ⇒ Object
-
#headers(value = nil) ⇒ Object
Getter and setter for headers.
-
#html_part ⇒ Object
Emulates Mail::Message#html_part.
- #inform_interceptors ⇒ Object
-
#initialize(method, arguments = {}) ⇒ Request
constructor
A new instance of Request.
-
#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
-
#text_part ⇒ Object
Emulates Mail::Message#text_part.
- #to ⇒ Object
-
#url ⇒ Object
URL of the where PostageApp::Request will be directed at.
Constructor Details
#initialize(method, arguments = {}) ⇒ Request
Returns a new instance of Request.
22 23 24 25 26 27 |
# File 'lib/postageapp/request.rb', line 22 def initialize(method, arguments = {}) @method = method @uid = arguments.delete('uid') @api_key = arguments.delete('api_key') || PostageApp.configuration.api_key @arguments = arguments end |
Instance Attribute Details
#api_key ⇒ Object
Ability to set api_key with eash 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.
147 148 149 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 147 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.
147 148 149 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 147 def perform_deliveries @perform_deliveries end |
#raise_delivery_errors ⇒ Object
Returns the value of attribute raise_delivery_errors.
147 148 149 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 147 def raise_delivery_errors @raise_delivery_errors end |
#uid(reload = false) ⇒ Object
Unique ID of the request
10 11 12 |
# File 'lib/postageapp/request.rb', line 10 def uid @uid end |
Instance Method Details
#arguments_to_send ⇒ Object
Arguments need to be appended with some some stuff before it’s ready to be send out
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/postageapp/request.rb', line 92 def arguments_to_send hash = { 'uid' => self.uid, 'api_key' => self.api_key } if !self.arguments.nil? && !self.arguments.empty? if !PostageApp.configuration.recipient_override.nil? && self.method.to_sym == :send_message self.arguments.merge!('recipient_override' => PostageApp.configuration.recipient_override) end hash.merge!('arguments' => self.arguments.recursive_stringify_keys!) end hash end |
#body ⇒ Object
47 48 49 50 |
# File 'lib/postageapp/mailer.rb', line 47 def body out = self.arguments_to_send.dig('arguments', 'content') out.is_a?(Hash) ? out.values.join("\n\n") : out.to_s end |
#deliver ⇒ Object
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
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 157 def deliver 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 = {}) ⇒ Object
Not 100% on this, but I need to assign this so I can properly handle deliver method
169 170 171 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 169 def delivery_method(method = nil, settings = {}) @delivery_method = method end |
#from ⇒ Object
39 40 41 |
# File 'lib/postageapp/mailer.rb', line 39 def from [self.arguments_to_send.dig('arguments', 'headers', 'from')].flatten 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'
24 25 26 27 28 29 30 31 32 |
# File 'lib/postageapp/mailer.rb', line 24 def headers(value = nil) self.arguments['headers'] ||= { } if value && value.is_a?(Hash) value.each do |k, v| self.arguments['headers'][k.to_s] = v.to_s end end self.arguments['headers'] end |
#html_part ⇒ Object
Emulates Mail::Message#html_part
71 72 73 |
# File 'lib/postageapp/request.rb', line 71 def html_part self.arguments && self.arguments['content'] && self.arguments['content']['text/html'] end |
#inform_interceptors ⇒ Object
151 152 153 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 151 def inform_interceptors Mail.inform_interceptors(self) 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
31 32 33 34 35 36 37 38 39 40 41 42 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 |
# File 'lib/postageapp/request.rb', line 31 def send(skip_failed_requests_processing = false) http = Net::HTTP::Proxy( PostageApp.configuration.proxy_host, PostageApp.configuration.proxy_port, PostageApp.configuration.proxy_user, PostageApp.configuration.proxy_pass ).new(url.host, url.port) http.read_timeout = PostageApp.configuration.http_read_timeout http.open_timeout = PostageApp.configuration.http_open_timeout http.use_ssl = PostageApp.configuration.secure? PostageApp.logger.info(self) http_response = begin http.post( url.path, self.arguments_to_send.to_json, HEADERS.merge('User-Agent' => "PostageApp-RubyGem #{PostageApp::VERSION} (Ruby #{RUBY_VERSION}, #{PostageApp.configuration.framework})") ) 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
43 44 45 |
# File 'lib/postageapp/mailer.rb', line 43 def subject self.arguments_to_send.dig('arguments', 'headers', 'subject') end |
#text_part ⇒ Object
Emulates Mail::Message#text_part
76 77 78 |
# File 'lib/postageapp/request.rb', line 76 def text_part self.arguments && self.arguments['content'] && self.arguments['content']['text/plain'] end |
#to ⇒ Object
34 35 36 37 |
# File 'lib/postageapp/mailer.rb', line 34 def to out = self.arguments_to_send.dig('arguments', 'recipients') out.is_a?(Hash) ? out : [out].flatten end |
#url ⇒ Object
URL of the where PostageApp::Request will be directed at
81 82 83 |
# File 'lib/postageapp/request.rb', line 81 def url URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json") end |