<commit_before>require 'gibbon'

module MailchimpService
    class Client
        include Singleton
  
        attr_accessor :api_key, :list_id
        
        def initialize(options = {})
            @api_key = options[:api_key] || Figaro.env.mailchimp_api_key
            @list_id = options[:list_id] || Figaro.env.mailchimp_list_id
            Rails.logger.info("MailchimpService::Client.initialize: #{@api_key}, #{list_id}")
            @gibbon = Gibbon::Request.new(api_key: api_key, debug: true)
        end

        def setOptions(options = {})
            @api_key = options[:api_key] || Figaro.env.mailchimp_api_key
            @list_id = options[:list_id] || Figaro.env.mailchimp_list_id
            Rails.logger.info("MailchimpService::Client.setOptions: #{@api_key}, #{list_id}")
            @gibbon = Gibbon::Request.new(api_key: api_key, debug: true)
        end

        def subscribe(user)
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                @lowercase_email_hash = Digest::MD5.hexdigest(user.email.downcase)
                res = @gibbon.lists(@list_id).members(@lowercase_email_hash).upsert(body: {
                    email_address: user.email,
                    status: "subscribed",
                    merge_fields: {LNAME: user.last_name, FNAME: user.first_name}
                })
                return res
            rescue => e
                logger.error "MailchimpService::Client Failed to subscribe user: #{e.message} - #{e.raw_body}"
            end
            return nil
        end


        def members()
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                return @gibbon.lists(@list_id).members()
            rescue => e
                logger.error "MailchimpService::Client Failed to list members: #{e.message} - #{e.raw_body}"
            end
            return nil
        end

        def unsubscribe(user)
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                @lowercase_email_hash = Digest::MD5.hexdigest(user.email.downcase)
                res = @gibbon.lists(@list_id).members(@lowercase_email_hash).update(body: {
                    status: "unsubscribed",
                })
                return res
            rescue => e
                logger.error "MailchimpService::Client Failed to unsubscribe user: #{e.message} - #{e.raw_body}"
            end
            return nil
        end
    end
end
    <commit_msg>adding loggin for errors in mailchimp<commit_after>require 'gibbon'

module MailchimpService
    class Client
        include Singleton
  
        attr_accessor :api_key, :list_id
        
        def initialize(options = {})
            @api_key = options[:api_key] || Figaro.env.mailchimp_api_key
            @list_id = options[:list_id] || Figaro.env.mailchimp_list_id
            Rails.logger.info("MailchimpService::Client.initialize: #{@api_key}, #{list_id}")
            @gibbon = Gibbon::Request.new(api_key: api_key, debug: true)
        end

        def setOptions(options = {})
            @api_key = options[:api_key] || Figaro.env.mailchimp_api_key
            @list_id = options[:list_id] || Figaro.env.mailchimp_list_id
            Rails.logger.info("MailchimpService::Client.setOptions: #{@api_key}, #{list_id}")
            @gibbon = Gibbon::Request.new(api_key: api_key, debug: true)
        end

        def subscribe(user)
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                @lowercase_email_hash = Digest::MD5.hexdigest(user.email.downcase)
                res = @gibbon.lists(@list_id).members(@lowercase_email_hash).upsert(body: {
                    email_address: user.email,
                    status: "subscribed",
                    merge_fields: {LNAME: user.last_name, FNAME: user.first_name}
                })
                return res
            rescue => e
                Rails.logger.error "MailchimpService::Client Failed to subscribe user: #{e.message} - #{e.raw_body}"
            end
            return nil
        end


        def members()
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                return @gibbon.lists(@list_id).members()
            rescue => e
                Rails.logger.error "MailchimpService::Client Failed to list members: #{e.message} - #{e.raw_body}"
            end
            return nil
        end

        def unsubscribe(user)
            if @list_id.nil?
                raise "list_id can't be null"
           end
           begin
                @lowercase_email_hash = Digest::MD5.hexdigest(user.email.downcase)
                res = @gibbon.lists(@list_id).members(@lowercase_email_hash).update(body: {
                    status: "unsubscribed",
                })
                return res
            rescue => e
                Rails.logger.error "MailchimpService::Client Failed to unsubscribe user: #{e.message} - #{e.raw_body}"
            end
            return nil
        end
    end
end
    
{'fraction_non_alphanumeric': 0.10119047619047619, 'fraction_numerical': 0.0007680491551459293, 'mean_word_length': 1.6590096988259315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5271038', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1485, 'n_words': 299}