Sha256: 578f2ffb7eb41d68bf47e331ab0f8566c36737bf5e453b6b26c71a223ba079a9

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

# https://github.com/freerobby/sendgrid_toolkit

module Mailkick
  class Service
    class SendGrid < Mailkick::Service
      def initialize(options = {})
        @api_user = options[:api_user] || ENV["SENDGRID_USERNAME"]
        @api_key = options[:api_key] || ENV["SENDGRID_PASSWORD"]
      end

      # TODO paginate
      def opt_outs
        unsubscribes + spam_reports + bounces
      end

      def unsubscribes
        fetch(::SendgridToolkit::Unsubscribes, "unsubscribe")
      end

      def spam_reports
        fetch(::SendgridToolkit::SpamReports, "spam")
      end

      def bounces
        fetch(::SendgridToolkit::Bounces, "bounce")
      end

      def self.discoverable?
        !!(defined?(::SendgridToolkit) && ENV["SENDGRID_USERNAME"] && ENV["SENDGRID_PASSWORD"])
      end

      protected

      def fetch(klass, reason)
        klass.new(@api_user, @api_key).retrieve_with_timestamps.map do |record|
          {
            email: record["email"],
            time: record["created"],
            reason: reason
          }
        end
      end
    end

    # backwards compatibility
    Sendgrid = SendGrid
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mailkick-1.3.1 lib/mailkick/service/sendgrid.rb
mailkick-1.3.0 lib/mailkick/service/sendgrid.rb
mailkick-1.2.2 lib/mailkick/service/sendgrid.rb
mailkick-1.2.1 lib/mailkick/service/sendgrid.rb
mailkick-1.2.0 lib/mailkick/service/sendgrid.rb
mailkick-1.1.2 lib/mailkick/service/sendgrid.rb
mailkick-1.1.1 lib/mailkick/service/sendgrid.rb
mailkick-1.1.0 lib/mailkick/service/sendgrid.rb
mailkick-1.0.1 lib/mailkick/service/sendgrid.rb
mailkick-1.0.0 lib/mailkick/service/sendgrid.rb
mailkick-0.4.3 lib/mailkick/service/sendgrid.rb
mailkick-0.4.2 lib/mailkick/service/sendgrid.rb