Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.

Commit 2c7379c

Browse files
committed
Minor cleanups
1 parent 30c8b8d commit 2c7379c

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

Diff for: emails.coffee

+27-23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
TAG = 'mailer'
99

10+
CONTENT_TYPES = {
11+
html: 'text/html',
12+
text: 'text/plain'
13+
}
14+
1015
# ## Setup
1116

1217
# Main exported symbol with some initial settings:
@@ -269,13 +274,12 @@ MailerClass = (options) ->
269274
# This function adds the `preview` route from a `template` object.
270275
# It will apply the returned data from a `data` function on the
271276
# provided `route` prop from the template.
272-
previewAction = (opts, template, params) ->
273-
check opts,
274-
type: Match.OneOf('html', 'text')
277+
previewAction = (type) ->
278+
check type, Match.OneOf('html', 'text')
275279

276-
return (template, params) ->
280+
return (req, res, params, template) ->
277281
try
278-
data = template.route.data and template.route.data.call(this, params)
282+
data = template.route.data and template.route.data.call(res, params)
279283
catch ex
280284
msg = ''
281285
exception = 'Exception in '+template.name+' data function: '+ex.message
@@ -291,29 +295,29 @@ MailerClass = (options) ->
291295
msg = exception
292296

293297
Utils.Logger.error(msg, TAG)
294-
this.writeHead(500)
295-
return this.end(msg)
298+
res.writeHead(500)
299+
res.end(msg)
296300

297301
# Compile, since we wanna refresh markup and CSS inlining.
298302
compile template
299303

300-
Utils.Logger.info "Rendering #{template.name} as #{opts.type} ...", TAG
304+
Utils.Logger.info "Rendering #{template.name} as #{type} ...", TAG
301305

302306
try
303307
html = render template.name, data
304-
content = if opts.type is 'html' then html else toText(html)
308+
content = if type is 'html' then html else toText(html)
305309
Utils.Logger.info "Rendering successful!", TAG
306310
catch ex
307311
msg = 'Could not preview email: ' + ex.message
308312
Utils.Logger.error msg, TAG
309313
content = msg
310314

311-
@writeHead 200, 'Content-Type': if opts.type is 'html' then 'text/html' else 'text/plain'
312-
@end(content, 'utf8')
315+
res.writeHead 200, 'Content-Type': CONTENT_TYPES[type]
316+
res.end(content, 'utf8')
313317

314318
# This function adds the `send` route, for easy sending emails from
315319
# the browser.
316-
sendAction = (template, params) ->
320+
sendAction = (req, res, params, template) ->
317321
# Who to send to? It depends: it primarly reads from the `?to`
318322
# query param, and secondly from the `testEmail` prop in settings.
319323
to = params.query.to or settings.testEmail
@@ -322,42 +326,42 @@ MailerClass = (options) ->
322326

323327
if to?
324328
try
325-
data = template.route.data and template.route.data.call(this, params)
329+
data = template.route.data and template.route.data.call(res, params)
326330
catch ex
327331
Utils.Logger.error 'Exception in '+template.name+' data function: '+ex.message, TAG
328332
return
329333

330-
res = sendEmail(
334+
result = sendEmail(
331335
to: to
332336
data: data
333337
template: template.name
334338
subject: '[TEST] ' + template.name
335339
)
336340

337-
if res is false
338-
@response.writeHead 500
341+
if result is false
342+
res.writeHead 500
339343
msg = 'Did not send test email, something went wrong. Check the logs.'
340344
else
341-
@writeHead 200
345+
res.writeHead 200
342346
# If there's no `MAIL_URL` environment variable, Meteor cannot send
343347
# the email and echoes it out to `STDOUT` instead.
344348
reallySentEmail = !!process.env.MAIL_URL
345349
msg = if reallySentEmail then "Sent test email to #{to}" else "Sent email to STDOUT"
346350

347-
@end(msg)
351+
res.end(msg)
348352

349353
else
350-
@writeHead 400
351-
@end("No testEmail provided.")
354+
res.writeHead 400
355+
res.end('No testEmail provided.')
352356

353357
# Adds all the routes from a template.
354358
addRoutes = (template) ->
355359
check template.name, String
356360
check template.route.path, String
357361

358362
types =
359-
preview: previewAction(type: 'html')
360-
text: previewAction(type: 'text')
363+
preview: previewAction('html')
364+
text: previewAction('text')
361365
send: sendAction
362366

363367
_.each types, (action, type) ->
@@ -378,7 +382,7 @@ MailerClass = (options) ->
378382

379383
# we use Picker for server side routes
380384
Picker.route(path, (params, req, res) ->
381-
action.call res, template, params
385+
action(req, res, params, template)
382386
)
383387

384388

0 commit comments

Comments
 (0)