metra_schedule: A library to access the Chicago Metra schedule information as Ruby objects.

This is the data library that powers metra.blakesmith.me

Supported Lines:

Union Pacific Northwest => :up_nw
Union Pacific North => :up_n
North Central Service => :ncs
Milwaukee District West => :md_w
Union Pacific West => :up_w
BNSF Railway => :bnsf
Heritage Corrider => :hc
Rock Island District => :ri
Metra Electric District => :me

metra_schedule provides:

Scraper

scrapes the metrarail.com website for schedule information.

Schedule objects

The scraper creates ruby objects for lines, trains and stops

Cacher

After a schedule is scraped, it can be serialized locally for subsequent object access

Flexible Query Language

Build complex scheduling constraints and permutations with the various query methods.

Usage

How to use metra_schedule

Setup

gem install metra_schedule -s http://gemcutter.org

Using

require 'metra'
line = Metra.new.line(:up_nw) # Union pacific Northwest line
line.load_schedule

Building a list of trains

# All outbound trains
line.outbound.trains

# All outbound weekday trains
line.outbound.weekday.trains

# All inbound saturday trains that start at Barrington and end at Ogilve
line.inbound.saturday.from(:barrington).to(:ogilve).trains

# All sunday/holiday outbound trains that start at Arlington Heights after 12:30PM and stop at Park Ridge 
line.outbound.holiday.from(:arlington_heights).at(Time.parse('12:30PM')).to(:park_ridge).trains

# All inbound trains on December 30th 2009
line.inbound.on(Date.parse("December 30th 2009")).trains

Trains

Instance variables:

train.train_num

Metra train number.

train.stops

An array of all Stop objects. Contains the stop name and what time the train will arrive at that stop

train.schedule

Symbol representing the day the train runs. Either :weekday, :saturday, or :sunday (holidays run on a sunday schedule)

train.direction

Symbol representing the train direction. Either :inbound or :outbound

train.bike_limit

Maximum number of bikes per train. nil if no limit.

Instance methods:

train.my_travel_time

Travel time in minutes of your trip. Assumes you specified Line#from and Line#to when you built your query

train.print_my_travel_time

Same as above, but in human readable form EG - “1 hour 2 minutes”

train.has_stop?(stop)

Takes a station name symbol and determines whether this train has that stop

train.in_time?(station, time)

Takes a station name symbol and time. Returns true if the time is before the time the train reaches that stop

Stops

Stored inside each train.

Instance variables:

stop.station

Symbol representing the station name

stop.time

Time object with the time the train stops there

Instance methods:

stop.in_time?(time)

True if time is less than stop.time

Class methods:

Stop.pretty_print(station)

Converts the station symbol to something human readable - EG: :irving_park becomes “Irving Park”

Lines

Instance variables:

line.line_key

Symbol of the line name

line.name

Human readable line name EG - “Union Pacific Northwest”

line.url

Url that the parser scrapes from.

line.dir

Symbol representing selected line direction. :outbound or :inbound. Set with Line#outbound Line#inbound or deduced using Line#from and Line#to or by current time of day using Line#deduce_direction_by_time

line.sched

Symbol representing selected line schedule. :weekday, :saturday or :sunday. Set with Line#weekday, Line#saturday Line#sunday Line#on(date) or Line#deduce_schedule

line.start

Symbol representing starting station name. EG - :clyborn. Set with Line#from

line.destination

Symbol representing destanation station name. EG - :ogilve. Set with Line#to

line.time

Time object representing the time you’d like to depart. Used to filter down lines. Set with Line#at

Instance methods:

line.load_schedule

Load cached engine data from Marshaled cache dir (~/.metra_schedule) or if not available, fetch from the Metra website, parse and cache it

line.trains

Takes in all the other parameters you specified and returns only the trains that meet the criteria you specfied

line.from(station)

Set your starting station

line.to(station)

Set your destination station

line.direction(dir)

Set the line direction. :outbound or :inbound

line.outbound

Same as line.direction(:outbound)

line.inbound

Same as line.direction(:inbound)

line.deduce_direction

Infers line direction if you specified Line#from and Line#to

line.deduce_direction_by_time

Infers line direction based on the system clocks time of day. Before noon it sets to inbound, after noon it assumes outbound

line.schedule(sched)

Sets the line schedule. Either :weekday :saturday or :sunday

line.weekday

Same as line.direction(:weekday)

line.saturday

Same as line.direction(:saturday)

line.sunday

Same as line.direction(:sunday)

line.holiday

Same as line.direction(:sunday)

line.on(date)

Sets schedule using the date you specified

line.deduce_schedule

Same as on(Date.today)

line.at(time)

Sets line time. Specifying this will only display trains that appear at your starting station (specified with Line#from) before the given time

Line Data

Current supported lines:

Union Pacific Northwest => :up_nw

stations:

:ogilve,
:clyborn,
:irving_park,
:jefferson_park,
:gladstone_park,
:norwood_park,
:edison_park,
:park_ridge,
:dee_road,
:des_plaines,
:cumberland,
:mount_prospect,
:arlington_heights,
:arlington_park,
:palatine,
:barrington,
:fox_river_grove,
:cary,
:pingree_road,
:crystal_lake,
:woodstock,
:mchenry,
:harvard

Union Pacific North => :up_n

stations:

:ogilve,
:clyborn,
:ravenswood,
:rogers_park,
:evanston_main_street,
:evanston_davis_street,
:evanston_central_street,
:wilmette,
:kenilworth,
:indian_hill,
:winnetka,
:hubbard_woods,
:glencoe,
:braeside,
:ravinia_park,
:ravinia,
:highland_park,
:highwood,
:fort_sheridan,
:lake_forest,
:lake_bluff,
:greate_lakes,
:north_chicago,
:waukegan,
:zion,
:winthrop_harbor,
:kenosha

North Central Service => :ncs

stations:

:union_station,
:western_avenue,
:river_grove,
:franklin_park,
:schiller_park,
:rosemont,
:ohare_transfer,
:prospect_heights,
:wheeling,
:buffalo_grove,
:prairie_view,
:vernon_hills,
:mundelein,
:libertyville,
:grayslake,
:round_lake_beach,
:lake_villa,
:antioch

Milwaukee District West => :md_w

stations:

:union_station,
:western_avenue,
:grand_cicero,
:hanson_park,
:galewood,
:mars,
:mont_clare,
:elmwood_park,
:river_park,
:franklin_park,
:mannheim,
:bensenville,
:wood_dale,
:itasca,
:medinah,
:roselle,
:schaumburg,
:hanover_park,
:bartlett,
:national_street,
:elgin,
:big_timber

Union Pacific West => :up_w

stations:

:ogilve,
:kedzie,
:oak_park,
:river_forest,
:maywood,
:melrose_park,
:bellwood,
:berkeley,
:elmhurst,
:villa_park,
:lombard,
:glen_ellyn,
:college_avenue,
:wheaton,
:winfield,
:west_chicago,
:geneva,
:la_fox,
:elburn

BNSF Railway => :bnsf

stations:

:union_station,
:halsted,
:western_avenue,
:cicero,
:lavergne,
:berwyn,
:harlem_ave,
:riverside,
:hollywood,
:brookfield,
:congress_park,
:lagrange_road,
:lagrange_stone_avenue,
:western_springs,
:highlands,
:hinsdale,
:west_hinsdale,
:clarendon_hills,
:westmont,
:fairview_avenue,
:downers_grove_main_street,
:belmont,
:lisle,
:naperville,
:route_59,
:aurora

Heritage Corrider => :hc

stations:

:union_station,
:summit,
:willow_springs,
:lemont,
:lockport,
:joliet

Rock Island District => :ri

stations:

:chicago_lasalle_station,
:gresham,
:longwood,
:washington_heights,
:brainerd,
:beverly_hills_91_street,
:beverly_hills_95_street,
:beverly_hills_99_street,
:beverly_hills_103_street,
:beverly_hills_107_street,
:morgan_park_111_street,
:morgan_park_115_street,
:street_119,
:street_123,
:prairie_street,
:blue_island,
:robbins,
:midlothian,
:oak_forest,
:tinley_park,
:tinley_park_80_avenue,
:hickory_creek,
:mokena,
:new_lenox,
:joliet

Metra Electric District => :me

stations:

:milennium_station,
:van_buren_street,
:museum_campus,
:street_18,
:mccormick_place,
:street_27,
:street_47,
:street_51_53,
:street_55_56_57,
:street_59,
:street_63,
:street_75,
:street_79,
:street_83,
:street_87,
:street_91,
:street_95,
:street_103,
:street_107,
:street_111,
:street_115,
:riverdale,
:ivanhoe,
:street_147,
:harvey,
:hazel_crest,
:calumet,
:homewood,
:flossmoor,
:olympia_fields,
:street_211,
:matteson,
:richton_park,
:university_park,
:stony_island,
:bryn_mawr,
:south_shore,
:windsor_park,
:street_79,
:street_83,
:street_87,
:street_93,
:state_street,
:stewart_ridge,
:west_pullman,
:racine,
:ashland,
:burr_oak,
:blue_island