Sha256: e76f515938287da71ac2797aa0cf0dc359e497f369a3665ff5477cc35d63ff67

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

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

module Mailkick
  class Service
    class Mailgun < Mailkick::Service
      def initialize(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

5 entries across 5 versions & 1 rubygems

Version Path
mailkick-0.2.1 lib/mailkick/service/mailgun.rb
mailkick-0.2.0 lib/mailkick/service/mailgun.rb
mailkick-0.1.6 lib/mailkick/service/mailgun.rb
mailkick-0.1.5 lib/mailkick/service/mailgun.rb
mailkick-0.1.4 lib/mailkick/service/mailgun.rb