Class: Mailtime::MailLogService

Inherits:
Object
  • Object
show all
Defined in:
lib/mailtime/processor/mail_log_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, loggable_class, options) ⇒ MailLogService

Returns a new instance of MailLogService.



5
6
7
8
9
10
11
# File 'lib/mailtime/processor/mail_log_service.rb', line 5

def initialize(mail, loggable_class, options)
  @mail = mail
  @loggable_class = loggable_class
  @loggable = nil
  @options = options
  @fields = []
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



4
5
6
# File 'lib/mailtime/processor/mail_log_service.rb', line 4

def field
  @field
end

#loggableObject

Returns the value of attribute loggable.



4
5
6
# File 'lib/mailtime/processor/mail_log_service.rb', line 4

def loggable
  @loggable
end

Instance Method Details

#attribute_is_loggable?(var) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/mailtime/processor/mail_log_service.rb', line 32

def attribute_is_loggable?(var)
  fields = @options[:fields] || Mailtime.configuration.log_fields || [:to, :cc, :bcc]
  fields.each do |field|
    if @mail.send(field).include?(var.send(@options[:attribute]))
      @fields << field
    end
  end
end

#class_is_loggable?(var) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/mailtime/processor/mail_log_service.rb', line 26

def class_is_loggable?(var)
  if var.class.to_s == @loggable_class
    @loggable = var
  end
end

#create_mail_logObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mailtime/processor/mail_log_service.rb', line 41

def create_mail_log
  @fields.each do |field|
    ::Mailtime::MailLog.create(
        :loggable => @loggable,
        :mailtime_mail_template_id => @template.try(:id),
        :action => @mail.mailer_action,
        :klass => @mail.mailer_klass,
        :headers => extract_headers,
        :scope => extract_scope,
        :field => field
    )
  end
end

#executeObject



13
14
15
# File 'lib/mailtime/processor/mail_log_service.rb', line 13

def execute
  log
end

#extract_headersObject



55
56
57
58
59
# File 'lib/mailtime/processor/mail_log_service.rb', line 55

def extract_headers
  headers = {}
  @mail.header_fields.map { |k| headers[k.name] = k.value }
  headers
end

#extract_scopeObject



61
62
63
# File 'lib/mailtime/processor/mail_log_service.rb', line 61

def extract_scope
  @mail.action_variables.as_json.except(*Mailtime.configuration.reject_scope_keys)
end

#logObject



17
18
19
20
# File 'lib/mailtime/processor/mail_log_service.rb', line 17

def log
  return unless loggable?
  create_mail_log
end

#loggable?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/mailtime/processor/mail_log_service.rb', line 22

def loggable?
  @mail.action_variables.detect { |_, var| class_is_loggable?(var) && attribute_is_loggable?(var) } && @fields.any?
end