Class: Beanbox::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/beanbox/scraper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(u) ⇒ Scraper

Returns a new instance of Scraper.



8
9
10
11
# File 'lib/beanbox/scraper.rb', line 8

def initialize(u)
  @URL_path = u
  @results = []
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



6
7
8
# File 'lib/beanbox/scraper.rb', line 6

def doc
  @doc
end

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/beanbox/scraper.rb', line 6

def results
  @results
end

#URL_pathObject

Returns the value of attribute URL_path.



6
7
8
# File 'lib/beanbox/scraper.rb', line 6

def URL_path
  @URL_path
end

Instance Method Details

#scrapeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/beanbox/scraper.rb', line 13

def scrape
  @doc = Nokogiri::HTML(open(@URL_path))
  @doc.css("div.twelve.columns.center.pad-top").first.css("div.roast-list").each do |element|
    coffee = Beanbox::Coffee.new
    coffee.name = element.css("h2").text
    # This next line normalizes the name, which is returned in all caps
    coffee.name = coffee.name.split(" ").collect{|n| n.capitalize}.join(" ")
    coffee.roaster = element.css("h3.roast-item-roaster a").text
    coffee.price = element.css("h4.roast-item-price").text.split("\n")[1]
    coffee.url = element.css("a").attribute("href").value
    self.results << coffee # add each coffee to @results
  end

  self.results
end

#scrape_detail(coffee) ⇒ Object



29
30
31
32
33
34
# File 'lib/beanbox/scraper.rb', line 29

def scrape_detail(coffee)
  @doc = Nokogiri::HTML(open(@URL_path))
  coffee.description = @doc.css("div.bb p").text.strip
  coffee.type = @doc.css("div.centered-mobile span.rt a")[1].text
  coffee.roast_level = @doc.css("div.centered-mobile span.rt a")[0].text
end