<commit_before>module Pacto
  module Stubs
    class WebMockHelper
      class << self
        def validate(request_signature, response)
          return if skip? request_signature
          pacto_response = webmock_to_pacto_response(response)
          contract = Pacto.contract_for(request_signature)
          validation = Validation.new request_signature, pacto_response, contract
          Pacto::ValidationRegistry.instance.register_validation validation
        end

        def generate(request_signature, response)
          return if skip? request_signature

          logger.debug("Generating Contract for #{request_signature}, #{response}")
          uri = URI(request_signature.uri)
          basename = File.basename(uri.path, '.json') + '.json'
          pacto_request = webmock_to_pacto_request(request_signature)
          pacto_response = webmock_to_pacto_response(response)
          # contract_file = File.expand_path(Pacto.configuration.contracts_path, File.dirname(uri.path), basename)
          contract_file = File.join(Pacto.configuration.contracts_path, uri.host, File.dirname(uri.path), basename)

          unless File.exists? contract_file
            generator = Pacto::Generator.new
            FileUtils.mkdir_p(File.dirname contract_file)
            begin
              File.write(contract_file, generator.save('vcr', pacto_request, pacto_response))
              logger.debug("Generating #{contract_file}")
            rescue => e
              logger.error("Error while generating Contract #{contract_file}: #{e}")
              logger.error("Backtrace: #{e.backtrace}")
            end
          end
        end

        def skip? request_signature
          if request_signature.uri.host =~ /json-schema\.org/
            # FIXME: This is hacky.  Ideally we shouldn't be base schemas anyways.
            logger.debug('Skipping hooks (json-schema.org detected)')
            true
          end
        end

        private

        def logger
          @logger ||= Logger.instance<commit_msg>load on generate (so validations work immediately) and log errors<commit_after>module Pacto
  module Stubs
    class WebMockHelper
      class << self
        def validate(request_signature, response)
          return if skip? request_signature
          pacto_response = webmock_to_pacto_response(response)
          contract = Pacto.contract_for(request_signature)
          validation = Validation.new request_signature, pacto_response, contract
          Pacto::ValidationRegistry.instance.register_validation validation
        end

        def generate(request_signature, response)
          return if skip? request_signature

          logger.debug("Generating Contract for #{request_signature}, #{response}")
          begin
            uri = URI(request_signature.uri)
            basename = File.basename(uri.path, '.json') + '.json'
            pacto_request = webmock_to_pacto_request(request_signature)
            pacto_response = webmock_to_pacto_response(response)
            # contract_file = File.expand_path(Pacto.configuration.contracts_path, File.dirname(uri.path), basename)
            contract_file = File.join(Pacto.configuration.contracts_path, uri.host, File.dirname(uri.path), basename)

            unless File.exists? contract_file
              generator = Pacto::Generator.new
              FileUtils.mkdir_p(File.dirname contract_file)
              File.write(contract_file, generator.save('vcr', pacto_request, pacto_response))
              logger.debug("Generating #{contract_file}")

              Pacto.load contract_file, uri.host, :generated
            end
          rescue => e
            logger.error("Error while generating Contract #{contract_file}: #{e}")
            logger.error("Backtrace: #{e.backtrace}")
          end
        end

        def skip? request_signature
          if request_signature.uri.host =~ /json-schema\.org/
            # FIXME: This is hacky.  Ideally we shouldn't be base schemas anyways.
            logger.debug('Skipping hooks (json-schema.org detected)')
            true
          end
        end

        private

        def logger
          @logger ||= Logger.instance
{'fraction_non_alphanumeric': 0.08389101338432123, 'fraction_numerical': 0.0, 'mean_word_length': 2.4331419196062347, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1190600', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1095, 'n_words': 240}