Class: PostageApp::Request
- Inherits:
-
Object
- Object
- PostageApp::Request
- Defined in:
- lib/postageapp/mailer.rb,
lib/postageapp/request.rb,
lib/postageapp/mailer/mailer_3.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
-
#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.
-
#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
- #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.
20 21 22 23 24 |
# File 'lib/postageapp/request.rb', line 20 def initialize(method, arguments = {}) @method = method @uid = arguments.delete(:uid) @arguments = arguments end |
Instance Attribute Details
#arguments ⇒ Object
A list of arguments in a Hash format passed along with the request
18 19 20 |
# File 'lib/postageapp/request.rb', line 18 def arguments @arguments end |
#delivery_handler ⇒ Object
Returns the value of attribute delivery_handler.
173 174 175 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 173 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)
15 16 17 |
# File 'lib/postageapp/request.rb', line 15 def method @method end |
#perform_deliveries ⇒ Object
Returns the value of attribute perform_deliveries.
173 174 175 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 173 def perform_deliveries @perform_deliveries end |
#raise_delivery_errors ⇒ Object
Returns the value of attribute raise_delivery_errors.
173 174 175 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 173 def raise_delivery_errors @raise_delivery_errors end |
#uid(reload = false) ⇒ Object
Unique ID of the request
11 12 13 |
# File 'lib/postageapp/request.rb', line 11 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/postageapp/request.rb', line 79 def arguments_to_send hash = { 'uid' => self.uid, 'api_key' => PostageApp.configuration.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
45 46 47 48 |
# File 'lib/postageapp/mailer.rb', line 45 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
179 180 181 182 183 184 185 186 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 179 def deliver if @delivery_method == Mail::TestMailer mailer = @delivery_method.new(nil) mailer.deliver!(self) else self.send 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
189 190 191 |
# File 'lib/postageapp/mailer/mailer_3.rb', line 189 def delivery_method(method = nil, settings = {}) @delivery_method = method end |
#from ⇒ Object
37 38 39 |
# File 'lib/postageapp/mailer.rb', line 37 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'
22 23 24 25 26 27 28 29 30 |
# File 'lib/postageapp/mailer.rb', line 22 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 |
#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
28 29 30 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 |
# File 'lib/postageapp/request.rb', line 28 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
41 42 43 |
# File 'lib/postageapp/mailer.rb', line 41 def subject self.arguments_to_send.dig('arguments', 'headers', 'subject') end |
#to ⇒ Object
32 33 34 35 |
# File 'lib/postageapp/mailer.rb', line 32 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
68 69 70 |
# File 'lib/postageapp/request.rb', line 68 def url URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json") end |