<commit_before>require 'mechanize'

class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
  
  def initialize(*args)
    super
    @agent = ::Mechanize.new
    @agent.redirect_ok = false
  end
  
  def visit(url)
    get url
  end
  
  def cleanup!
    @agent.cookie_jar.clear!
    super
  end
  
  def current_url
    (response_proxy && response_proxy.current_url) || super
  end
  
  def response
    response_proxy || super
  end

  # TODO see how this can be cleaned up
  def follow_redirect!
    unless response.redirect?
      raise "Last response was not a redirect. Cannot follow_redirect!"
    end

    if response.respond_to?(:page)
      location = response.page.response['Location'] 
    else
      location = response['Location']
    end
    get(location)
  end
  
  def get(url, params = {}, headers = {})
    process_remote_request(:get, url) || super
  end

  def post(url, params = {}, headers = {})
    process_remote_request(:post, url, params, headers) || super
  end

  def remote?(url)
    if !Capybara.app_host.nil? 
      true
    elsif Capybara.default_host.nil?
      false
    else
      host = URI.parse(url).host
      !(host.nil? || host.include?(Capybara.default_host))
    end
  end

  private

  def process_remote_request(method, url, *options)
    if remote?(url)
      url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?
      reset_cache
      @agent.send *( [method, url] + options)
      follow_redirects!
      true
    end
  end

  def response_proxy
    ResponseProxy.new(@agent.current_page) if @agent.current_page
  end
  
  class ResponseProxy
    extend Forwardable
    
    def_delegator :page, :body
    
    attr_reader :page
    
    def initialize(page)
      @page = page
    end
    
    def current_url
      page.uri.to_s
    end
    
    def headers
      page.response
    end

    def status
      page.code.to_i
    end    

    def redirect?
      [301, 302].include?(status)
    end
    
  end 
   
end<commit_msg>Have a better seperation between remote and local request and also fixing double redirects<commit_after>require 'mechanize'

class Capybara::Driver::Mechanize < Capybara::Driver::RackTest

  def initialize(*args)
    super
    @agent = ::Mechanize.new
    @agent.redirect_ok = false
  end
    
  def visit(url)
    get url
  end
  
  def cleanup!
    @agent.cookie_jar.clear!
    super
  end
  
  def current_url
    last_request_remote? ? remote_response.current_url : super
  end
  
  def response
    last_request_remote? ? remote_response : super
  end
  
  # TODO see how this can be cleaned up
  def follow_redirect!
    unless response.redirect?
      raise "Last response was not a redirect. Cannot follow_redirect!"
    end

    location = if last_request_remote?
        remote_response.page.response['Location'] 
      else
        response['Location']
      end
    
    get(location)
  end
  
  def get(url, params = {}, headers = {})
    process_remote_request(:get, url) || super
  end

  def post(url, params = {}, headers = {})
    process_remote_request(:post, url, params, headers) || super
  end

  def remote?(url)
    if !Capybara.app_host.nil? 
      true
    elsif Capybara.default_host.nil?
      false
    else
      host = URI.parse(url).host
      !(host.nil? || host.include?(Capybara.default_host))
    end
  end

  private

  def last_request_remote?
    !!@last_request_remote
  end

  def process_remote_request(method, url, *options)
    if remote?(url)
      url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?
      reset_cache
      @agent.send *( [method, url] + options)
      @last_request_remote = true
      follow_redirects!
      true
    else
      @last_request_remote = false
    end
  end

  def remote_response
    ResponseProxy.new(@agent.current_page) if @agent.current_page
  end
  
  class ResponseProxy
    extend Forwardable
    
    def_delegator :page, :body
    
    attr_reader :page
    
    def initialize(page)
      @page = page
    end
    
    def current_url
      page.uri.to_s
    end
    
    def headers
      page.response
    end

    def status
      page.code.to_i
    end    

    def redirect?
      [301, 302].include?(status)
    end
    
  end 
   
end
{'fraction_non_alphanumeric': 0.08951406649616368, 'fraction_numerical': 0.0027900488258544524, 'mean_word_length': 2.4581993569131835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4490459', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1387, 'n_words': 343}