module RankChecker::Google
Public Class Methods
check(search = nil, domain = nil, search_depth = 20)
click to toggle source
# File lib/rank_checker.rb, line 11 def self.check(search = nil, domain = nil, search_depth = 20) unless search.nil? or domain.nil? found = false page = 0 patterns = [/^.*q=(.*)&sa.*$/i, /^.*url=(.*)&sa.*$/i] while not found and page <= search_depth page += 1 position = (page - 1) * 10 doc = Nokogiri::HTML(open("https://www.google.com/search?q=#{CGI::escape(search)}&start=#{position}&sa=N")) doc.css('.r a').each do |link| host = '' position += 1 patterns.each do |pattern| page_url = link.attributes['href'].value.match(pattern) if not page_url.nil? host = URI.parse(page_url[1]).host break end end if not host.nil? and host.gsub('www.', '') == domain.gsub('www.', '') found = true break end end end position = nil if found == false return position end end