millisami
millisami

Reputation: 10161

Mechanize with FakeWeb

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?

Upvotes: 1

Views: 1449

Answers (2)

Felipe Lima
Felipe Lima

Reputation: 10750

You can easily fix that adding the option :content_type => "text/html" you your FakeWeb.register_uri call

Upvotes: 5

Steve Graham
Steve Graham

Reputation: 3021

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.

Upvotes: 7

Related Questions