Class: Mailtrap::Mail::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mailtrap/mail/base.rb

Direct Known Subclasses

FromTemplate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from: nil, to: [], cc: [], bcc: [], subject: nil, text: nil, html: nil, attachments: [], headers: {}, custom_variables: {}, category: nil) ⇒ Base

rubocop:disable Metrics/ParameterLists, Metrics/MethodLength



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mailtrap/mail/base.rb', line 11

def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
  from: nil,
  to: [],
  cc: [],
  bcc: [],
  subject: nil,
  text: nil,
  html: nil,
  attachments: [],
  headers: {},
  custom_variables: {},
  category: nil
)
  @from = from
  @to = to
  @cc = cc
  @bcc = bcc
  @subject = subject
  @text = text
  @html = html
  self.attachments = attachments
  @headers = headers
  @custom_variables = custom_variables
  @category = category
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



9
10
11
# File 'lib/mailtrap/mail/base.rb', line 9

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def bcc
  @bcc
end

#categoryObject

Returns the value of attribute category.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def category
  @category
end

#ccObject

Returns the value of attribute cc.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def cc
  @cc
end

#custom_variablesObject

Returns the value of attribute custom_variables.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def custom_variables
  @custom_variables
end

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def from
  @from
end

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def headers
  @headers
end

#htmlObject

Returns the value of attribute html.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def html
  @html
end

#subjectObject

Returns the value of attribute subject.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def subject
  @subject
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def text
  @text
end

#toObject

Returns the value of attribute to.



8
9
10
# File 'lib/mailtrap/mail/base.rb', line 8

def to
  @to
end

Instance Method Details

#add_attachment(content:, filename:, type: nil, disposition: nil, content_id: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mailtrap/mail/base.rb', line 65

def add_attachment(content:, filename:, type: nil, disposition: nil, content_id: nil)
  attachment = Mailtrap::Attachment.new(
    content: content,
    filename: filename,
    type: type,
    disposition: disposition,
    content_id: content_id
  )
  attachments << attachment

  attachment
end

#as_jsonObject

rubocop:disable Metrics/MethodLength



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mailtrap/mail/base.rb', line 37

def as_json # rubocop:disable Metrics/MethodLength
  {
    'from' => from,
    'to' => to,
    'cc' => cc,
    'bcc' => bcc,
    'subject' => subject,
    'text' => text,
    'html' => html,
    'attachments' => attachments.map(&:as_json),
    # TODO: update headers and custom_variables with as_json method
    'headers' => headers,
    'custom_variables' => custom_variables,
    'category' => category
  }.compact
end

#to_json(*args) ⇒ Object



54
55
56
57
58
59
# File 'lib/mailtrap/mail/base.rb', line 54

def to_json(*args)
  JSON.generate(
    as_json,
    *args
  )
end