Class: PostageApp::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/postageapp/mailer/mailer_3.rb,
lib/postageapp/request.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 =

Constants ============================================================

'1.0'
HEADERS_DEFAULT =
{
  'Content-type' => 'application/json',
  'Accept' => 'text/json, application/json'
}
TimeoutError =
defined?(::Timeout) ? ::Timeout::Error : ::TimeoutError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, arguments = nil) ⇒ Request

Creates a new Request with the given API call method and arguments.



43
44
45
46
47
48
49
# File 'lib/postageapp/request.rb', line 43

def initialize(method, arguments = nil)
  @method = method
  @arguments = 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



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

def api_key
  @api_key
end

#argumentsObject

A list of arguments in a Hash format passed along with the request



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

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)



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

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



93
94
95
96
97
# File 'lib/postageapp/request.rb', line 93

def uid(reload = false)
  @uid = nil if (reload)

  @uid ||= Digest::SHA1.hexdigest("#{rand}#{Time.now.to_f}#{self.arguments}")
end

Class Method Details

.user_agentObject

Returns a user-agent string used for identification when making API calls.



31
32
33
34
35
36
37
38
# File 'lib/postageapp/request.rb', line 31

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



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/postageapp/request.rb', line 184

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/postageapp/request.rb', line 100

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

#attachmentsObject



158
159
160
# File 'lib/postageapp/request.rb', line 158

def attachments
  self.arguments['attachments']
end

#bccObject



221
222
223
224
# File 'lib/postageapp/request.rb', line 221

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

#bcc=(list) ⇒ Object



226
227
228
# File 'lib/postageapp/request.rb', line 226

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

#bodyObject

_content and (_content or _content) end



247
248
249
250
# File 'lib/postageapp/request.rb', line 247

def body
  out = self.arguments_to_send.dig('arguments', 'content')
  out.is_a?(Hash) ? out.values.join("\n\n") : out.to_s
end

#ccObject



154
155
156
# File 'lib/postageapp/request.rb', line 154

def cc
  self.header['cc']
end

#contentObject



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

def content
  self.arguments['content'] ||= { }
end

#deliver_nowObject Also known as: deliver

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



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/postageapp/mailer/mailer_4.rb', line 198

def deliver_now
  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

#find_first_mime_type(type) ⇒ Object



138
139
140
# File 'lib/postageapp/request.rb', line 138

def find_first_mime_type(type)
  self.content[type]
end

#fromObject



211
212
213
# File 'lib/postageapp/request.rb', line 211

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

#from=(address) ⇒ Object



215
216
217
218
219
# File 'lib/postageapp/request.rb', line 215

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

  _headers['from'] = address.to_s
end

#headerObject



146
147
148
# File 'lib/postageapp/request.rb', line 146

def header
  self.arguments['headers'] ||= { }
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'


171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/postageapp/request.rb', line 171

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

– Mail::Message Emulation ———————————————-



130
131
132
# File 'lib/postageapp/request.rb', line 130

def html_part
  self.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

#mime_typeObject



142
143
144
# File 'lib/postageapp/request.rb', line 142

def mime_type
  self.content.keys.first
end

#multipart?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/postageapp/request.rb', line 162

def multipart?
  self.content.keys.length > 1
end

#reply_toObject



150
151
152
# File 'lib/postageapp/request.rb', line 150

def reply_to
  self.header['reply-to']
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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/postageapp/request.rb', line 53

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



230
231
232
# File 'lib/postageapp/request.rb', line 230

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

#subject=(subject) ⇒ Object



234
235
236
237
238
# File 'lib/postageapp/request.rb', line 234

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

  _headers['subject'] = subject.to_s
end

#text_partObject



134
135
136
# File 'lib/postageapp/request.rb', line 134

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

#toObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/postageapp/request.rb', line 196

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

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

#to=(list) ⇒ Object



207
208
209
# File 'lib/postageapp/request.rb', line 207

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

#urlObject

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



88
89
90
# File 'lib/postageapp/request.rb', line 88

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