class Bart::Station

Constants

ID_TO_NAME
LIST

Attributes

abbr[R]
document[R]

DEBUG

Public Class Methods

from_xml(xml) click to toggle source
# File lib/bart/station.rb, line 28
def self.from_xml(xml)
  document   = Nokogiri::XML.parse(xml)
  abbr       = document.css('abbr').text
  departures = document.css('etd')

  station = new(abbr)
  station.departures = departures.inject([]) do |memo, i|
    memo << Etd.new(i.to_s)
  end

  station
end
new(options = {}) click to toggle source

OPTIMIZE If we have one station object, we don’t need to initialize new ones over and over again. We’ll leave it alone for now, for simplicity.

# File lib/bart/station.rb, line 24
def initialize(options = {})
  @abbr = options[:abbr] ? options[:abbr].downcase : nil
end

Public Instance Methods

departures() click to toggle source
# File lib/bart/station.rb, line 45
def departures
  return @_departures if defined?(@_departures);
  @_departures = load_departures
end
load_departures(query_params = {}) click to toggle source

fetch

# File lib/bart/station.rb, line 51
def load_departures(query_params = {})
  params = {
    :cmd => 'etd',
    :orig => @abbr,
    :key => 'MW9S-E7SL-26DU-VV8V'
  }

  [:plat, :dir].each do |param|
    params[param] = query_params[param] if query_params.include?(param)
  end

  query_string = '?' + params.map { |key, value| [key, value] * '=' } * '&'
  ssan_etd = Net::HTTP::Get.new('/api/etd.aspx' + query_string )

  response = Net::HTTP.start('api.bart.gov') do |http|
    http.request(ssan_etd)
  end

  parse_departures(response.body)
end
name() click to toggle source
# File lib/bart/station.rb, line 41
def name
  ID_TO_NAME[abbr]
end
parse_departures(xml) click to toggle source
# File lib/bart/station.rb, line 72
def parse_departures(xml)
  document = Nokogiri::XML.parse(xml)
  @document = document
  document.css('etd').inject([]) do |memo, i|
    memo << Etd.new(i.to_s)
  end
end