Class: RestaurantWeekBoston::Restaurant

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/restaurant_week_boston/restaurant.rb

Overview

A Restaurant has the following properties:

  • name (“224 Boston Street”)

  • short-name (“224-boston-street”, used internally by RWB site)

  • meals: %wdinner, %wlunch, %wdinner

  • neighborhood: “back-bay”, etc

  • phone: “617-555-1234”

  • menu: use menu_link_for(“lunch” || “dinner”)

  • map_link: a link to the RWB map page for that Restaurant

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Restaurant

entry is a Nokogiri::XML::Node from the RWB web site.



14
15
16
# File 'lib/restaurant_week_boston/restaurant.rb', line 14

def initialize(entry)
  @entry = entry
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this Restaurant’s name to other‘s name, and returns -1, 0, 1 as appropriate.



21
22
23
# File 'lib/restaurant_week_boston/restaurant.rb', line 21

def <=>(other)
  return name <=> other.name
end

#inspectObject

Returns “<Restaurant: #restaurant-name>”



49
50
51
# File 'lib/restaurant_week_boston/restaurant.rb', line 49

def inspect
  "<Restaurant: #{name}>"
end

Return the URL to the map of this Restaurant at the RWB web site.



130
131
132
# File 'lib/restaurant_week_boston/restaurant.rb', line 130

def map_link
  @map_link ||= "http://www.restaurantweekboston.com/map/#{neighborhood}/#{short_name}/#topOfMap"
end

#mealsObject

Return a 0-2 element array containing the meals offered by this Restaurant: “lunch” and/or “dinner”.



83
84
85
86
87
88
89
90
91
92
# File 'lib/restaurant_week_boston/restaurant.rb', line 83

def meals
  unless @meals
    @meals = []
    lunch = ! @entry.css('a.lunchMenuButton').empty?
    dinner = ! @entry.css('a.dinnerMenuButton').empty?
    @meals << 'lunch' if lunch
    @meals << 'dinner' if dinner
  end
  @meals
end

meal should be either “lunch” or “dinner”



120
121
122
123
124
125
126
# File 'lib/restaurant_week_boston/restaurant.rb', line 120

def menu_link_for(meal)
  if meal == 'lunch'
    @lunch_menu_link ||= "http://www.restaurantweekboston.com/fetch/#{short_name}/lunch/"
  elsif meal == 'dinner'
    @dinner_menu_link ||= "http://www.restaurantweekboston.com/fetch/#{short_name}/dinner/"
  end
end

#nameObject

Return the full name of this Restaurant, e.g. “224 Boston Street”.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/restaurant_week_boston/restaurant.rb', line 69

def name
  unless @name
    # UGLY, but it's not wrapped in a tag so there it is.
    # split: ["224", "Boston", "Street", "[", "map", "]"]
    split = @entry.css('h4')[0].text.strip.split(/\s+/)
    # Remove [[", "map", "]"]
    split.slice!(-3, 3)
    @name = split.join(' ')
  end
  @name
end

#neighborhoodObject

Return the neighborhood this Restaurant is in (e.g. “back-bay”).



95
96
97
98
99
100
101
# File 'lib/restaurant_week_boston/restaurant.rb', line 95

def neighborhood
  unless @neighborhood
    link = @entry.css('a[@href*="neighborhood"]').first
    @neighborhood = link.attributes['href'].value.sub('/?neighborhood=', '')
  end
  @neighborhood
end

#offers_dinner?Boolean

Returns true if this Restaurant offers dinner.

Returns:

  • (Boolean)


59
60
61
# File 'lib/restaurant_week_boston/restaurant.rb', line 59

def offers_dinner?
  meals.include?('dinner')
end

#offers_lunch?Boolean

Returns true if this Restaurant offers lunch.

Returns:

  • (Boolean)


54
55
56
# File 'lib/restaurant_week_boston/restaurant.rb', line 54

def offers_lunch?
  meals.include?('lunch')
end

#phoneObject

Return this Restaurant’s phone number, or “<no phone given>” is none is provided.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/restaurant_week_boston/restaurant.rb', line 106

def phone
  unless @phone
    # UGLY, but it's not wrapped in a tag so there it is.
    phone = @entry.css('.restaurantInfoBasic > p').children[6].to_s
    if phone == '<br>'
      @phone = '<no phone given>'
    else
      @phone = phone
    end
  end
  @phone
end

#short_nameObject

Return the short name (e.g. ‘224-boston-street’ for “224 Boston Street”).



64
65
66
# File 'lib/restaurant_week_boston/restaurant.rb', line 64

def short_name
  @short_name ||= @entry[:id].sub('restaurantID-', '')
end

#to_sObject

Generate a pretty representation of the Restaurant.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/restaurant_week_boston/restaurant.rb', line 27

def to_s
  # back-bay -> "Back Bay"
  pretty_neighborhood = neighborhood.split('-').map{|x| x.capitalize }.join(' ')

  s = "Name: #{name} (short-name: #{short_name})"
  s += "\n"
  s += "Meals: #{meals.map(&:capitalize).join(', ')}"
  s += "\n"
  s += "Neighborhood: #{pretty_neighborhood}"
  s += "\n"
  s += "Phone: #{phone}"
  @meals.each do |meal|
    s += "\n"
    s += "%s Menu: #{menu_link_for(meal)}" % [meal.capitalize]
  end
  s += "\n"
  s += "Map: #{map_link}"
  s
end