Module: FrothyBeer

Defined in:
lib/frothybeer.rb,
lib/frothybeer/beer.rb,
lib/frothybeer/version.rb,
lib/frothybeer/webpage.rb

Defined Under Namespace

Modules: WebPage Classes: Beer

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.beersearch(beerToFind) ⇒ Object

Main search function used to find the beer



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/frothybeer.rb', line 22

def self.beersearch(beerToFind)
  return nil if beerToFind.nil? # TODO raise an exception
  
  # Clean query string
  query = beerToFind.gsub(" ", "+")
  url = "http://beeradvocate.com/search?q=#{query}&qt=beer"
  
  resultsPage = WebPage::getPage(url)
  if WebPage::hasResults?(resultsPage)
    results = WebPage::getResults(resultsPage)
  else
    # TODO raise an exception
    return []
  end

  # find the best match beers
  beer = nil
  suggestions = []
  results.each do |beer_name, beer_link|
    beer_page = WebPage::getPage(beer_link)
    b = Beer.new(self.makeBeerHash(beer_page))
    
    if beer_name == beerToFind # TODO Figure out a better matching algorithm
      beer = b
    else
      suggestions << b
    end
  end
  
  return suggestions.unshift(beer)
end

.makeBeerHash(page) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/frothybeer.rb', line 54

def self.makeBeerHash(page)
  hash = {}
  
  hash[:name] = self.scrapeName(page)
  hash[:brewery] = self.scrapeBrewery(page)
  hash[:rating] = self.scrapeRating(page)
  hash[:style] = self.scrapeStyle(page)
  hash[:abv] = self.scrapeABV(page)
  
  hash
end

.scrapeABV(page) ⇒ Object



86
87
88
89
90
# File 'lib/frothybeer.rb', line 86

def self.scrapeABV(page)
  raw = page.search("a/b")[3].parent().next().inspect
  return nil if raw.match(/\?/) # There is no abv specified
  abv = raw.match(/([\d\.]*)%/)[0]
end

.scrapeBrewery(page) ⇒ Object



71
72
73
# File 'lib/frothybeer.rb', line 71

def self.scrapeBrewery(page)
  page.search("a/b")[2].html
end

.scrapeName(page) ⇒ Object

Methods to scrape the beer profile page.



67
68
69
# File 'lib/frothybeer.rb', line 67

def self.scrapeName(page)
  page.search("h1[@class='norm']").html
end

.scrapeRating(page) ⇒ Object

Given a beer page, returns rating for the beer (returns nil if rating N/A)



77
78
79
80
# File 'lib/frothybeer.rb', line 77

def self.scrapeRating(page)
  rating = page.search("span[@class='BAscore_big']").first.html
  (rating == "N/A") ? nil : "#{rating}/100"
end

.scrapeStyle(page) ⇒ Object



82
83
84
# File 'lib/frothybeer.rb', line 82

def self.scrapeStyle(page)
  page.search("a/b")[3].html
end