Sha256: baab244e03596b6584994aa051cda190e1b29ba635a670e9a19be935650e9e8b

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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

      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailkick-0.0.1 lib/mailkick/service/sendgrid.rb