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: [], reply_to: nil, cc: [], bcc: [], subject: nil, text: nil, html: nil, attachments: [], headers: {}, custom_variables: {}, category: nil, template_uuid: nil, template_variables: {}) ⇒ Base

rubocop:disable Metrics/ParameterLists



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

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

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



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

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

#reply_toObject

Returns the value of attribute reply_to.



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

def reply_to
  @reply_to
end

#subjectObject

Returns the value of attribute subject.



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

def subject
  @subject
end

#template_uuidObject

Returns the value of attribute template_uuid.



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

def template_uuid
  @template_uuid
end

#template_variablesObject

Returns the value of attribute template_variables.



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

def template_variables
  @template_variables
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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mailtrap/mail/base.rb', line 75

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

  attachment
end

#as_jsonObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mailtrap/mail/base.rb', line 44

def as_json
  {
    'from' => from,
    'to' => to,
    'reply_to' => reply_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,
    'template_uuid' => template_uuid,
    'template_variables' => template_variables
  }.transform_values { |value| presence value }.compact
end

#presence(value) ⇒ Object



88
89
90
# File 'lib/mailtrap/mail/base.rb', line 88

def presence(value)
  value.respond_to?(:empty?) && value.empty? ? nil : value
end

#to_json(*args) ⇒ Object



64
65
66
67
68
69
# File 'lib/mailtrap/mail/base.rb', line 64

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