Sha256: 14a941a2e9dce3f3056af20d9e01c8b08817a2a5772a6470de5b7eedf79913d2

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module MBTA
  module HeavyRail
    class Line
      attr_reader :name, :color
      attr_writer :stations
      attr_accessor :trips

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

      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

      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

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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mbta-0.1.4 lib/mbta/heavy_rail/line.rb
mbta-0.1.3 lib/mbta/heavy_rail/line.rb
mbta-0.1.1 lib/mbta/heavy_rail/line.rb