Skip to content

Commit

Permalink
pagers now run as instance methods. this might break some things, sor…
Browse files Browse the repository at this point in the history
…ry but it's better.
  • Loading branch information
twoism committed Jul 10, 2010
1 parent e397d99 commit 67d6f0a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/active_rain_post.rb
Expand Up @@ -35,8 +35,8 @@ class ActiveRainPost

end

ActiveRainPost.source = 'http://activerain.com/blogs/elizabethweintraub'
@posts = ActiveRainPost.all(:max_pages => 1)
ActiveRainPost.source = 'http://activerain.com/blogs/danawilkinson'
@posts = ActiveRainPost.all(:max_pages => 100)

@posts.each do |post|
puts "#{post.pub_date}"
Expand Down
4 changes: 2 additions & 2 deletions examples/ning_post.rb
Expand Up @@ -46,8 +46,8 @@ class NingPost

end

NING_URL = 'http://www.friendsorenemies.com/profiles/blog/list?user=3vx1daeuxrt14'
@posts = NingPost.new( :source => NING_URL ).all(:max_pages => 2)
NING_URL = 'http://vstar650.ning.com/profiles/blog/list'
@posts = NingPost.new( :source => NING_URL ).all(:max_pages => 10)

@posts.each do |post|
puts "#{post.pub_date} -- #{post.title}"
Expand Down
2 changes: 1 addition & 1 deletion lib/graboid.rb
Expand Up @@ -9,7 +9,7 @@ module Graboid
extend self

def user_agent
@user_agent ||= 'Graboid'
@user_agent ||= 'Foo'
end

def user_agent=(agent)
Expand Down
24 changes: 16 additions & 8 deletions lib/graboid/scraper.rb
Expand Up @@ -25,11 +25,9 @@ def inferred_selector
end

def page_with &block
@pager = block
end

def pager
@pager
define_method :pager do
instance_eval &block
end
end

def root_selector
Expand Down Expand Up @@ -76,7 +74,8 @@ def all opts={}, reload=false
alias_method :scrape, :all

def all_fragments
return page_fragments if self.class.pager.nil?
return page_fragments unless self.respond_to?(:pager)
return page_fragments if self.pager(self.doc).nil?
old_source = self.source

while next_page?
Expand Down Expand Up @@ -151,18 +150,22 @@ def mode=(m)

def next_page?
if max_pages.zero?
return true unless self.class.pager.call(doc).nil?
return true unless self.pager(doc).nil?
else
current_page <= max_pages-1
end
end

def original_source
@original_source
end

def page_fragments
doc.css(self.class.root_selector)
end

def paginate
next_page_url = self.class.pager.call(doc) rescue nil
next_page_url = self.pager(doc)
self.source = next_page_url
self.current_page += 1
end
Expand All @@ -182,11 +185,16 @@ def reset_context
self.max_pages = 0
end

def host
self.source.scan(/http[s]?:\/\/.*\//).first
end

def source
@source
end

def source=(src)
@original_source = src if @original_source.nil?
@source = src
end

Expand Down
4 changes: 2 additions & 2 deletions spec/graboid/scraper_spec.rb
Expand Up @@ -37,7 +37,7 @@ class ScraperWithPager
end

page_with do |doc|
'http://localhost:9393'+doc.css('a.next').first['href'] rescue nil
'http://localhost:9393'+self.doc.css('a.next').first['href'] rescue nil
end

before_paginate do
Expand Down Expand Up @@ -172,7 +172,7 @@ class Phony; include Graboid::Scraper; end
describe "with a limit" do
before(:each) do
@scraper = ScraperWithPager.new( :source => 'http://localhost:9393/posts' )
@scraper.expects(:run_before_paginate_callbacks).times(3)
#@scraper.expects(:run_before_paginate_callbacks).times(3)
@posts = @scraper.all(:max_pages => 3)
end
it "should set the callback" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -1,3 +1,4 @@
--colour
--format nested
--loadby mtime
--backtrace

0 comments on commit 67d6f0a

Please sign in to comment.