views:

207

answers:

1

I'm using Mechanize to extract the links from the page. To ease with development, I'm using fakeweb to do superfast response to get less waiting and annoying with every code run.

tags_url = "http://website.com/tags/"
FakeWeb.register_uri(:get, tags_url, :body => "tags.txt")

agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
   puts link.text.strip
end

When I run the above code, it says:

nokogiri_test.rb:33: undefined method `links' for #<WWW::Mechanize::File:0x9a886e0> (NoMethodError)

After inspecting the class of the page object

puts page.class # => File

If I don't fake out the tags_url, it works since the page class is now Page

puts page.class # => Page

So, how can I use the fakeweb with mechanize to return Page instead of File object?

+4  A: 

Use FakeWeb to replay a prefetched HTTP request:

tags_url = "http://website.com/tags/"
request  = `curl -is #{tags_url}`
FakeWeb.register_uri(:get, tags_url, :response => request)

agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
   puts link.text.strip
end

Calling curl with the -i flag will include headers in the response.

Steve Graham
Thanks 4 ur reply. But doesn't the above code hits the real site over internet everytime I run it? though curl with option -i return the header, my objective was not to hit the internet every time so I kinda like to use the FakeWeb option.
Millisami
no. the code in the backticks shells out and stores the response in `request`. You put the first 3 lines in a `before(:all)` block if you are testing with RSpec, therefore the site would be called once.
Steve Graham