Sha256: ed2c4cd80e646adc9acd7c5dc1a5864ae9c259476281727715ad56b4b02673c9

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module PUNK
  class SendEmailWorker < Worker
    args :from, :to, :subject, :template, :variables, :tags

    def validate
      validates_type String, :from
      validates_type String, :to
      validates_email :to
      validates_includes PUNK.get.mailgun.whitelist, :to if !PUNK.env.test? && !PUNK.get.mailgun.mock && PUNK.get.mailgun.whitelist.present?
      validates_type String, :subject
      validates_type String, :template
      validates_type Hash, :variables, allow_nil: true
      validates_type Array, :tags, allow_nil: true
      validates_length_range 0..1, :tags, allow_nil: true
    end

    def process
      require 'mailgun-ruby'

      client =
        if !PUNK.env.test? && PUNK.get.mailgun.mock
          Mailgun::Client.new(PUNK.get.mailgun.api_key, 'bin.mailgun.net', PUNK.get.mailgun.postbin, ssl=false) # rubocop:disable Lint/UselessAssignment,Layout/SpaceAroundOperators
        else
          Mailgun::Client.new(PUNK.get.mailgun.api_key)
        end

      client.enable_test_mode! if PUNK.env.test?

      message =
        {
          from: from,
          to: to,
          subject: subject,
          template: template
        }
      variables.each { |variable, value| message["v:#{variable}"] = value } if variables.present?
      tags.each { |tag| message["o:tag"] = tag } if tags.present?

      # TODO: store return value in Message table
      # TODO: update Message table with events from mailgun webhooks
      client.send_message(PUNK.get.mailgun.domain, message)

      return unless !PUNK.env.test? && PUNK.get.mailgun.mock

      require 'launchy'
      Launchy.open("http://bin.mailgun.net/#{PUNK.get.mailgun.postbin}")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
punk-0.3.6 lib/punk/workers/send_email_worker.rb
punk-0.3.5 lib/punk/workers/send_email_worker.rb
punk-0.3.4 lib/punk/workers/send_email_worker.rb
punk-0.3.3 lib/punk/workers/send_email_worker.rb
punk-0.3.2 lib/punk/workers/send_email_worker.rb
punk-0.3.1 lib/punk/workers/send_email_worker.rb
punk-0.2.0 lib/punk/workers/send_email_worker.rb