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_DEFAULT =
{
  'Content-type' => 'application/json',
  'Accept' => 'text/json, application/json'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, arguments = { }) ⇒ 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 = { })
  @method = method
  @arguments = arguments.dup

  @uid = @arguments.delete('uid')
  @api_key = @arguments.delete('api_key') || PostageApp.configuration.api_key
end

Instance Attribute Details

#api_keyObject

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

#argumentsObject

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_handlerObject

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

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



14
15
16
# File 'lib/postageapp/request.rb', line 14

def method
  @method
end

#perform_deliveriesObject

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_errorsObject

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



10
11
12
# File 'lib/postageapp/request.rb', line 10

def uid
  @uid
end

Class Method Details

.user_agentObject

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/postageapp/mailer.rb', line 44

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_sendObject

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

#bccObject



81
82
83
84
# File 'lib/postageapp/mailer.rb', line 81

def bcc
  # Not supported natively via API at this time
  [ ]
end

#bcc=(list) ⇒ Object



86
87
88
# File 'lib/postageapp/mailer.rb', line 86

def bcc=(list)
  # Not supported natively via API at this time
end

#bodyObject

Emulation of Mail::Message interface



114
115
116
117
# File 'lib/postageapp/request.rb', line 114

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



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/postageapp/mailer/mailer_3.rb', line 174

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 = 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

#fromObject



71
72
73
# File 'lib/postageapp/mailer.rb', line 71

def from
  [ self.arguments_to_send.dig('arguments', 'headers', 'from') ].flatten
end

#from=(address) ⇒ Object



75
76
77
78
79
# File 'lib/postageapp/mailer.rb', line 75

def from=(address)
  _headers = self.arguments['headers'] ||= { }

  _headers['from'] = address.to_s
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'


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/postageapp/mailer.rb', line 31

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_partObject

Emulates Mail::Message#html_part



119
120
121
# File 'lib/postageapp/request.rb', line 119

def html_part
  self.arguments and self.arguments['content'] and self.arguments['content']['text/html']
end

#inform_interceptorsObject



168
169
170
# File 'lib/postageapp/mailer/mailer_3.rb', line 168

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



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

#subjectObject



90
91
92
# File 'lib/postageapp/mailer.rb', line 90

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

#subject=(subject) ⇒ Object



94
95
96
97
98
# File 'lib/postageapp/mailer.rb', line 94

def subject=(subject)
  _headers = self.arguments['headers'] ||= { }

  _headers['subject'] = subject.to_s
end

#text_partObject

Emulates Mail::Message#text_part



124
125
126
# File 'lib/postageapp/request.rb', line 124

def text_part
  self.arguments and self.arguments['content'] and self.arguments['content']['text/plain']
end

#toObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/postageapp/mailer.rb', line 56

def to
  out = self.arguments_to_send.dig('arguments', 'recipients')

  case (out)
  when Hash
    out
  else
    [ out ].flatten
  end
end

#to=(list) ⇒ Object



67
68
69
# File 'lib/postageapp/mailer.rb', line 67

def to=(list)
  self.arguments['recipients'] = list
end

#urlObject

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