Class: Bahn::Route

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::DateHelper
Defined in:
lib/bahn/bahn_route.rb

Overview

Feel free to refactor ;)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, type) ⇒ 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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bahn/bahn_route.rb', line 15

def initialize page, 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
	@type = type

	route = ""
	html_parts.each do |part|
		text = part.text.strip
		next if text.start_with? "Reiseprofil" # not important
		route << text << "\n"
	end
	route = route.split("\n")
	create_door2door route, summary_time if @type == :door2door
	create_station2station route if @type == :station2station
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



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

def parts
  @parts
end

#priceObject

Returns the value of attribute price.



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

def price
  @price
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#end_timeObject

End time of the route



49
50
51
# File 'lib/bahn/bahn_route.rb', line 49

def end_time
	@parts.last.start_time
end

#end_time_from_nowObject

End time from now in words



39
40
41
# File 'lib/bahn/bahn_route.rb', line 39

def end_time_from_now
	distance_of_time_in_words DateTime.now, @parts.last.start_time
end

#startObject

Starting point of the route



54
55
56
# File 'lib/bahn/bahn_route.rb', line 54

def start
	@parts.first.start
end

#start_timeObject

Start time of the route



44
45
46
# File 'lib/bahn/bahn_route.rb', line 44

def start_time
	@parts.first.start_time
end

#start_time_from_nowObject

Start time from now in words



34
35
36
# File 'lib/bahn/bahn_route.rb', line 34

def start_time_from_now
	distance_of_time_in_words DateTime.now, @parts.first.start_time
end

#targetObject

Target point of the route



59
60
61
# File 'lib/bahn/bahn_route.rb', line 59

def target
	@parts.last.target
end