Class: Bahn::Route
- Inherits:
-
Object
- Object
- Bahn::Route
- Defined in:
- lib/bahn/bahn_route.rb
Overview
Feel free to refactor ;)
Instance Attribute Summary collapse
-
#notes ⇒ Object
Returns the value of attribute notes.
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#price ⇒ Object
Returns the value of attribute price.
-
#start_type ⇒ Object
Returns the value of attribute start_type.
-
#target_type ⇒ Object
Returns the value of attribute target_type.
Instance Method Summary collapse
-
#end_time ⇒ Object
End time of the route.
-
#initialize(page, options = {}) ⇒ Route
constructor
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.
-
#start ⇒ Object
Starting point of the route.
-
#start_time ⇒ Object
Start time of the route.
-
#target ⇒ Object
Target point of the route.
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, = {} = {:start_type => :address, :target_type => :address, :include_coords => true}.merge() @do_load = [:include_coords] self.start_type = [:start_type] self.target_type = [: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 [: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 [: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 [:target_type] == :station @target = @parts.last elsif [: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
#notes ⇒ Object
Returns the value of attribute notes.
9 10 11 |
# File 'lib/bahn/bahn_route.rb', line 9 def notes @notes end |
#parts ⇒ Object
Returns the value of attribute parts.
9 10 11 |
# File 'lib/bahn/bahn_route.rb', line 9 def parts @parts end |
#price ⇒ Object
Returns the value of attribute price.
9 10 11 |
# File 'lib/bahn/bahn_route.rb', line 9 def price @price end |
#start_type ⇒ Object
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_type ⇒ Object
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_time ⇒ Object
End time of the route
83 84 85 |
# File 'lib/bahn/bahn_route.rb', line 83 def end_time @parts.last.end_time end |
#start ⇒ Object
Starting point of the route
88 89 90 |
# File 'lib/bahn/bahn_route.rb', line 88 def start @parts.first.start end |
#start_time ⇒ Object
Start time of the route
78 79 80 |
# File 'lib/bahn/bahn_route.rb', line 78 def start_time @parts.first.start_time end |
#target ⇒ Object
Target point of the route
93 94 95 |
# File 'lib/bahn/bahn_route.rb', line 93 def target @parts.last.target end |