Sha256: 29a4d684d297fc87a43ce515612945f1570eb29990082ed90287e46008cb5d9b

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

class TopBeers::Scraper
  @@HOME = "https://www.beeradvocate.com"

  def self.scrape_beers
    doc = Nokogiri::HTML(open("https://www.beeradvocate.com/lists/top/"))
    beers = doc.search(".hr_bottom_light[@align='left']")
    self.create_beers(beers)
    beers
  end

  def self.create_beers(beers)
    beers.pop
    i = 0
    beers.each do |beer|
      if i.even?
        new_beer = TopBeers::Beer.new(beer.css("b").text)
        new_beer.url = beer.children[0].attributes['href'].value
        new_beer.brewery = "#{beer.css("#extendedInfo a")[0].text}"
        new_beer.style = beer.css("#extendedInfo a")[1].text
        if beer.css("#extendedInfo").children[3] != nil
          new_beer.abv = beer.css("#extendedInfo").children[3].text[3, 10].chomp(" ABV")
        end
      end
      i += 1
    end
  end

  def self.scrape_details(beer)
    doc = Nokogiri::HTML(open("https://www.beeradvocate.com"+beer.url))
    beer.ba_score = doc.search(".ba-score").text

      beer.brewery.location_1 = "#{doc.search('.break')[1].children[15].text}"
      if beer.brewery.location_1 == "Belgium"
        beer.availability = doc.search(".break")[1].children[35].text.strip
        beer.brewery.website = "#{doc.search(".break")[1].children[17].text}"
        beer.description = doc.search(".break")[1].children[42].text.gsub(/\n\t\t/, '')
      else
        beer.availability = doc.search(".break")[1].children[37].text.strip
        beer.description = doc.search(".break")[1].children[44].text.gsub(/\n\t\t/, '')
        beer.brewery.location_2 = "#{doc.search(".break")[1].children[17].text}"
        beer.brewery.website = "#{doc.search('.break')[1].children[19].text}"
      end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
top-beers-cli-app-0.1.5 lib/top-beers/scraper.rb
top-beers-cli-app-0.1.4 lib/top-beers/scraper.rb
top-beers-cli-app-0.1.3 lib/top-beers/scraper.rb