lib/page_rankr/ranks/google.rb in PageRankr-1.7.1 vs lib/page_rankr/ranks/google.rb in PageRankr-2.0.0

- old
+ new

@@ -1,25 +1,32 @@ -require "open-uri" +require 'typhoeus' require File.join(File.dirname(__FILE__), "google", "checksum") module PageRankr - class Ranks < Tracker - class Google < Rank + class Ranks + class Google + include Rank + def initialize(site) - checksum = Checksum.generate(site) - @rank = begin - open(url(site, checksum)) {|io| io.read.scan(regex)[0][0].to_i} - rescue - -1 + @site = site + @checksum = Checksum.generate(@site.to_s) + + request.on_complete do |response| + @rank = if response.body =~ regex + clean($1) + else + nil + end end end def regex /Rank_\d+:\d+:(\d+)/ end - def url(site, checksum) - "http://toolbarqueries.google.com/search?client=navclient-auto&ch=#{checksum}&features=Rank&q=info:#{site}" + def request + @request ||= Typhoeus::Request.new("http://toolbarqueries.google.com/search", + :params => {:client => "navclient-auto", :ch => @checksum, :features => "Rank", :q => "info:#{@site.to_s}"}) end end end end \ No newline at end of file