Sha256: 24b82835f63ce3c6bd76c0e39ee87222893ebab0332b27860c1ef5a347941db2

Contents?: true

Size: 1.1 KB

Versions: 17

Compression:

Stored size: 1.1 KB

Contents

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

module Mailkick
  class Service
    class Mailchimp < Mailkick::Service
      def initialize(options = {})
        @gibbon = ::Gibbon::Request.new(api_key: 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(@list_id).members.retrieve(params: {status: "unsubscribed"}).body["members"], "unsubscribe")
      end

      def spam_reports
        fetch(@gibbon.lists(@list_id).abuse_reports.retrieve.body["abuse_reports"], "spam")
      end

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

      protected

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mailkick-1.3.1 lib/mailkick/service/mailchimp.rb
mailkick-1.3.0 lib/mailkick/service/mailchimp.rb
mailkick-1.2.2 lib/mailkick/service/mailchimp.rb
mailkick-1.2.1 lib/mailkick/service/mailchimp.rb
mailkick-1.2.0 lib/mailkick/service/mailchimp.rb
mailkick-1.1.2 lib/mailkick/service/mailchimp.rb
mailkick-1.1.1 lib/mailkick/service/mailchimp.rb
mailkick-1.1.0 lib/mailkick/service/mailchimp.rb
mailkick-1.0.1 lib/mailkick/service/mailchimp.rb
mailkick-1.0.0 lib/mailkick/service/mailchimp.rb
mailkick-0.4.3 lib/mailkick/service/mailchimp.rb
mailkick-0.4.2 lib/mailkick/service/mailchimp.rb
mailkick-0.4.1 lib/mailkick/service/mailchimp.rb
mailkick-0.4.0 lib/mailkick/service/mailchimp.rb
mailkick-0.3.1 lib/mailkick/service/mailchimp.rb
mailkick-0.3.0 lib/mailkick/service/mailchimp.rb
mailkick-0.2.1 lib/mailkick/service/mailchimp.rb