Class: LeicaScFinderApp::Camera

Inherits:
Object
  • Object
show all
Defined in:
lib/leica_sc_finder_app/camera.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def condition
  @condition
end

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def description
  @description
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def name
  @name
end

#priceObject

Returns the value of attribute price.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def price
  @price
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def url
  @url
end

#yearObject

Returns the value of attribute year.



2
3
4
# File 'lib/leica_sc_finder_app/camera.rb', line 2

def year
  @year
end

Class Method Details

.allObject



6
7
8
# File 'lib/leica_sc_finder_app/camera.rb', line 6

def self.all
  @@all
end


14
15
16
17
18
19
20
# 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

.sanitizeObject



10
11
12
# File 'lib/leica_sc_finder_app/camera.rb', line 10

def self.sanitize
  @@all.delete_if { |camera| camera.name =="" }
end

.scrape(url) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#add_self_descriptionObject



42
43
44
45
# 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