<commit_before># frozen_string_literal: true

require "mechanize"

module IbmNotes
  class InvalidCredentials < StandardError; end
  class ServiceUnavailable < StandardError; end

  class Api
    def self.get_person_events(params)
      make_request params
    end

    def self.get_event(params)
      make_request params
    end

    def self.get_recurrent_event_instances(params)
      params[:endpoint] += "/instances"
      make_request params
    end

    private

    def self.make_request(params)
      response_page = get_response_page(params)
      begin
        if response_page.uri.path == URI.parse(params[:endpoint]).path
          log_message "[GET #{params[:endpoint]}][HTTP #{response_page.code}]"
          if response_page.code.to_i == 200
            JSON.parse(response_page.body)
          else
            raise IbmNotes::ServiceUnavailable
          end
        else
          raise IbmNotes::InvalidCredentials
        end
      rescue JSON::ParserError
        if response_page.body == ""
          []
        else
          raise JSON::ParserError
        end
      end
    rescue ::Mechanize::ResponseCodeError
      log_message "[GET #{params[:endpoint]}][Mechanize response code error]"
      return nil
    end

    def self.get_response_page(params)
      signin_page = agent.get params[:endpoint]
      result = signin_page.form_with(action: "/names.nsf?Login") do |form|
        form.Username = params[:username]
        form.Password = <PASSWORD>]
      end.submit
    ensure
      agent.shutdown
      result
    end

    def self.agent
      agent = Mechanize.new
      agent.user_agent_alias = "Mac Safari"
      agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      agent
    end

    def self.log_message(message)
      Rails.logger.info "[SYNC-AGENDAS][IBM Notes]#{message}"
    end
  end
end
<commit_msg>Use Mechanize block syntax to ensure connections are closed<commit_after># frozen_string_literal: true

require "mechanize"

module IbmNotes
  class InvalidCredentials < StandardError; end
  class ServiceUnavailable < StandardError; end

  class Api
    def self.get_person_events(params)
      make_request params
    end

    def self.get_event(params)
      make_request params
    end

    def self.get_recurrent_event_instances(params)
      params[:endpoint] += "/instances"
      make_request params
    end

    # private class methods

    def self.make_request(params)
      response_page = get_response_page(params)
      begin
        if response_page.uri.path == URI.parse(params[:endpoint]).path
          log_message "[GET #{params[:endpoint]}][HTTP #{response_page.code}]"
          if response_page.code.to_i == 200
            JSON.parse(response_page.body)
          else
            raise IbmNotes::ServiceUnavailable
          end
        else
          raise IbmNotes::InvalidCredentials
        end
      rescue JSON::ParserError
        if response_page.body == ""
          []
        else
          raise JSON::ParserError
        end
      end
    rescue ::Mechanize::ResponseCodeError
      log_message "[GET #{params[:endpoint]}][Mechanize response code error]"
      nil
    end
    private_class_method :make_request

    def self.get_response_page(params)
      Mechanize.start do |agent|
        agent.user_agent_alias = "Mac Safari"
        agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE

        signin_page = agent.get(params[:endpoint])

        signin_page.form_with(action: "/names.nsf?Login") do |form|
          form.Username = params[:username]
          form.Password = <PASSWORD>]
        end.submit
      end
    end
    private_class_method :get_response_page

    def self.log_message(message)
      Rails.logger.info "[SYNC-AGENDAS][IBM Notes]#{message}"
    end
    private_class_method :log_message
  end
end

{'fraction_non_alphanumeric': 0.08577405857740586, 'fraction_numerical': 0.0015690376569037657, 'mean_word_length': 2.5714285714285716, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4782885', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1097, 'n_words': 242}