Class: RestaurantWeekBoston::Scraper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/restaurant_week_boston/scraper.rb

Overview

Scrapes Restaurant Week site.

Constant Summary collapse

URL =
"http://www.restaurantweekboston.com/?neighborhood=all&meal=all&view=all&cuisine=all"

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



13
14
15
16
17
# File 'lib/restaurant_week_boston/scraper.rb', line 13

def initialize
  @cache = File.expand_path('~/.restaurant_week_boston.cache')
  entries = doc().css('.restaurantEntry')
  @restaurants = entries.map { |entry| Restaurant.new(entry) }
end

Instance Method Details

#docObject

Return a Nokogiri::HTML::Document parsed from get_html. Prints status messages along the way.



39
40
41
42
43
44
45
46
47
# File 'lib/restaurant_week_boston/scraper.rb', line 39

def doc
  # get_html beforehand for good output messages
  html = get_html
  print "Parsing doc..."
  doc = Nokogiri::HTML(html)
  puts "done."
  puts
  doc
end

#each(&blk) ⇒ Object

Iterates over @restaurants. All methods in Enumerable work.



20
21
22
# File 'lib/restaurant_week_boston/scraper.rb', line 20

def each(&blk)
  @restaurants.each(&blk)
end

#get_htmlObject

Returns the result of open()ing the url from create_url(), as a String.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/restaurant_week_boston/scraper.rb', line 25

def get_html
  print "Getting doc..."
  if File.size?(@cache)
    html = File.read(@cache)
  else
    html = open(URL).read
    IO.write(@cache, html)
  end
  puts "done."
  html
end

#special_find(names) ⇒ Object

Pass in an array of names that =~ (case-insensitive) the ones you’re thinking of, and this will get those. So, if you’re thinking of Bond, 224 Boston Street, and Artu, you can pass in [‘bond’, ‘boston street’, ‘artu’].



53
54
55
56
57
58
59
# File 'lib/restaurant_week_boston/scraper.rb', line 53

def special_find(names)
  results = @restaurants.find_all do |restaurant|
    names.detect { |name| name.casecmp(restaurant.name) == 0 }
  end
  # Add separators
  results.join("\n" + ("-" * 80) + "\n")
end