Skip to content

Commit

Permalink
Replace typheous.
Browse files Browse the repository at this point in the history
  • Loading branch information
blatyo committed Dec 17, 2012
1 parent 16cbad7 commit 460c78a
Show file tree
Hide file tree
Showing 19 changed files with 762 additions and 752 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## Version 3.2.2
* Fix for Google pagerank check. Query parts were omitted which made the some pages get the same PR as the domain.
* Lock version of typheous to 0.4 set for ruby 1.8.7 compatability. Typheous to be dropped in 4.0.0 version of PageRankr.

## Version 3.2.1
* Fix issue where tracker calls proxy with class name rather than name defined on class instance.
Expand Down
3 changes: 2 additions & 1 deletion PageRankr.gemspec
Expand Up @@ -16,11 +16,12 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "fuubar", ">= 0.0.1"
s.add_development_dependency "vcr"
s.add_development_dependency "fakeweb"

s.add_runtime_dependency "nokogiri", ">= 1.4.1"
s.add_runtime_dependency "json", ">= 1.4.6"
s.add_runtime_dependency "public_suffix", ">= 0.9.0"
s.add_runtime_dependency "typhoeus", "~> 0.4.1"
s.add_runtime_dependency "httparty", ">= 0.9.0"
s.add_runtime_dependency "jsonpath", ">= 0.4.2"

s.files = `git ls-files`.split("\n")
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -228,6 +228,7 @@ Then, just make sure you require the class and PageRankr and whenever you call P
* [Hans Haselberg](https://github.com/i0rek) - Update typhoeus gem.
* [Priit Haamer](https://github.com/priithaamer) - Fix google backlinks lookup.
* [Marty McKenna](https://github.com/martyMM) - Idea for proxy service
*

## Shout Out
Gotta give credit where credits due!
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Expand Up @@ -4,4 +4,9 @@ Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task :default => :spec

desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -I lib -r page_rankr.rb"
end
4 changes: 3 additions & 1 deletion lib/page_rankr/proxy_services/random.rb
@@ -1,8 +1,10 @@
require 'uri'

module PageRankr
module ProxyServices
class Random
def initialize(proxies)
@proxies = proxies
@proxies = proxies.map{|proxy| URI.parse(proxy)}
end

def proxy(name, site)
Expand Down
2 changes: 1 addition & 1 deletion lib/page_rankr/proxy_services/round_robin.rb
Expand Up @@ -2,7 +2,7 @@ module PageRankr
module ProxyServices
class RoundRobin
def initialize(proxies)
@proxies = proxies
@proxies = proxies.map{|proxy| URI.parse(proxy)}
@index = 0
end

Expand Down
44 changes: 44 additions & 0 deletions lib/page_rankr/request.rb
@@ -0,0 +1,44 @@
require 'httparty'

module PageRankr
class Request
def initialize(tracker, options)
@tracker = tracker
@options = options
end

def perform
method = tracker.method
url = tracker.url

response = HTTParty.send(method, url, construct_options(tracker))
yield response.body if block_given?
end

private
attr_reader :tracker

def construct_options(tracker)
proxy = tracker.proxy
params = tracker.params if tracker.respond_to?(:params)

options = default_options
options.merge!({
:http_proxyaddr => proxy.host,
:http_proxyport => proxy.port,
:http_proxyuser => proxy.user,
:http_proxypass => proxy.password
}) if proxy
options.merge!({:query => params}) if params
options.merge!(@options)
end

def default_options
{
:headers => {
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.56.5 (KHTML, like Gecko) Version/5.1.6 Safari/534.56.5'
}
}
end
end
end
26 changes: 7 additions & 19 deletions lib/page_rankr/tracker.rb
@@ -1,8 +1,8 @@
require 'typhoeus'
require 'nokogiri'
require 'json'
require 'jsonpath'
require File.expand_path('../site', __FILE__)
require File.expand_path('../request', __FILE__)

module PageRankr
module Tracker
Expand All @@ -12,21 +12,7 @@ module Tracker

def initialize(site, options = {})
@site = PageRankr::Site(site)

@options = {:method => method, :headers => {'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'}}
@options[:params] = params if respond_to? :params
@options[:proxy] = proxy
@options.merge!(options)

request.on_complete do |response|
self.body = response.body
self.raw = content(body)
self.tracked = clean(raw)
end
end

def request
@request ||= Typhoeus::Request.new(url, @options)
@options = options
end

def url
Expand All @@ -50,9 +36,11 @@ def proxy
end

def run
hydra = Typhoeus::Hydra.new
hydra.queue request
hydra.run
PageRankr::Request.new(self, @options).perform do |body|
self.body = body
self.raw = content(body)
self.tracked = clean(raw)
end

tracked
end
Expand Down
20 changes: 7 additions & 13 deletions lib/page_rankr/trackers.rb
@@ -1,5 +1,3 @@
require 'typhoeus'

module PageRankr
module Trackers
attr_accessor :site_trackers
Expand All @@ -10,22 +8,18 @@ def initialize

def lookup(site, *trackers)
trackers = site_trackers if trackers.empty?

tracked = {}
hydra = Typhoeus::Hydra.new
trackers.each do |tracker|

trackers.map do |tracker|
name, klass = constant_name(tracker), self.class

next unless klass.const_defined? name

tracked[tracker] = klass.const_get(name).new(site)
hydra.queue tracked[tracker].request
end
hydra.run

tracked.keys.each do |tracker|
tracked[tracker] = tracked[tracker].tracked
end
instance = klass.const_get(name)
Thread.new(tracker, instance, site) do |t, i, s|
tracked[t] = i.new(s).run
end
end.each(&:join)

tracked
end
Expand Down
48 changes: 24 additions & 24 deletions spec/fixtures/vcr_cassettes/alexa_ranks_edge_case_1.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

320 changes: 152 additions & 168 deletions spec/fixtures/vcr_cassettes/failure_backlinks.yml

Large diffs are not rendered by default.

0 comments on commit 460c78a

Please sign in to comment.