Class: Metrorail::Line

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

Overview

A class representing a line in the Metrorail system.

Constant Summary collapse

@@lines =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, stations = []) ⇒ Line

Returns a new instance of Line.



113
114
115
116
117
# File 'lib/metrorail.rb', line 113

def initialize(id, name, stations=[])
    @id = id
    @name = name
    @stations = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



110
111
112
# File 'lib/metrorail.rb', line 110

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



110
111
112
# File 'lib/metrorail.rb', line 110

def name
  @name
end

#stationsObject (readonly)

Returns the value of attribute stations.



110
111
112
# File 'lib/metrorail.rb', line 110

def stations
  @stations
end

Class Method Details

.all(force = false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/metrorail.rb', line 123

def self.all(force=false)
    if @@lines.size > 0
        return @@lines
    else
        lines_request = Metrorail::make_request("Rail", "jLines")["Lines"]
        lines = []
        lines_request.each do |line|
            new_line = Line.new(line["LineCode"], line["DisplayName"])
            stations = Metrorail::make_request("Rail", "jPath", {fromStationCode: line["StartStationCode"], toStationCode: line["EndStationCode"]})["Path"]
            stations.each do |station|
                new_line.add_station(Metrorail::Station.find_by_id(station["StationCode"]))
            end
            lines << new_line
        end
        return lines
    end
end

.find_by_id(id) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/metrorail.rb', line 141

def self.find_by_id(id)
    self.update_cache_if_needed
    matching = self.all.select { |line| line.id == id }
    if matching.size > 0
        matching[0]
    else
        nil
    end
end

.update_cacheObject



151
152
153
# File 'lib/metrorail.rb', line 151

def self.update_cache
    @@lines = self.all(true)
end

.update_cache_if_neededObject



155
156
157
158
159
# File 'lib/metrorail.rb', line 155

def self.update_cache_if_needed
    if @@lines.size == 0
        self.update_cache
    end
end

Instance Method Details

#add_station(station) ⇒ Object



119
120
121
# File 'lib/metrorail.rb', line 119

def add_station(station)
    @stations << station
end