Class: MailMaleMail::PostalService::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_male_mail/postal_service/delivery_method.rb

Defined Under Namespace

Classes: InvalidOption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



21
22
23
24
25
26
# File 'lib/mail_male_mail/postal_service/delivery_method.rb', line 21

def initialize(options = {})
  unless options[:url].present? || options[:method].present?
    raise InvalidOption, "A url or method option is required to send email using the Postal Service delivery method"
  end
  self.settings = options
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



20
21
22
# File 'lib/mail_male_mail/postal_service/delivery_method.rb', line 20

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mail_male_mail/postal_service/delivery_method.rb', line 27

def deliver!(mail)
  message = [:to, :cc, :bcc].reduce({}) do |a, e|
    val = mail.public_send(e)
    a[e] = val.join(",") if val.present?

    a
  end

  message[:subject] = mail.subject.to_s
  message[:from] = mail.from.first.to_s
  message[:extra_provider_data] = ActiveSupport::JSON.decode(mail.header['X-PostalService-Data'].to_s) if mail.header['X-PostalService-Data']

  if mail.header['X-PostalService-Provider']
    message[:provider] = mail.header['X-PostalService-Provider'].to_s
  elsif self.settings[:provider]
    message[:provider] = self.settings[:provider]
  end

  message[:category] = mail.header['X-PostalService-Category'].to_s if mail.header['X-PostalService-Category']

  message[:text] = mail.text_part.body.to_s if mail.text_part
  message[:html] = mail.html_part.body.to_s if mail.html_part
  unless mail.text_part || mail.html_part
    part = mail.content_type =~ /html/ ? :html : :text
    message[part] = mail.body.to_s
  end
  MailMaleMail::PostalService.api_request(self.settings, message)
end