class Metrorail::Station

A class representing a station in the Metrorail system.

Attributes

id[R]
is_transfer[R]
lines_served[R]
location[R]
name[R]

Public Class Methods

all(force=false) click to toggle source
# File lib/metrorail.rb, line 55
def self.all(force=false)
    if not force and @@stations.size > 0
        return @@stations
    end

    stations_request = Metrorail::make_request("Rail", "jStations")["Stations"]
    stations = []

    stations_request.each do |station|
        same_station = stations.select { |s| s.name == station["Name"] }
        if same_station.size > 0
            old_station = same_station[0]
            old_station.add_id station["Code"]
            ["LineCode1", "LineCode2", "LineCode3", "LineCode4"].each do |linecode|
                if station[linecode] != nil
                    old_station.add_line_served(station[linecode])
                end
            end
        else
            new_station = Station.new(station["Code"], station["Name"], [], Location.new(station["Lat"], station["Lon"]))
            ["LineCode1", "LineCode2", "LineCode3", "LineCode4"].each do |linecode|
                if station[linecode] != nil
                    new_station.add_line_served(station[linecode])
                end
            end
            stations << new_station
        end
    end

    return stations
end
find_by_id(id) click to toggle source
# File lib/metrorail.rb, line 87
def self.find_by_id(id)
    self.update_cache_if_needed
    matching = self.all.select { |station| station.id.include? id }
    if matching.size > 0
        matching[0]
    else
        nil
    end
end
new(id, name, lines_served, location) click to toggle source
# File lib/metrorail.rb, line 33
def initialize(id, name, lines_served, location)
    @id = [id]
    @name = name
    @lines_served = lines_served
    @location = location
    @is_transfer = false
end
update_cache() click to toggle source
# File lib/metrorail.rb, line 97
def self.update_cache
    @@stations = self.all(true)
end
update_cache_if_needed() click to toggle source
# File lib/metrorail.rb, line 101
def self.update_cache_if_needed
    if @@stations.size == 0
        self.update_cache
    end
end

Public Instance Methods

add_id(id) click to toggle source
# File lib/metrorail.rb, line 41
def add_id(id)
    @id << id
end
add_line_served(line) click to toggle source
# File lib/metrorail.rb, line 45
def add_line_served(line)
    if not @lines_served.include? line
        @lines_served << line
    end
end
to_s() click to toggle source
# File lib/metrorail.rb, line 51
def to_s
    return "<Station: #{@name} (#{@id.join(",")}) serving #{@lines_served.join(",")} at #{@location.to_s}>"
end