Class: Bart::Station

Inherits:
Object
  • Object
show all
Defined in:
lib/bart/station.rb,
lib/bart/station/list.rb

Constant Summary collapse

LIST =
[
  { :id => "12th", :name => "12th St. Oakland City Center" },
  { :id => "16th", :name => "16th St. Mission (SF)" },
  { :id => "19th", :name => "19th St. Oakland" },
  { :id => "24th", :name => "24th St. Mission (SF)" },
  { :id => "ashb", :name => "Ashby (Berkeley)" },
  { :id => "balb", :name => "Balboa Park (SF)" },
  { :id => "bayf", :name => "Bay Fair (San Leandro)" },
  { :id => "cast", :name => "Castro Valley" },
  { :id => "civc", :name => "Civic Center (SF)" },
  { :id => "cols", :name => "Coliseum" },
  { :id => "colm", :name => "Colma" },
  { :id => "conc", :name => "Concord" },
  { :id => "daly", :name => "Daly City" },
  { :id => "dbrk", :name => "Downtown Berkeley" },
  { :id => "dubl", :name => "Dublin/Pleasanton" },
  { :id => "deln", :name => "El Cerrito del Norte" },
  { :id => "plza", :name => "El Cerrito Plaza" },
  { :id => "embr", :name => "Embarcadero (SF)" },
  { :id => "frmt", :name => "Fremont" },
  { :id => "ftvl", :name => "Fruitvale (Oakland)" },
  { :id => "glen", :name => "Glen Park (SF)" },
  { :id => "hayw", :name => "Hayward" },
  { :id => "lafy", :name => "Lafayette" },
  { :id => "lake", :name => "Lake Merritt (Oakland)" },
  { :id => "mcar", :name => "MacArthur (Oakland)" },
  { :id => "mlbr", :name => "Millbrae" },
  { :id => "mont", :name => "Montgomery St. (SF)" },
  { :id => "nbrk", :name => "North Berkeley" },
  { :id => "ncon", :name => "North Concord/Martinez" },
  { :id => "oakl", :name => "Oakland Int'l Airport" },
  { :id => "orin", :name => "Orinda" },
  { :id => "pitt", :name => "Pittsburg/Bay Point" },
  { :id => "phil", :name => "Pleasant Hill" },
  { :id => "powl", :name => "Powell St. (SF)" },
  { :id => "rich", :name => "Richmond" },
  { :id => "rock", :name => "Rockridge (Oakland)" },
  { :id => "sbrn", :name => "San Bruno" },
  { :id => "sfia", :name => "San Francisco Int'l Airport" },
  { :id => "sanl", :name => "San Leandro" },
  { :id => "shay", :name => "South Hayward" },
  { :id => "ssan", :name => "South San Francisco" },
  { :id => "ucty", :name => "Union City" },
  { :id => "wcrk", :name => "Walnut Creek" },
  { :id => "woak", :name => "West Oakland" }
]
ID_TO_NAME =
LIST.inject({}) { |memo, i| memo[i[:id]] = i[:name]; memo }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Station

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.



24
25
26
# File 'lib/bart/station.rb', line 24

def initialize(options = {})
  @abbr = options[:abbr] ? options[:abbr].downcase : nil
end

Instance Attribute Details

#abbrObject (readonly)

Returns the value of attribute abbr.



16
17
18
# File 'lib/bart/station.rb', line 16

def abbr
  @abbr
end

#documentObject (readonly)

DEBUG



19
20
21
# File 'lib/bart/station.rb', line 19

def document
  @document
end

Class Method Details

.from_xml(xml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#departuresObject



45
46
47
48
# File 'lib/bart/station.rb', line 45

def departures
  return @_departures if defined?(@_departures);
  @_departures = load_departures
end

#load_departures(query_params = {}) ⇒ Object

fetch



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

#nameObject



41
42
43
# File 'lib/bart/station.rb', line 41

def name
  ID_TO_NAME[abbr]
end

#parse_departures(xml) ⇒ Object



72
73
74
75
76
77
78
# 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