Class: Bort::Realtime::Estimates

Inherits:
Object
  • Object
show all
Defined in:
lib/bort/realtime.rb

Constant Summary collapse

VALID_PLATFORMS =
%w(1 2 3 4)
VALID_DIRECTIONS =
%w(n s)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orig, options = {}) ⇒ Estimates

Returns a new instance of Estimates.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bort/realtime.rb', line 8

def initialize(orig, options={})
  self.origin = orig
  load_options(options)

  download_options = {
    :action => 'etd',
    :cmd    => 'etd',
    :orig   => origin,
    :plat   => platform,
    :dir    => direction,
  }

  xml = Util.download(download_options)
  data = Hpricot(xml)

  self.origin = (data/:abbr).inner_text
  self.fetched_at = Time.parse((data/:time).inner_text)
  self.trains = []
  self.destinations = []

  (data/:etd).map do |estimate|
    destination = (estimate/:abbreviation).inner_text
    self.destinations << destination
    (estimate/:estimate).map do |train|
      self.trains << Train.new(train, origin, destination)
    end
  end
end

Instance Attribute Details

#destinationsObject

Returns the value of attribute destinations.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def destinations
  @destinations
end

#directionObject

Returns the value of attribute direction.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def direction
  @direction
end

#fetched_atObject

Returns the value of attribute fetched_at.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def fetched_at
  @fetched_at
end

#originObject

Returns the value of attribute origin.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def origin
  @origin
end

#platformObject

Returns the value of attribute platform.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def platform
  @platform
end

#trainsObject

Returns the value of attribute trains.



4
5
6
# File 'lib/bort/realtime.rb', line 4

def trains
  @trains
end

Instance Method Details

#select(type, value = nil) ⇒ Object Also known as: filter



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bort/realtime.rb', line 37

def select(type, value=nil)
  filters =
    if type.class == Hash
      type
    else
      { type => value }
    end
  trains.select do |train|
    filters.all? do |type, value|
      train.send(type) == value
    end
  end
end