Sha256: a575a0f139c0bf195a369f1e9abc1074c63d835dd7303b295132ab38df1c722f

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

module Workarea
  module MailChimp
    class Gateway::Members < Gateway
      def initialize( default_list_id)
        @default_list_id = default_list_id
      end

      # Returns Hash or nil
      #
      def details(email)
        response ||= begin
          MailChimp.request.lists(@default_list_id).members(digest_email(email)).retrieve
          rescue ::Gibbon::MailChimpError => e
            response_error_handler(e)
          end

        extract_member_details(response)
      end

      def unsubscribe(email, options = {})
        begin
          MailChimp.request.lists(@default_list_id).members(digest_email(email)).update(body: { status: "unsubscribed" })
        rescue ::Gibbon::MailChimpError => e
          response_error_handler(e)
        end
      end

      def subscribe(subscribe_argument)
        return unless subscribe_argument[:email_address]
        begin
          MailChimp.request.lists(@default_list_id).members(digest_email(subscribe_argument[:email_address])).upsert(body: subscribe_argument.merge("status" => "subscribed"))
        rescue ::Gibbon::MailChimpError => e
          response_error_handler(e)
        end
      end

      private

        # Returns
        # * Hash of details for subscribed emails
        # * nil if email does not exist
        # * nil if email is unsubscribed
        #
        def extract_member_details(details)
          if details["error"].present?
            nil
          elsif details["status"] == "unsubscribed"
            nil
          else
            details
          end
        end

        def digest_email(email)
          md5 = Digest::MD5.new
          md5.update(email.downcase)
          md5.hexdigest
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-mail_chimp-3.0.3 lib/integrations/workarea/mail_chimp/gateway/members.rb
workarea-mail_chimp-3.0.2 lib/integrations/workarea/mail_chimp/gateway/members.rb