Sha256: 3ccc68fa5bb3235026763d545512b68de26ed8d924b16095e40df935060e732d

Contents?: true

Size: 1.06 KB

Versions: 11

Compression:

Stored size: 1.06 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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mailkick-0.4.1 lib/mailkick/service/sendgrid.rb
mailkick-0.4.0 lib/mailkick/service/sendgrid.rb
mailkick-0.3.1 lib/mailkick/service/sendgrid.rb
mailkick-0.3.0 lib/mailkick/service/sendgrid.rb
mailkick-0.2.1 lib/mailkick/service/sendgrid.rb
mailkick-0.2.0 lib/mailkick/service/sendgrid.rb
mailkick-0.1.6 lib/mailkick/service/sendgrid.rb
mailkick-0.1.5 lib/mailkick/service/sendgrid.rb
mailkick-0.1.4 lib/mailkick/service/sendgrid.rb
mailkick-0.1.3 lib/mailkick/service/sendgrid.rb
mailkick-0.1.2 lib/mailkick/service/sendgrid.rb