Class: Bahn::Agent

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

Overview

> Am 2013-02-01 von 23:07 bis 23:15 : Oberseestr., Berlin nach Berlin - Alt-Hohenschönhausen, Hauptstraße 10 via Fußweg

Constant Summary collapse

@@options =
{
	:url_route => 'http://mobile.bahn.de/bin/mobil/query.exe/dox?country=DEU&rt=1&use_realtime_filter=1&searchMode=ADVANCED',
	:uri_adresses => 'http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/en?REQ0JourneyStopsS0A=2&REQ0JourneyStopsS0G=',
	:uri_stations => 'http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/en?REQ0JourneyStopsS0A=1&REQ0JourneyStopsS0G='			
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Initialize a new Agent options:

:user_agent => Set the user agent. Default: "bahn.rb"


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

def initialize
	@agent = Mechanize.new
	@agent.user_agent = @@user_agent ||= "bahn.rb"
end

Class Method Details

.user_agent=(val) ⇒ Object

Set the used user agent



27
28
29
# File 'lib/bahn/bahn_agent.rb', line 27

def self.user_agent=val
	@@user_agent = val
end

Instance Method Details

#find_address(address) ⇒ Object

Finds the first usable address for the given parameter. The returned address can then be used for further processing in routes Example: Input: Roßstr. 41 40476 Düsseldorf Output: Düsseldorf - Golzheim, Rossstraße 41



100
101
102
103
104
105
# File 'lib/bahn/bahn_agent.rb', line 100

def find_address address		
	result = @agent.get("#{@@options[:uri_adresses]}#{address}").body.gsub("SLs.sls=", "").gsub(";SLs.showSuggestion();", "")
	# a Mechanize::File instead of a Page is returned so we have to convert manually
	result = Iconv.conv("utf-8", "iso-8859-1", result)
	JSON.parse(result)["suggestions"].first["value"]
end

#find_station(name) ⇒ Object

Find the first best station by name Example: Input: HH Allee Düsseldorf Output: Heinrich-Heine-Allee U, Düsseldorf



89
90
91
92
93
94
# File 'lib/bahn/bahn_agent.rb', line 89

def find_station name		
	result = @agent.get("#{@@options[:uri_stations]}#{name}").body.gsub("SLs.sls=", "").gsub(";SLs.showSuggestion();", "")
	# a Mechanize::File instead of a Page is returned so we have to convert manually
	result = Iconv.conv("utf-8", "iso-8859-1", result)
	r = JSON.parse(result)["suggestions"].first["value"]
end

#get_routes(from, to, options = {}) ⇒ Object

Get the next few routes with public transportation from A to B.

:start_type and :target_type should be the same, no other options is implemented yet Options: * :time => start time for the connection * :start_type => 1 = station, 2 = address * :target_type => 1 = station, 2 = address Returns: Array of Bahn::Route(s) Raises: “no_route” if no route could be found



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
76
77
78
79
80
81
82
83
# File 'lib/bahn/bahn_agent.rb', line 50

def get_routes from, to, options = {}
	options = {:time => Time.now, :start_type => 2, :target_type => 2, :depth => 0}.merge(options)
	page = @agent.get @@options[:url_route]
	form = page.forms.first
	form["REQ0JourneyDate"] = "#{options[:time].day}.#{options[:time].month}.#{options[:time].year-2000}"
	form["REQ0JourneyTime"] = "#{options[:time].hour}:#{options[:time].min}"
	form["REQ0JourneyStopsS0A"] = options[:start_type]
	form["REQ0JourneyStopsZ0A"] = options[:target_type]
	form["REQ0JourneyStopsS0G"] = from
	form["REQ0JourneyStopsZ0G"] = to			
	result = form.submit(form.button_with(:value => "Suchen"))
	
	type = :undefined
	type = :door2door if options[:start_type] == 2 && options[:target_type] == 2
	type = :station2station if options[:start_type] == 1 && options[:target_type] == 1

	routes = []
	links = result.links_with(:href => /details=opened!/)
	links.each  do |link|
	  page = link.click 
	  routes << Route.new(page, type)
	end
	
	# Keine Station gefunden und es werden keine Vorschläge angezeigt... 
	# also suchen wir nachder nächstbesten Adresse und nutzen dies
	if links.count == 0 && options[:depth] == 0 && type == :door2door
		from = find_address from
		to = find_address to
		return get_routes from, to, {:time => options[:time], :depth => options[:depth]+1}
	end
	
	raise "no_route" if routes.count == 0 || links.count == 0			
	routes
end