<commit_before># Mailchimp::ContactsService.new(Lab.first).find_or_create_list_from_contacts("bonsoir",  Lab.first.contacts.where(id: 148))
# Mailchimp::ContactsService.new(Lab.first).find_or_create_list("bonsoir")

module Mailchimp
  class ContactsService

    attr_reader :lab, :gibbon

    def initialize(lab)
      @lab    = lab
      @gibbon = Gibbon::Request.new(:api_key => @lab.mailchimp_api_key)
    end

    def create_list_from_contacts(list_name:, contacts:)
      if lab.mailchimp_configured?
        list = find_or_create_list(list_name)

        contacts.each do |contact|
          if contact.email.present?
            upsert_contact_to_list(list['id'], contact)
          end
        end
      end
    end

    def retrieve_lists
      gibbon.lists.retrieve
    end

    def sitcom_list_id
      list_id_from_name('SITCOM')
    end

    def list_id_from_name(list_name)
      retrieve_lists.body['lists'].select { |list| list['name'] == list_name }.try(:first).try(:[], 'id')
    end

    def upsert_contact_to_list(list_id, contact)
      begin
        response = gibbon.lists(list_id).members(self.class.hashed(contact.email)).upsert({<commit_msg>debug info in mailchimp service<commit_after># Mailchimp::ContactsService.new(Lab.first).find_or_create_list_from_contacts("bonsoir",  Lab.first.contacts.where(id: 148))
# Mailchimp::ContactsService.new(Lab.first).find_or_create_list("bonsoir")

module Mailchimp
  class ContactsService

    attr_reader :lab, :gibbon

    def initialize(lab)
      @lab    = lab
      @gibbon = Gibbon::Request.new(:api_key => @lab.mailchimp_api_key)
    end

    def create_list_from_contacts(list_name:, contacts:)
      if lab.mailchimp_configured?
        list = find_or_create_list(list_name)

        contacts.each do |contact|
          if contact.email.present?
            upsert_contact_to_list(list['id'], contact)
          end
        end
      end
    end

    def retrieve_lists
      gibbon.lists.retrieve
    end

    def sitcom_list_id
      list_id_from_name('SITCOM')
    end

    def list_id_from_name(list_name)
      retrieve_lists.body['lists'].select { |list| list['name'] == list_name }.try(:first).try(:[], 'id')
    end

    def upsert_contact_to_list(list_id, contact)
      puts contact.id
      puts contact.email
      puts

      begin
        response = gibbon.lists(list_id).members(self.class.hashed(contact.email)).upsert({
{'fraction_non_alphanumeric': 0.10949813355454169, 'fraction_numerical': 0.002488593944421402, 'mean_word_length': 3.224168126094571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6876640', 'n_tokens_mistral': 841, 'n_tokens_neox': 818, 'n_words': 130}