Class: BeerMe::Beer

Inherits:
Object
  • Object
show all
Defined in:
lib/beer_me/beer.rb

Constant Summary collapse

@@beers =

set the inital value of beers to be an empty array that can hold the beers

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeer

Returns a new instance of Beer.



7
8
9
# File 'lib/beer_me/beer.rb', line 7

def initialize
  @@beers << self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/beer_me/beer.rb', line 2

def name
  @name
end

#reviewsObject

Returns the value of attribute reviews.



2
3
4
# File 'lib/beer_me/beer.rb', line 2

def reviews
  @reviews
end

#scoreObject

Returns the value of attribute score.



2
3
4
# File 'lib/beer_me/beer.rb', line 2

def score
  @score
end

#styleObject

Returns the value of attribute style.



2
3
4
# File 'lib/beer_me/beer.rb', line 2

def style
  @style
end

Class Method Details

.allObject



11
12
13
# File 'lib/beer_me/beer.rb', line 11

def self.all
  @@beers
end

.scrape_beers_siteObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/beer_me/beer.rb', line 15

def self.scrape_beers_site
  #obtains the page from ratebeer as nokogiri elements
  doc = Nokogiri::HTML(open("https://www.ratebeer.com/Ratings/TopOfTheMonth.asp"))

  #List the beers of the month
  #Use 'tr' to get the rows that contains the info for each beer
  #each row is an array of items 
  #the name of the beer is the [1] element
  beer_rows = doc.css('tr')
  
  beer_rows.each_with_index do |row, i|
    #since the iteration will bring back the first row and that row has no info we want to skip the first one
    if i != 0 
      beer = BeerMe::Beer.new
      beer.name = row.css('td')[1].text
      beer.style = row.css('td')[4].text
      beer.reviews = row.css('td')[3].text
      beer.score = row.css('td')[2].text
      
    end
  end
end