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 =
'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 = nil) ⇒ 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 = 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



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



82
83
84
85
86
# File 'lib/postageapp/request.rb', line 82

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.



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



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

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

#attachmentsObject



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

def attachments
  self.arguments['attachments']
end

#bccObject



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

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

#bcc=(list) ⇒ Object



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

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

#bodyObject

_content and (_content or _content) end



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

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

#ccObject



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

def cc
  self.header['cc']
end

#contentObject



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

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



127
128
129
# File 'lib/postageapp/request.rb', line 127

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

#fromObject



200
201
202
# File 'lib/postageapp/request.rb', line 200

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

#from=(address) ⇒ Object



204
205
206
207
208
# File 'lib/postageapp/request.rb', line 204

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

  _headers['from'] = address.to_s
end

#headerObject



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

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'


160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/postageapp/request.rb', line 160

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



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

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



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

def mime_type
  self.content.keys.first
end

#multipart?Boolean

Returns:

  • (Boolean)


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

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

#reply_toObject



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

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



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



219
220
221
# File 'lib/postageapp/request.rb', line 219

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

#subject=(subject) ⇒ Object



223
224
225
226
227
# File 'lib/postageapp/request.rb', line 223

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

  _headers['subject'] = subject.to_s
end

#text_partObject



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

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

#toObject



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

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

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

#to=(list) ⇒ Object



196
197
198
# File 'lib/postageapp/request.rb', line 196

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