Sha256: 21c183d4c6b282c53021d869054c67aa5f5d49d9814ef64fd2bea2b78126d270

Contents?: true

Size: 1.15 KB

Versions: 12

Compression:

Stored size: 1.15 KB

Contents

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

module Mailkick
  class Service
    class SendGridV2 < Mailkick::Service
      def initialize(options = {})
        @api_key = options[:api_key] || ENV["SENDGRID_API_KEY"]
      end

      def opt_outs
        unsubscribes + spam_reports + bounces
      end

      def unsubscribes
        fetch(client.suppression.unsubscribes, "unsubscribe")
      end

      def spam_reports
        fetch(client.suppression.spam_reports, "spam")
      end

      def bounces
        fetch(client.suppression.bounces, "bounce")
      end

      def self.discoverable?
        !!(defined?(::SendGrid::API) && ENV["SENDGRID_API_KEY"])
      end

      protected

      def client
        @client ||= ::SendGrid::API.new(api_key: @api_key).client
      end

      def fetch(query, reason)
        # TODO paginate
        response = query.get

        raise "Bad status code: #{response.status_code}" if response.status_code.to_i != 200

        response.parsed_body.map do |record|
          {
            email: record[:email],
            time: Time.at(record[:created]),
            reason: reason
          }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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