Class: Bahn::Route

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

Overview

Feel free to refactor ;)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, options = {}) ⇒ Route

Initialize with a Mechanize::Page and a specific type The page should be the detail view of m.bahn.de Parameters: * page => Mechanize::Page * type => :door2door or :station2station



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bahn/bahn_route.rb', line 16

def initialize page, options = {}
	options = {:start_type => :address, :target_type => :address, :include_coords => true}.merge(options)
	@do_load = options[:include_coords]
	self.start_type = options[:start_type]
	self.target_type = options[:target_type]
	summary_time = page.search("//div[contains(@class, 'querysummary2')]").text.gsub("\n", " ").strip
	html_parts = page.search("//div[contains(@class, 'haupt')]")
	price = html_parts.pop.text
	html_parts.pop

	route = ""
	html_parts.each do |part|
		text = part.text.strip
		next if text.start_with? "Reiseprofil" # not important
		if text.starts_with?("Hinweis", "Aktuelle Informationen")
			self.notes = "#{self.notes}\n#{text}" if !text.include?("Start/Ziel mit äquivalentem Bahnhof ersetzt")
			next
		end
		
		route << text << "\n"
	end
	route = route.split("\n")			
	
	idx = 3
	@start = RoutePart.new
	@target = RoutePart.new
	if options[:start_type] == :address
		@start.start = Station.new({"value" => route[0], :load => :foot, :do_load => @do_load})
		@start.type = "Fußweg" # route[2]
		@start.end_time = DateTime.parse(summary_time.split("-").first.gsub(".13", ".2013"))
		@start.start_time = @start.end_time - route[1].to_i.minutes
		@start.target = route[3]
	elsif	options[:start_type] == :station
		@start.start = Station.new({"value" => route[0], :load => :station, :do_load => @do_load})
		@start.start_time = DateTime.parse(@date.to_s + route[1])
		@start.type = route[2]
		@start.end_time = DateTime.parse(@date.to_s + route[3])
		@start.target = Station.new({"value" => route[4], :load => :station, :do_load => @do_load})
		idx = 4
	end
	
	@date = @start.start_time.to_date # otherwise all dates will be "today"
	create_parts idx, route
	if options[:target_type] == :station
		@target = @parts.last
	elsif options[:target_type] == :address
		@target.type = "Fußweg"
		@target.target = Station.new({"value" => route.last, :load => :foot, :do_load => @do_load})
		if summary_time.split("-").last.strip.length != 5
			# Date is included in the string
			@target.end_time = DateTime.parse(summary_time.split("-").last.gsub(".13", ".2013"))
		else
			# no date given, use start date
			@target.end_time = DateTime.parse("#{@start.start_time.to_date} #{summary_time.split("-").last}")
		end
		
		@target.end_time += route[route.length-3].to_i.minutes				
		@parts << @target
	end
end

Instance Attribute Details

#notesObject

Returns the value of attribute notes.



9
10
11
# File 'lib/bahn/bahn_route.rb', line 9

def notes
  @notes
end

#partsObject

Returns the value of attribute parts.



9
10
11
# File 'lib/bahn/bahn_route.rb', line 9

def parts
  @parts
end

#priceObject

Returns the value of attribute price.



9
10
11
# File 'lib/bahn/bahn_route.rb', line 9

def price
  @price
end

#start_typeObject

Returns the value of attribute start_type.



9
10
11
# File 'lib/bahn/bahn_route.rb', line 9

def start_type
  @start_type
end

#target_typeObject

Returns the value of attribute target_type.



9
10
11
# File 'lib/bahn/bahn_route.rb', line 9

def target_type
  @target_type
end

Instance Method Details

#end_timeObject

End time of the route



83
84
85
# File 'lib/bahn/bahn_route.rb', line 83

def end_time
	@parts.last.end_time
end

#startObject

Starting point of the route



88
89
90
# File 'lib/bahn/bahn_route.rb', line 88

def start
	@parts.first.start
end

#start_timeObject

Start time of the route



78
79
80
# File 'lib/bahn/bahn_route.rb', line 78

def start_time
	@parts.first.start_time
end

#targetObject

Target point of the route



93
94
95
# File 'lib/bahn/bahn_route.rb', line 93

def target
	@parts.last.target
end