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



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

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

Instance Attribute Details

#detailObject

Returns the value of attribute detail.



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

def detail
  @detail
end

#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



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

def scrape
  @doc = Nokogiri::HTML(open(@URL_path))
  @doc.css("div.ten.columns.center.results").first.css("div.roast-list").each_with_index do |element, i|
    if ((0..9).to_a.include?(i))
      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
      # binding.pry
      self.results << coffee # add each coffee to @results
    end
  end

  self.results
end

#scrape_detail(coffee) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/beanbox/scraper.rb', line 51

def scrape_detail(coffee)
  @detail = Nokogiri::HTML(open(coffee.url))
  # binding.pry
  coffee.description = @detail.css("div.bb p").text.strip
  coffee.type = @detail.css("div.centered-mobile span.rt a").first.text
  coffee.roast_level = @detail.css("div.centered-mobile span.rt a").first.text
end