Class: DiningHall

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/dininghall.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DiningHall

Has days, which have meals



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/classes/dininghall.rb', line 7

def initialize(name)
  @name = name
  case @name.downcase
    when "hill"
      dining_hall_path = "hill/"
    when "commons"
      dining_hall_path = "commons/"
    when "kings court"
      dining_hall_path = "kings/"
    when "mcclelland"
      dining_hall_path = "mcclelland/"
  end
  
  @urls = {
  'breakfast' => ROOT_URL + dining_hall_path + BREAKFAST_PATH,
  'lunch' => ROOT_URL + dining_hall_path + LUNCH_PATH,
  'dinner' => ROOT_URL + dining_hall_path + DINNER_PATH,
  }
  
  # Get dates for each day in the current week
  self.get_days()
  
  # Get the tds for each #{meal} for each day of the week
  @urls.each { |meal, _| self.get_meal_tds(meal) }
end

Instance Attribute Details

#breakfast_tdsObject

Returns the value of attribute breakfast_tds.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def breakfast_tds
  @breakfast_tds
end

#datesObject

Returns the value of attribute dates.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def dates
  @dates
end

#dinner_tdsObject

Returns the value of attribute dinner_tds.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def dinner_tds
  @dinner_tds
end

#fridayObject

Returns the value of attribute friday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def friday
  @friday
end

#lunch_tdsObject

Returns the value of attribute lunch_tds.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def lunch_tds
  @lunch_tds
end

#mondayObject

Returns the value of attribute monday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def monday
  @monday
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def name
  @name
end

#saturdayObject

Returns the value of attribute saturday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def saturday
  @saturday
end

#sundayObject

Returns the value of attribute sunday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def sunday
  @sunday
end

#thursdayObject

Returns the value of attribute thursday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def thursday
  @thursday
end

#tuesdayObject

Returns the value of attribute tuesday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def tuesday
  @tuesday
end

#urlsObject

Returns the value of attribute urls.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def urls
  @urls
end

#wednesdayObject

Returns the value of attribute wednesday.



2
3
4
# File 'lib/classes/dininghall.rb', line 2

def wednesday
  @wednesday
end

Instance Method Details

#get_daysObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/classes/dininghall.rb', line 33

def get_days()
  @dates = []
  today = Date.today
  @dates << today - today.wday # sunday
  (1..6).each { |n| @dates << @dates[0] + n }
  
  # Assign dates to days
  # Each day will contain b, l, and d menus
  @dates.each do |date|
    if !(DAYS.index(date.strftime("%A").downcase).nil?)
      self.instance_variable_set("@#{date.strftime("%A").downcase}", Day.new(self, date))
    end
  end
end

#get_meal_tds(meal) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/classes/dininghall.rb', line 48

def get_meal_tds(meal)
  # Get menu table from dining website, use dinner for dates
  agent = Mechanize.new
  meal_page = agent.get(self.urls[meal])
  table = meal_page.search("div.boxbody")
  
  # Scrape all tds into array
  tds = []
  table.xpath('//tr/td').to_a.each do |td|
    td = td.text.lstrip.rstrip
    tds << td unless (td.gsub(/\s+/, "") == "" or td.gsub(/\s+/, "") == "\u00A0")
  end
  
  # Remove dining hall title, which is the first element in tds
  tds.shift
  
  # Set instance var
  self.instance_variable_set("@#{meal}_tds", tds)
end