class LeicaScFinderApp::Camera
Attributes
condition[RW]
description[RW]
name[RW]
price[RW]
url[RW]
year[RW]
Public Class Methods
all()
click to toggle source
# File lib/leica_sc_finder_app/camera.rb, line 6 def self.all @@all end
print()
click to toggle source
# File lib/leica_sc_finder_app/camera.rb, line 14 def self.print @@all.each.with_index(1) do |camera, ind| puts "#{ind}. #{camera.name}, price: #{camera.price}, year: #{camera.year}, condition: #{camera.condition}" puts "--Description: #{camera.description}" puts "" end end
sanitize()
click to toggle source
# File lib/leica_sc_finder_app/camera.rb, line 10 def self.sanitize @@all.delete_if { |camera| camera.name =="" } end
scrape(url)
click to toggle source
# File lib/leica_sc_finder_app/camera.rb, line 23 def self.scrape(url) @@all.clear doc = Nokogiri::HTML(open(url)) doc.css("ol.products-list li").each do |camera| the_camera = self.new the_camera.name = camera.css("h2.product-name").text rescue "" the_camera.price = camera.css("span.price").text.gsub(".-", "") rescue "" the_camera.condition = camera.css("span.attribute:contains('Condition:')").text.gsub("Condition: ","") rescue "" the_camera.year = camera.css("span.attribute:contains('Year:')").text.gsub("Year: ", "") rescue "" the_camera.url = camera.css("h2.product-name a").attribute("href").value rescue "" if the_camera.url != "" the_camera.add_self_description end @@all << the_camera end self.sanitize end
Public Instance Methods
add_self_description()
click to toggle source
# File lib/leica_sc_finder_app/camera.rb, line 42 def add_self_description doc = Nokogiri::HTML(open(self.url)) self.description = doc.css("div.std").text.strip end