Skip to content

Instantly share code, notes, and snippets.

@BlakeWilliams
Created December 19, 2013 21:37
Show Gist options
  • Select an option

  • Save BlakeWilliams/8046647 to your computer and use it in GitHub Desktop.

Select an option

Save BlakeWilliams/8046647 to your computer and use it in GitHub Desktop.
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, logger: nil, phantomjs_logger: nil)
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def index
handle_search_engines!
end
private
def handle_search_engines!
if request.env["HTTP_USER_AGENT"] =~ /Googlebot|Bingbot|Baiduspider|iaskspider/
webkit = Capybara::Session.new(:poltergeist)
capybara_response = webkit.visit "#{request.scheme}://#{request.host_with_port}/#{params[:url]}"
if capybara_response["status"] == "success"
render text: webkit.source, layout: false
end
webkit.driver.quit
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment