Class: MBTA::HeavyRail::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/mbta/heavy_rail/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, stations = {}) ⇒ Line

Returns a new instance of Line.



8
9
10
11
12
13
# File 'lib/mbta/heavy_rail/line.rb', line 8

def initialize(name, stations = {})
  @name = name
  @color = name.downcase.to_sym
  @stations = stations
  @trips = {}
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



4
5
6
# File 'lib/mbta/heavy_rail/line.rb', line 4

def color
  @color
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mbta/heavy_rail/line.rb', line 4

def name
  @name
end

#stations(name_or_direction = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/mbta/heavy_rail/line.rb', line 15

def stations(name_or_direction = nil)
  case name_or_direction
    when String then self[name_or_direction]
    when Symbol then nil
    else @stations
  end
end

#tripsObject

Returns the value of attribute trips.



6
7
8
# File 'lib/mbta/heavy_rail/line.rb', line 6

def trips
  @trips
end

Instance Method Details

#[](name_or_key) ⇒ Object



23
24
25
26
27
28
# File 'lib/mbta/heavy_rail/line.rb', line 23

def [](name_or_key)
  case name_or_key
    when String then @stations.find { |station| station.name == name_or_key }
    when Symbol then platforms.find { |platform| platform.key == name_or_key }
  end
end

#parse_announcements(json) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mbta/heavy_rail/line.rb', line 34

def parse_announcements(json)
  data = JSON.parse(json)
  _trips = data.inject(Array.new) { |memo, announcement| memo << announcement['Trip'].to_i }.uniq
  _trips.each do |trip|
    self.trips[trip] = MBTA::HeavyRail::Trip.new(self, trip)
  end

  data.each do |a|
    trip = self.trips[a['Trip'].to_i]
    time = Time.strptime(a['Time'], '%m/%d/%Y %H:%M:%S %p')
    platform = self[a['PlatformKey'].downcase.to_sym]

    announcement_type = case a['InformationType']
      when 'Arrived' then MBTA::HeavyRail::Announcements::Arrival
      when 'Predicted' then MBTA::HeavyRail::Announcements::Prediction
    end

    trip.announcements << announcement_type.new(trip, platform, time) if announcement_type
  end
end

#platformsObject



30
31
32
# File 'lib/mbta/heavy_rail/line.rb', line 30

def platforms
  stations.inject(Array.new) { |memo, station| memo << station.platforms }.flatten
end