I have the following code creates a Parser class which has many parse_something methods. Then finally they are merged in the parse method:
class Parser
def 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
]
array_of_hashes.inject(merged_hashes,:update)
return merged_hashes
end
def parse_department
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 parse_super_saver
(etc...)
So right now, I'm using it like this:
def html_pet_supplies
File.open("amazon_pet_supplies.html")
end
Parser.new.parse html_pet_supplies
But I would like to use it like this instead: Parser.parse html_pet_supplies
I tried removing new but I got this error:
NoMethodError: undefined method `parse' for Parser:Class
What's the simplest way of changing the code so that I just do Parser.parse?