Permalink
Browse files

Merge branch 'master' of github.com:koos/kvb-api

  • Loading branch information...
2 parents 9332ab3 + fa0f287 commit c45f8f04119577a98fb817e9ccc1943f748bf15a @koos koos committed Apr 29, 2012
Showing with 108 additions and 2 deletions.
  1. +31 −0 app/models/line.rb
  2. +2 −2 app/models/station.rb
  3. +75 −0 app/models/vehicle.rb
View
@@ -19,4 +19,35 @@ def save_kind
def stations
@station_ids ||= self.station_connections.map(&:station_a) << self.station_connections.last.station_b
end
+
+
+ def self.cached_routes
+ @cached_routes ||= begin
+ data = Line.all.map do |line|
+ route = {}
+
+ line.station_connections.each do |station_connection|
+ station = station_connection.station_b
+
+ if prev_station = route[station_connection.station_a.kvb_id]
+ prev_station[:travel_time_up] = station_connection.travel_time
+ else
+ # Special case for first station on route
+ route[station_connection.station_a.kvb_id] = {
+ station: station_connection.station_a,
+ travel_time_up: station_connection.travel_time,
+ travel_time_down: 0
+ }
+ end
+
+ route[station.kvb_id] = { station: station, travel_time_up: 0, travel_time_down: station_connection.travel_time }
+ end
+
+ [line.number, route]
+ end
+
+ Hash[data]
+ end
+ end
+
end
View
@@ -35,8 +35,8 @@ def self.associate_aliases_for(station_name, aliases)
station = self.find_by_name(station_name)
if station
[aliases].flatten.each do |alias_name|
- station = StationAlias.find_by_name(alias_name)
- station.update_attribute :station_id, station.id if station
+ station_alias = StationAlias.find_by_name(alias_name)
+ station_alias.update_attribute :station_id, station.id if station_alias
end
end
end
View
@@ -0,0 +1,75 @@
+require 'arrivals'
+
+class Vehicle
+
+ attr_accessor :line, :destination, :direction, :position, :speed
+
+ def self.at_station(station)
+ arrivals = Arrivals.new(station.kvb_id).import.trains
+
+ arrivals.map do |arrival|
+ line = Line.find_by_number(arrival[:line])
+ Rails.logger.debug { "Line: #{line.number} => #{arrival[:destination]}" }
+ if destination = Station.with_name_or_alias(arrival[:destination])
+ direction = station.direction(line, destination) # :up, :down, nil
+ Vehicle.new(line, direction, destination, station, arrival[:arrival])
+ end
+ end.compact
+ end
+
+ def kind
+ line.kind
+ end
+
+ def initialize(line, direction, destination, station, travel_time_to_station)
+ self.line = line
+ self.direction = direction
+ self.destination = destination
+
+ @station = station
+ @travel_time_to_station = travel_time_to_station
+ end
+
+ def id
+ @id ||= nil
+ end
+
+ def arrival_time_at_destination
+ @arrival_time_at_destination ||= begin
+ route = Line.cached_routes[line.number]
+
+ state = :delete
+ remaining_route = route.delete_if do |station_kvb_id, data|
+ if state == :delete and station_kvb_id == @station.kvb_id
+ state = :keep
+ end
+
+ if state == :keep
+ if station_kvb_id == destination.kvb_id
+ state == :delete
+ end
+ end
+
+ state == :delete
+ end
+
+ #raise remaining_route.inspect
+
+ remaining_route.reduce(@travel_time_to_station) { |memo, row| memo += row.last[:travel_time_up] }
+ end
+ end
+
+ def to_hash
+ {
+ line: self.line.number,
+ id: self.id,
+ kind: self.kind,
+ position: self.position,
+ destination: self.destination,
+ direction: self.direction,
+ speed: self.speed,
+ arrival_time_at_destination: self.arrival_time_at_destination
+ }
+ end
+
+end

0 comments on commit c45f8f0

Please sign in to comment.