Class: PostageApp::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#argumentsObject

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_handlerObject

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

#methodObject

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_deliveriesObject

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_errorsObject

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_sendObject

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

#bodyObject



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

#deliverObject

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

#fromObject



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

#subjectObject



41
42
43
# File 'lib/postageapp/mailer.rb', line 41

def subject
  self.arguments_to_send.dig('arguments', 'headers', 'subject')
end

#toObject



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

#urlObject

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