Sha256: ffed53905914bc003faac6b04b19d9001ff22e36a07174bc95c0fcfe4f067fe8

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require './mbta/heavy_rail/line'
require './mbta/heavy_rail/station'
require './mbta/heavy_rail/platform'

module MBTA
  module HeavyRail
    module System
      class Map
        attr_reader :lines

        def initialize(lines = {})
          @lines = lines
        end
      end

      class Builder
        def initialize
          @data = ::CSV.read("../data/RealTimeHeavyRailKeys.csv", { headers: true })
        end

        def map
          data = @data
          line_names = data.inject(Array.new) {|memo, platform| memo.push(platform['Line']) }.uniq
          lines = line_names.map{|name| MBTA::HeavyRail::Line.new(name) }

          lines.each do |line|
            platforms = data.select {|row| row['Line'] == line.name }
            station_names = platforms.inject(Array.new) {|memo, platform| memo.push(platform['StationName']) }.uniq
            line.stations = station_names.map {|name| MBTA::HeavyRail::Station.new(line, name.capitalize) }

            line.stations.each do |station|
              data.select{|row| row['StationName'].capitalize == station.name }.each do |row|
                direction = case row['Direction']
                  when 'NB' then :northbound
                  when 'SB' then :southbound
                  when 'EB' then :eastbound
                  when 'WB' then :westbound
                end
                key = row['PlatformKey'].downcase.to_sym
                station.platforms.push(MBTA::HeavyRail::Platform.new(station, direction, row['PlatformOrder'], key))
              end
            end
          end

          _lines = lines.inject(Hash.new){|lines, line| lines[line.name] = line; lines }

          map = MBTA::HeavyRail::System::Map.new(_lines)
          return map
        end
      end

      MAP = Builder.new.map
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mbta-0.1.3 lib/mbta/heavy_rail/system.rb
mbta-0.1.1 lib/mbta/heavy_rail/system.rb