Sha256: 3fbf23566c307efd9901ea4ee6501c124621fb333a978e731898a782da8f1bcd

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

# https://github.com/amro/gibbon

module Mailkick
  class Service
    class Mailchimp < Mailkick::Service
      def initialize(options = {})
        @gibbon = ::Gibbon::API.new(options[:api_key] || ENV["MAILCHIMP_API_KEY"])
        @list_id = options[:list_id] || ENV["MAILCHIMP_LIST_ID"]
      end

      # TODO paginate
      def opt_outs
        unsubscribes + spam_reports
      end

      def unsubscribes
        fetch(@gibbon.lists.members(id: @list_id, status: "unsubscribed"), "unsubscribe")
      end

      def spam_reports
        fetch(@gibbon.lists.abuse_reports(id: @list_id), "spam")
      end

      def self.discoverable?
        !!(defined?(::Gibbon) && ENV["MAILCHIMP_API_KEY"] && ENV["MAILCHIMP_LIST_ID"])
      end

      protected

      def fetch(response, reason)
        response["data"].map do |record|
          {
            email: record["email"],
            time: ActiveSupport::TimeZone["UTC"].parse(record["timestamp_opt"] || record["date"]),
            reason: reason
          }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mailkick-0.2.0 lib/mailkick/service/mailchimp.rb
mailkick-0.1.6 lib/mailkick/service/mailchimp.rb
mailkick-0.1.5 lib/mailkick/service/mailchimp.rb
mailkick-0.1.4 lib/mailkick/service/mailchimp.rb
mailkick-0.1.3 lib/mailkick/service/mailchimp.rb
mailkick-0.1.2 lib/mailkick/service/mailchimp.rb