Class: Bahn::DepartureBoard

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

Constant Summary collapse

TRANSPORT_TYPES =
{
	:ice =>0x100,
	:ic_ec => 0x80,
	:ir => 0x40,
	:regional => 0x20,
	:urban => 0x10,
	:bus => 0x08,
	:boat => 0x04,
	:subway => 0x02,
	:tram => 0x01
}

Instance Method Summary collapse

Constructor Details

#initialize(station, opts) ⇒ DepartureBoard

Returns a new instance of DepartureBoard.



127
128
129
130
131
132
133
134
135
# File 'lib/bahn.rb', line 127

def initialize(station, opts)
	@station = station
	
	transport_types = Array(opts[:include] || TRANSPORT_TYPES.keys) - Array(opts[:exclude])
	filter_num = transport_types.inject(0) {|sum, type| sum + (TRANSPORT_TYPES[type] || 0) }
	filter = sprintf("%09b", filter_num)
	@url_prefix = "http://reiseauskunft.bahn.de/bin/bhftafel.exe/en?input=#{station.id}&productsFilter=#{filter}&start=1&boardType=dep&dateBegin=14.12.08&dateEnd=12.12.09&time=00:00"
	@departure_pages = []
end

Instance Method Details

#eachObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/bahn.rb', line 137

def each
	0.upto(23) do |hour|
		doc = departure_page(hour)
		# find all tr children of table.result which contain a td.train
		# and do not have class 'browse'
		departure_docs = doc / 'table.result tr[td.train]:not(.browse)'
		departure_docs.each do |departure_doc|
			service_link = departure_doc % 'td.train:nth-child(1) a'
			destination_link = departure_doc % 'td.route span.bold a'
			(destination_id, destination_time_string), = destination_link['onclick'].scan(/^sHC\(this, '', '(\d+)','([^']*)'\)/)
			destination_time = ClockTime.parse(destination_time_string)

			intermediate_stops =  (departure_doc % 'td.route').children.find_all{|node| node.text?}.join(' ')
			departure_time = ClockTime.parse((departure_doc % 'td.time').inner_text)
			last_time = departure_time
			days_passed = 0

			intermediate_stops.scan(/\d+:\d+/) do |time_string|
				time = ClockTime.parse(time_string)
				days_passed += 1 if time < last_time
				last_time = time
			end
			inferred_time_to_destination = days_passed * 24*60*60 + (destination_time - departure_time)
			
			platform_td = departure_doc % 'td.platform'
			if platform_td.nil?
				platform = :none
			else
				platform = platform_td.inner_text.strip
			end

			yield Stop.new(
				:station => @station,
				:service => Service.new(
					:path => service_link['href'].sub(/\?.*/, ''),
					:name => service_link.inner_text.strip.gsub(/\s+/, ' '),
					:destination_info => {
						:station => Station.new(
							:id => destination_id,
							:name => destination_link.inner_text.strip
						),
						:arrival_time => destination_time
					}
				),
				:departure_time => departure_time,
				:platform => (platform == '' ? :none : platform),
				:inferred_time_to_destination => inferred_time_to_destination
			)
		end
	end
end