Class: Leadersend::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/leadersend/mailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(to: nil, from: nil, fromname: nil, subject: "System", template_path: nil, locals: {}, title: "System", template: "") ⇒ Mail

Returns a new instance of Mail.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/leadersend/mailer.rb', line 3

def initialize to: nil, from: nil, fromname: nil, subject: "System", template_path: nil, locals: {}, title: "System", template: ""
  @api_email_url = Leadersend.config.api_url
  @api_user = Leadersend.config.username
  @api_key = Leadersend.config.api_key
  @to = to
  @subject = subject
  @template_path = template_path
  @locals = locals
  @title = title
  @from = from
  @fromname = fromname
  @template_path = template_path
  @template = template

  if @template_path
    @template = ApplicationController.new.render_to_string(:partial => @template_path, :locals => @locals )
  end
end

Instance Method Details

#call_apiObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/leadersend/mailer.rb', line 35

def call_api
  subject = @subject
  fromname = @fromname
  options = {method: "messagesSend", apikey: @api_key, to: {email: @to}, subject: @subject, html: @template, from: {name: @fromname, email: @from}, auto_plain: true}
  resp = post_api @api_email_url, options
  json = JSON.parse(resp)
  puts json
  return json
rescue => e
  return [{"status" => "error", "description" => "<#{e.class.name}>: #{e.message}"}]
end

#sendObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/leadersend/mailer.rb', line 22

def send
  result = call_api
  status = (result[0] && result[0]["status"]) ? result[0]["status"] : "error"
  return {
    title: @title,
    body: @template,
    status: status,
    subject: @subject,
    to_address: @to,
    response: result.inspect
  }
end