Sha256: 519f1b5917618c0c32a732da247129d93333af88d2944090b933b8b21bda0cdb

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

# https://github.com/mailgun/mailgun-ruby

module Mailkick
  class Service
    class Mailgun < Mailkick::Service
      def initalize(options = {})
        require 'mailgun'
        mailgun_client = ::Mailgun::Client.new(options[:api_key] || ENV["MAILGUN_API_KEY"])
        domain = options[:domain] || ActionMailer::Base.default_url_options[:host]
        @mailgun_events = ::Mailgun::Events.new(mailgun_client, domain)
      end

      def opt_outs
        unsubscribes + spam_reports + bounces
      end

      def unsubscribes
        fetch(@mailgun_events.get({event: 'unsubscribed'}), 'unsubscribe')
      end

      def spam_reports
        fetch(@mailgun_events.get({event: 'complained'}), 'spam')
      end

      def bounces
        fetch(@mailgun_events.get({event: 'failed'}), 'bounce')
      end

      def self.discoverable?
        !!(defined?(::Mailgun) && ENV["MAILGUN_API_KEY"])
      end

      protected

      def fetch(response, reason)
        response.to_h['items'].map do |record|
          {
            email: record['recipient'],
            time: ActiveSupport::TimeZone['UTC'].at(record['timestamp']),
            reason: reason
          }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mailkick-0.1.3 lib/mailkick/service/mailgun.rb
mailkick-0.1.2 lib/mailkick/service/mailgun.rb