<commit_before>require 'webmock'

VCR::VersionChecker.new('WebMock', WebMock.version, '1.7.0', '1.7').check_version!

module VCR
  module HttpStubbingAdapters
    module WebMock
      include VCR::HttpStubbingAdapters::Common
      extend self

    private

      def response_hash_for(response)
        {
          :body    => response.body,
          :status  => [response.status.code.to_i, response.status.message],
          :headers => response.headers
        }
      end

      def vcr_request_from(webmock_request)
        VCR::Request.new \
          webmock_request.method,
          webmock_request.uri.to_s,
          webmock_request.body,
          webmock_request.headers
      end

      def vcr_response_from(response)
        VCR::Response.new \
          VCR::ResponseStatus.new(response.status.first, response.status.last),
          response.headers,
          response.body,
          '1.1'
      end

      GLOBAL_VCR_HOOK = ::WebMock::RequestStub.new(:any, /.*/).tap do |stub|
        stub.with { |request|
          vcr_request = vcr_request_from(request)

          if VCR.request_ignorer.ignore?(vcr_request)
            false
          elsif VCR.http_interactions.has_interaction_matching?(vcr_request)
            true
          elsif VCR.real_http_connections_allowed?
            false
          else
            raise_connections_disabled_error(vcr_request)
          end
        }.to_return(lambda { |request|
          response_hash_for VCR.http_interactions.response_for(vcr_request_from(request))
        })
      end

      ::WebMock::StubRegistry.instance.register_request_stub(GLOBAL_VCR_HOOK)
      ::WebMock.allow_net_connect!

      ::WebMock.after_request(:real_requests_only => true) do |request, response|
        if enabled?
          http_interaction = VCR::HTTPInteraction.new \
            vcr_request_from(request),
            vcr_response_from(response)

          VCR.record_http_interaction(http_interaction)
        end
      end
    end
  end
end

WebMock::StubRegistry.class_eval do
  undef reset!
  def reset!
    self.request_stubs = [VCR::HttpStubbingAdapters::WebMock::GLOBAL_VCR_HOOK]
  end
end<commit_msg>Ensure that VCR will work even if an end user calls WebMock.disable_net_connect!<commit_after>require 'webmock'

VCR::VersionChecker.new('WebMock', WebMock.version, '1.7.0', '1.7').check_version!

module VCR
  module HttpStubbingAdapters
    module WebMock
      include VCR::HttpStubbingAdapters::Common
      extend self

    private

      def response_hash_for(response)
        {
          :body    => response.body,
          :status  => [response.status.code.to_i, response.status.message],
          :headers => response.headers
        }
      end

      def vcr_request_from(webmock_request)
        VCR::Request.new \
          webmock_request.method,
          webmock_request.uri.to_s,
          webmock_request.body,
          webmock_request.headers
      end

      def vcr_response_from(response)
        VCR::Response.new \
          VCR::ResponseStatus.new(response.status.first, response.status.last),
          response.headers,
          response.body,
          '1.1'
      end

      GLOBAL_VCR_HOOK = ::WebMock::RequestStub.new(:any, /.*/).tap do |stub|
        stub.with { |request|
          vcr_request = vcr_request_from(request)

          if VCR.request_ignorer.ignore?(vcr_request)
            false
          elsif VCR.http_interactions.has_interaction_matching?(vcr_request)
            true
          elsif VCR.real_http_connections_allowed?
            false
          else
            raise_connections_disabled_error(vcr_request)
          end
        }.to_return(lambda { |request|
          response_hash_for VCR.http_interactions.response_for(vcr_request_from(request))
        })
      end

      ::WebMock::StubRegistry.instance.register_request_stub(GLOBAL_VCR_HOOK)

      ::WebMock.after_request(:real_requests_only => true) do |request, response|
        if enabled?
          http_interaction = VCR::HTTPInteraction.new \
            vcr_request_from(request),
            vcr_response_from(response)

          VCR.record_http_interaction(http_interaction)
        end
      end
    end
  end
end

class << WebMock
  # ensure HTTP requests are always allowed; VCR takes care of disallowing
  # them at the appropriate times in its hook
  undef net_connect_allowed?
  def net_connect_allowed?(*args)
    true
  end
end

WebMock::StubRegistry.class_eval do
  # ensure our VCR hook is not removed when WebMock is reset
  undef reset!
  def reset!
    self.request_stubs = [VCR::HttpStubbingAdapters::WebMock::GLOBAL_VCR_HOOK]
  end
end
{'fraction_non_alphanumeric': 0.08627366609294321, 'fraction_numerical': 0.0030120481927710845, 'mean_word_length': 2.4719940253920836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5551462', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1446, 'n_words': 246}