Module: FrothyBeer::WebPage

Defined in:
lib/frothybeer/webpage.rb

Class Method Summary collapse

Class Method Details

.getPage(url) ⇒ Object

Creates a new webpage from a url



6
7
8
# File 'lib/frothybeer/webpage.rb', line 6

def self.getPage(url)
  open(url) {|f| Hpricot(f)}
end

.getResults(page) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/frothybeer/webpage.rb', line 15

def self.getResults(page)
  return {} if !self.hasResults?(page)
  
  resultHash = {}
  list_elements = page.search("li/a")
  list_elements.each do |li|
    # Beer names are in bold. Alternate links are breweries
    if !li.at("b").nil?
      resultHash[li.at("b").html] = "http://beeradvocate.com#{li.get_attribute("href")}"
    end
  end
  
  resultHash
end

.hasResults?(page) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/frothybeer/webpage.rb', line 10

def self.hasResults?(page)
  # are there elements with 'no results' as the text
  page.search("li[text()*='No results']").empty?
end