Thursday, 22 August 2013

Method doesn't work even if it's a duplicate of another just with another name?

Method doesn't work even if it's a duplicate of another just with another
name?

In the following code I took the method parse_results, duplicated it and
then called it parse_babies (at the bottom).
class Parser
def self.parse(html)
@data = Nokogiri.HTML(open(html))
merged_hashes = {}
array_of_hashes = [
parse_department,
parse_super_saver,
parse_new_arrivals,
parse_out_of_stock,
parse_categories,
parse_results,
parse_babies
]
array_of_hashes.inject(merged_hashes,:update)
return merged_hashes
end
## Categories
def self.parse_department
#url =
"http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpets&field-keywords="
department = @data.css('#ref_2619534011')
@department_hash = {}
department.css('li').drop(1).each do | department |
department_title = department.css('.refinementLink').text
department_count =
department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@department_hash[:department] ||= {}
@department_hash[:department]["Pet Supplies"] ||= {}
@department_hash[:department]["Pet Supplies"][department_title] =
department_count
end
return @department_hash
end
def self.parse_super_saver
super_saver = @data.css('#ref_2661623011')
@super_saver_hash = {}
super_saver.css('li').each do | super_saver |
super_saver_title = super_saver.css('.refinementLink').text
super_saver_count =
super_saver.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@super_saver_hash[:super_saver] ||= {}
@super_saver_hash[:super_saver][super_saver_title] = super_saver_count
end
return @super_saver_hash
end
def self.parse_new_arrivals
new_arrivals = @data.css('#ref_2661608011')
@new_arrivals_hash = {}
new_arrivals.css('li').each do | new_arrivals |
new_arrivals_title = new_arrivals.css('.refinementLink').text
new_arrivals_count =
new_arrivals.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@new_arrivals_hash[:new_arrivals] ||= {}
@new_arrivals_hash[:new_arrivals][new_arrivals_title] =
new_arrivals_count
end
return @new_arrivals_hash
end
def self.parse_out_of_stock
out_of_stock = @data.css('#ref_2661599011')
@out_of_stock_hash = {}
out_of_stock.css('li').each do | out_of_stock |
out_of_stock_title = out_of_stock.css('.refinementLink').text
out_of_stock_count =
out_of_stock.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@out_of_stock_hash[:out_of_stock] ||= {}
@out_of_stock_hash[:out_of_stock][out_of_stock_title] =
out_of_stock_count
end
return @out_of_stock_hash
end
def self.parse_categories
categories = @data.css('#refinements ul').first
if categories.css('li:nth-child(1) a span').text == "Pet Supplies"
@categories_hash = {}
categories_category = categories.css('li:nth-child(2) strong').text
categories.css('li').drop(2).each do | categories |
categories_title = categories.css('.refinementLink').text
categories_count =
categories.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@categories_hash[:categories] ||= {}
@categories_hash[:categories][categories_category] ||= {}
@categories_hash[:categories][categories_category][categories_title]
= categories_count
end
else
@categories_hash = {}
end
return @categories_hash
end
def self.parse_results
results = @data.css('#refinements ul').first
if results.css('li:nth-child(1) a span').text == "Pet Supplies"
@results_hash = {}
@results_hash[:results] ||= {}
@results_hash[:results] = @data.at_css('#resultCount
span').text[/(\S+) Results$/i, 1].delete(",").to_i
else
@results_hash = {}
end
return @results_hash
end
## Hot Lists
def self.parse_babies
babies = @data.css('#refinements ul').first
if babies.css('li:nth-child(1) a span').text == "Pet Supplies"
@babies_hash = {}
@babies_hash[:babies] ||= {}
@babies_hash[:babies] = @data.at_css('#resultCount
span').text[/(\S+) babies$/i, 1].delete(",").to_i
else
@babies_hash = {}
end
return @babies_hash
end
end
parse_results works just fine. But when I want to wotk with parse_babies I
get this error:
def html_pet_supplies
File.open("parse_categories/amazon_pet_supplies.html")
end
def html_dog_supplies
File.open("parse_categories/amazon_dog_supplies.html")
end
def html_bird_supplies
File.open("parse_categories/amazon_bird_supplies.html")
end
def html_baby
File.open("parse_hotlists/amazon_baby.html")
end
Failure/Error: let(:babies_hash) { Parser.parse html_dog_supplies }
NoMethodError:
undefined method `delete' for nil:NilClass
# ./parser.rb:124:in `parse_babies'
# ./parser.rb:15:in `parse'
What could be the cause?

No comments:

Post a Comment