-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.rb
48 lines (42 loc) · 1.18 KB
/
schedule.rb
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'sinatra'
require 'nokogiri'
require 'open-uri'
require 'builder'
NAME = 0
LOCATION = 1
DATE = 2
TIME = 3
get '/' do
get_schedule_for params["date"], params["filter"]
end
get '/StickAndPuck' do
get_schedule_for params["date"], "stick and puck"
end
private
def get_schedule_for(date, event_filter=nil)
date ||= Time.now.strftime("%Y-%m-%d")
doc = Nokogiri::HTML(Kernel.open('http://www.thechiller.com/rink-schedule'))
rows = doc.css("tr[@class='#{date}']")
return "No events found for #{date}" if rows.empty?
xml = build_xml_for_rows rows, event_filter
return "No events found for '#{date}' and '#{event_filter}'" if !xml.include?("<event>")
xml
end
def build_xml_for_rows(rows, event_filter)
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.schedule do
rows.each do |row|
contents = row.children.collect {|c| c.content}
if event_filter.nil? or contents.join(" ").match /#{event_filter}/i
xml.event do
xml.name contents[NAME]
xml.location contents[LOCATION]
xml.date contents[DATE]
xml.time contents[TIME]
end
end
end
end
end
end