Class: PostageApp::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(method, arguments = {}) ⇒ Request

Returns a new instance of Request.



23
24
25
26
27
28
# File 'lib/postageapp/request.rb', line 23

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_keyObject

Ability to set api_key with eash request



21
22
23
# File 'lib/postageapp/request.rb', line 21

def api_key
  @api_key
end

#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.



148
149
150
# File 'lib/postageapp/mailer/mailer_3.rb', line 148

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.



148
149
150
# File 'lib/postageapp/mailer/mailer_3.rb', line 148

def perform_deliveries
  @perform_deliveries
end

#raise_delivery_errorsObject

Returns the value of attribute raise_delivery_errors.



148
149
150
# File 'lib/postageapp/mailer/mailer_3.rb', line 148

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/postageapp/request.rb', line 83

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

#bodyObject



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

#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



154
155
156
157
158
159
160
# File 'lib/postageapp/mailer/mailer_3.rb', line 154

def deliver
  if @delivery_method == Mail::TestMailer
    @delivery_method.deliveries << 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



163
164
165
# File 'lib/postageapp/mailer/mailer_3.rb', line 163

def delivery_method(method = nil, settings = {})
  @delivery_method = method
end

#fromObject



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

#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



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
69
# File 'lib/postageapp/request.rb', line 32

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



43
44
45
# File 'lib/postageapp/mailer.rb', line 43

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

#toObject



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

#urlObject

URL of the where PostageApp::Request will be directed at



72
73
74
# File 'lib/postageapp/request.rb', line 72

def url
  URI.parse("#{PostageApp.configuration.url}/v.#{API_VERSION}/#{self.method}.json")
end