Class: Hubspot::Mailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/hubspot/mailer.rb,
lib/hubspot/mailer/message.rb,
lib/hubspot/mailer/delivery.rb,
lib/hubspot/mailer/exceptions.rb,
lib/hubspot/mailer/hubspot_preview_interceptor.rb

Defined Under Namespace

Classes: Delivery, DeliveryError, HubspotPreviewInterceptor, InvalidTemplateError, Message, MissingRecipientError, MissingTemplateError, RecipientAddressError, RequestError, SenderAddressError, SendingError, UnknownResponseError

Constant Summary collapse

SINGLE_SEND_PATH =
'/email/public/v1/singleEmail/send'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailer

Returns a new instance of Mailer.



127
128
129
130
131
# File 'lib/hubspot/mailer.rb', line 127

def initialize
  super()
  @_mail_was_called = false
  @_message         = Message.new
end

Class Method Details

.deliver_mail(mail) ⇒ Object

Wraps an email delivery inside of ActiveSupport::Notifications instrumentation.

This method is actually called by the Mail::Message object itself through a callback when you call :deliver on the Mail::Message, calling deliver_mail directly and passing a Mail::Message will do nothing except tell the logger you sent the email.



27
28
29
30
31
32
# File 'lib/hubspot/mailer.rb', line 27

def deliver_mail(mail) #:nodoc:
  ActiveSupport::Notifications.instrument("deliver.hubspot_mailer") do |payload|
    set_payload_for_mail(payload, mail)
    yield # Let Mail do the delivery actions
  end
end

.single_send(mail) ⇒ Object

Perform the delivery by calling Hubspot’s API

Receives regular mail object that contains additional template details.



37
38
39
40
41
42
43
44
45
46
# File 'lib/hubspot/mailer.rb', line 37

def single_send(mail)
  # Format the request data
  data = single_send_params(mail)

  # Call the API
  response = Hubspot::Connection.post_json(SINGLE_SEND_PATH, params: {}, body: data)

  # Parse response and either raise or return event details
  parse_response(response)
end

Instance Method Details

#mail(headers = {}, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hubspot/mailer.rb', line 146

def mail(headers = {}, &block)
  return message if @_mail_was_called && headers.blank? && !block

  headers = apply_defaults(headers)

  # Set configure delivery behavior
  wrap_delivery_behavior!(headers[:delivery_method], headers[:delivery_method_options])

  # Hubspot-specific attributes, e.g. emailId (template ID)
  assign_attributes_to_message(message, headers)

  # Default headers
  assign_headers_to_message(message, headers)

  @_mail_was_called = true

  message
end

#process(method_name, *args) ⇒ Object

:nodoc:



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hubspot/mailer.rb', line 133

def process(method_name, *args) #:nodoc:
  payload = {
    mailer: self.class.name,
    action: method_name,
    args:   args
  }

  ActiveSupport::Notifications.instrument("process.hubspot_mailer", payload) do
    super
    @_message = ActionMailer::Base::NullMail.new unless @_mail_was_called
  end
end