metra_schedule 0.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/metra/cacher.rb +1 -1
- data/lib/metra/line.rb +7 -1
- data/lib/metra/stop.rb +4 -0
- data/lib/metra/train.rb +6 -0
- data/test/test_line.rb +12 -0
- data/test/test_stop.rb +4 -0
- data/test/test_train.rb +11 -0
- metadata +1 -1
    
        data/lib/metra/cacher.rb
    CHANGED
    
    
    
        data/lib/metra/line.rb
    CHANGED
    
    | @@ -93,7 +93,7 @@ module MetraSchedule | |
| 93 93 | 
             
                  unless start_or_destination == :start or start_or_destination == :destination
         | 
| 94 94 | 
             
                    raise ArgumentError.new "First argument must be either :start or :destination"
         | 
| 95 95 | 
             
                  end
         | 
| 96 | 
            -
                  raise ArgumentError.new "Not a valid station" unless LINES[ | 
| 96 | 
            +
                  raise ArgumentError.new "Not a valid station" unless LINES[@line_key][:stations].include?(station)
         | 
| 97 97 | 
             
                  @start = station if start_or_destination == :start
         | 
| 98 98 | 
             
                  @destination = station if start_or_destination == :destination 
         | 
| 99 99 | 
             
                  self
         | 
| @@ -116,6 +116,12 @@ module MetraSchedule | |
| 116 116 | 
             
                  self
         | 
| 117 117 | 
             
                end
         | 
| 118 118 |  | 
| 119 | 
            +
                def deduce_direction_by_time
         | 
| 120 | 
            +
                  @dir = :inbound if Time.now < Time.parse("12:00PM")
         | 
| 121 | 
            +
                  @dir = :outbound if Time.now > Time.parse("12:00PM")
         | 
| 122 | 
            +
                  self
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
             | 
| 119 125 | 
             
                def trains
         | 
| 120 126 | 
             
                  return [] unless engines
         | 
| 121 127 | 
             
                  engines.find_all do |engine|
         | 
    
        data/lib/metra/stop.rb
    CHANGED
    
    
    
        data/lib/metra/train.rb
    CHANGED
    
    | @@ -22,5 +22,11 @@ module MetraSchedule | |
| 22 22 | 
             
                  false
         | 
| 23 23 | 
             
                end
         | 
| 24 24 |  | 
| 25 | 
            +
                def departure_and_arrival(start, destination)
         | 
| 26 | 
            +
                  departure = @stops.find {|s| s.station == start}.time
         | 
| 27 | 
            +
                  arrival = @stops.find {|s| s.station == destination}.time
         | 
| 28 | 
            +
                  {:departure => departure, :arrival => arrival}
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 25 31 | 
             
              end
         | 
| 26 32 | 
             
            end
         | 
    
        data/test/test_line.rb
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            require 'test/unit'
         | 
| 2 2 | 
             
            require File.join(File.dirname(__FILE__), "../lib", "metra")
         | 
| 3 | 
            +
            require File.join(File.dirname(__FILE__), "timecop", "lib", "timecop")
         | 
| 3 4 |  | 
| 4 5 | 
             
            class TestLine < Test::Unit::TestCase
         | 
| 5 6 |  | 
| @@ -250,4 +251,15 @@ class TestLine < Test::Unit::TestCase | |
| 250 251 | 
             
                assert_equal([train1, train2, train3, train4], line.trains)
         | 
| 251 252 | 
             
              end
         | 
| 252 253 |  | 
| 254 | 
            +
             | 
| 255 | 
            +
              def test_deduce_direction_by_time
         | 
| 256 | 
            +
                Timecop.freeze(Time.parse("11:00AM"))
         | 
| 257 | 
            +
                line = Metra.new.line(:up_nw).deduce_direction_by_time
         | 
| 258 | 
            +
                assert_equal(:inbound, line.dir)
         | 
| 259 | 
            +
             | 
| 260 | 
            +
                Timecop.freeze(Time.parse("12:01PM"))
         | 
| 261 | 
            +
                line = Metra.new.line(:up_nw).deduce_direction_by_time
         | 
| 262 | 
            +
                assert_equal(:outbound, line.dir)
         | 
| 263 | 
            +
              end
         | 
| 264 | 
            +
             | 
| 253 265 | 
             
            end
         | 
    
        data/test/test_stop.rb
    CHANGED
    
    
    
        data/test/test_train.rb
    CHANGED
    
    | @@ -51,4 +51,15 @@ class TestLine < Test::Unit::TestCase | |
| 51 51 | 
             
                assert_equal(false, @@t.in_time?(:barrington, Time.parse("12:35")))
         | 
| 52 52 | 
             
              end
         | 
| 53 53 |  | 
| 54 | 
            +
              def test_departure_and_arrival
         | 
| 55 | 
            +
                stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse("12:30")
         | 
| 56 | 
            +
                stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse("12:40")
         | 
| 57 | 
            +
                @@t = MetraSchedule::Train.new :train_num => 651, :bike_limit => 12, :schedule => :weekday, :direction => :inbound, :stops => [stop1, stop2]
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                l = Metra.new.line(:up_nw)
         | 
| 60 | 
            +
                l.engines = [@@t]
         | 
| 61 | 
            +
                train = l.trains.first
         | 
| 62 | 
            +
                assert_equal({:departure => Time.parse("12:30"), :arrival => Time.parse("12:40")}, train.departure_and_arrival(:barrington, :arlington_heights))
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 54 65 | 
             
            end
         |