class Mailgarage::DeliveryMethod

Public Class Methods

new(options = {}) click to toggle source
# File lib/mailgarage/delivery_method.rb, line 5
def initialize(options = {})
  @settings = options
  @api_key = Mailgarage.configuration.api_key
end

Public Instance Methods

deliver!(mail) click to toggle source
# File lib/mailgarage/delivery_method.rb, line 10
def deliver!(mail)
  @mail = mail

  case Rails.env
  when 'development'
    deliver_in_development
  when 'production'
    deliver_in_production
  when 'test'
    deliver_in_test
  else # any arbitrary env like staging
    deliver_in_production
  end
end
deliver_in_development() click to toggle source
# File lib/mailgarage/delivery_method.rb, line 41
def deliver_in_development
  LetterOpener::DeliveryMethod.new(
    message_template: 'light',
    location: Rails.root.join('tmp', 'mailgarage')
  )
  .deliver!(@mail)
end
deliver_in_production() click to toggle source
# File lib/mailgarage/delivery_method.rb, line 25
def deliver_in_production
  raise(Mailgarage::Error, "We can't send emails in #{Rails.env} without api_key set.") unless @api_key

  uri = URI.parse('https://mailgarage.rocks')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new('/emails')
  request.body = { email: @mail.to_s, environment: Rails.env }.to_json
  request['Content-Type'] = 'application/json'
  request['Api-Key'] = @api_key
  http.request(request)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  raise(Mailgarage::Error, "Error from Net::HTTP: #{e.message}")
end
deliver_in_test() click to toggle source
# File lib/mailgarage/delivery_method.rb, line 49
def deliver_in_test
  raise(Mailgarage::Error, 'You should set config.mail_delivery to :test in your environment config so you can test your email delivery.')
end