<commit_before>require 'expertsender_api/subscriber'
require 'expertsender_api/expertsender_error'

module ExpertSenderApi
  class API
    include HTTParty

    class << self
      attr_accessor :api_key, :api_endpoint, :throws_exceptions
    end

    attr_accessor :api_key, :api_endpoint, :throws_exceptions

    def initialize(key: nil, **parameters)
      @api_key = key || self.class.api_key || ENV['EXPERTSENDER_API_KEY']
      @api_key = @api_key.strip if @api_key

      @throws_exceptions = parameters.has_key?(:throws_exceptions) ? parameters.delete(:throws_exceptions) : self.class.throws_exceptions
      @api_endpoint = parameters.delete(:api_endpoint) || self.class.api_endpoint

      @subscribers_url = api_endpoint.concat('/Api/Subscribers') unless api_endpoint.nil?
    end

    def add_subscribers_to_list(subscribers)
      builder = Nokogiri::XML::Builder.new do |xml|
        xml.ApiRequest {
          xml.ApiKey api_key
          xml.MultiData {
            subscribers.each { |subscriber| subscriber.insert_to(xml) }
          }
        }
      end

      xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
      response = self.class.post(@subscribers_url, body: xml)

      handle_response(response)
    end

    def remove_subscriber_from_list(options)
      email = options.delete :email
      id = options.delete :id

      response = id.nil? ? remove_subscriber_by_email(email, options) : remove_subscriber_by_id(id, options)

      handle_response(response)
    end

    private

    def handle_response(response)
      parsed_response = nil<commit_msg>Method for adding one subscriber to list<commit_after>require 'expertsender_api/subscriber'
require 'expertsender_api/expertsender_error'

module ExpertSenderApi
  class API
    include HTTParty

    class << self
      attr_accessor :api_key, :api_endpoint, :throws_exceptions
    end

    attr_accessor :api_key, :api_endpoint, :throws_exceptions

    def initialize(key: nil, **parameters)
      @api_key = key || self.class.api_key || ENV['EXPERTSENDER_API_KEY']
      @api_key = @api_key.strip if @api_key

      @throws_exceptions = parameters.has_key?(:throws_exceptions) ? parameters.delete(:throws_exceptions) : self.class.throws_exceptions
      @api_endpoint = parameters.delete(:api_endpoint) || self.class.api_endpoint

      @subscribers_url = api_endpoint.concat('/Api/Subscribers') unless api_endpoint.nil?
    end

    def add_subscriber_to_list(subscriber)
      add_subscribers_to_list([subscriber])
    end

    def add_subscribers_to_list(subscribers)
      builder = Nokogiri::XML::Builder.new do |xml|
        xml.ApiRequest {
          xml.ApiKey api_key
          xml.MultiData {
            subscribers.each { |subscriber| subscriber.insert_to(xml) }
          }
        }
      end

      xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
      response = self.class.post(@subscribers_url, body: xml)

      handle_response(response)
    end

    def remove_subscriber_from_list(options)
      email = options.delete :email
      id = options.delete :id

      response = id.nil? ? remove_subscriber_by_email(email, options) : remove_subscriber_by_id(id, options)

      handle_response(response)
    end

    private

    def handle_response(response)
      parsed_response = nil
{'fraction_non_alphanumeric': 0.09126155681479273, 'fraction_numerical': 0.0, 'mean_word_length': 3.483957219251337, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '7120631', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1041, 'n_words': 189}