<commit_before>require 'vcr/util/version_checker'
require 'vcr/request_handler'
require 'excon'

VCR::VersionChecker.new('Excon', Excon::VERSION, '0.9.5', '0.9').check_version!

module VCR
  class LibraryHooks
    # @private
    module Excon
      class RequestHandler < ::VCR::RequestHandler
        attr_reader :params
        def initialize(params)
          @vcr_response = nil
          @params = params
        end

        def handle
          super
        ensure
          invoke_after_request_hook(@vcr_response)
        end

      private

        def on_stubbed_request
          @vcr_response = stubbed_response
          {
            :body     => stubbed_response.body,
            :headers  => normalized_headers(stubbed_response.headers || {}),
            :status   => stubbed_response.status.code
          }
        end

        def on_ignored_request
          perform_real_request
        end

        def response_from_excon_error(error)
          if error.respond_to?(:response)
            error.response
          elsif error.respond_to?(:socket_error)
            response_from_excon_error(error.socket_error)
          else
            warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
          end
        end

        def real_request_params
          # TODO: should we change :retry_limit and :expects for an ignored request?
          params.merge(:mock => false, :retry_limit => 0).tap do |p|
            p.delete(:expects)
          end
        end

        def perform_real_request
          begin
            response = ::Excon.new(uri).request(real_request_params)
          rescue ::Excon::Errors::Error => excon_error
            response = response_from_excon_error(excon_error)
          end

          @vcr_response = vcr_response_from(response)
          yield response if block_given?
          raise excon_error if excon_error

          response.attributes<commit_msg>Delete some more excon options.<commit_after>require 'vcr/util/version_checker'
require 'vcr/request_handler'
require 'excon'

VCR::VersionChecker.new('Excon', Excon::VERSION, '0.9.5', '0.9').check_version!

module VCR
  class LibraryHooks
    # @private
    module Excon
      class RequestHandler < ::VCR::RequestHandler
        attr_reader :params
        def initialize(params)
          @vcr_response = nil
          @params = params
        end

        def handle
          super
        ensure
          invoke_after_request_hook(@vcr_response)
        end

      private

        def on_stubbed_request
          @vcr_response = stubbed_response
          {
            :body     => stubbed_response.body,
            :headers  => normalized_headers(stubbed_response.headers || {}),
            :status   => stubbed_response.status.code
          }
        end

        def on_ignored_request
          perform_real_request
        end

        def response_from_excon_error(error)
          if error.respond_to?(:response)
            error.response
          elsif error.respond_to?(:socket_error)
            response_from_excon_error(error.socket_error)
          else
            warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
          end
        end

        def real_request_params
          # Excon supports a variety of options that affect how it handles failure
          # and retry; we don't want to use any options here--we just want to get
          # a raw response, and then the main request (with :mock => true) can
          # handle failure/retry on its own with its set options.
          params.merge(:mock => false, :retry_limit => 0).tap do |p|
            [:expects, :idempotent, :instrumentor_name, :instrumentor].each do |key|
              p.delete(key)
            end
          end
        end

        def perform_real_request
          begin
            response = ::Excon.new(uri).request(real_request_params)
          rescue ::Excon::Errors::Error => excon_error
            response = response_from_excon_error(excon_error)
          end

          @vcr_response = vcr_response_from(response)
          yield response if block_given?
          raise excon_error if excon_error

          response.attributes
{'fraction_non_alphanumeric': 0.07521810893657156, 'fraction_numerical': 0.0028295213393067674, 'mean_word_length': 2.012784090909091, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '219974', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1165, 'n_words': 282}