I’ve been hammering out some methods and testing them in the console. The problem is that an error occurs when running on the server. Specifically, the first method below triggers this NoMethod error:
undefined method `text' for nil:NilClass
Here’s the code. It’s from the webpage.rb file, which has a uri column.
def download_page_title
page_as_xml.at_css('title').text
end
def page_as_xml
page_as_xml ||= Nokogiri::HTML(open(page_uri))
end
def page_uri
base_uri = Site.find_by_id(self.site_id).homepage #always has "/" at end
if self.url == base_uri
page_uri = base_uri
else
page_uri = "#{base_uri}#{self.url}"
end
end
Any idea what I’m doing wrong? Also, any idea why things go smoothly in console but derail on the server?
EDIT: Here’s an example of what I’m doing in console.
1.9.3p194 :262 > a = Webpage.new( url: "http://www.cnn.com/", site_id:165)
=> #<Webpage id: nil, url: "http://www.cnn.com/", site_id: 165, created_at: nil, updated_at: nil, title: nil>
1.9.3p194 :263 > a.download_page_title
Site Load (0.3ms) SELECT "sites".* FROM "sites" WHERE "sites"."id" = 165 LIMIT 1
=> "CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video News"
This means that
page_as_xml.at_cssis returningnil, and you’re then trying to call.texton thatnilobject.So, figure out why that’s returning
nil. For example, does the page you’re downloading not specify atitletag that is accessible via CSS selectors? Is the page blank or return no result? Does it return an error status (something not in the 2xx HTTP status range)?To say it another way, there’s probably an upstream difference between the pages you’re using for testing versus what the server is actually getting.
You might need to handle (or throw an exception, or log/report) pages that return
nilfrom a call topage_as_xml.at_css– that really depends on what your software needs to do.