Class: BusTracker::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/bus_tracker/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Service

Returns a new instance of Service.



5
6
7
8
# File 'lib/bus_tracker/service.rb', line 5

def initialize(number)
  self.number    = number
  self.bus_stops = []
end

Instance Attribute Details

#bus_stopsObject

Returns the value of attribute bus_stops.



3
4
5
# File 'lib/bus_tracker/service.rb', line 3

def bus_stops
  @bus_stops
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/bus_tracker/service.rb', line 3

def number
  @number
end

Instance Method Details

#fetch_bus_stops!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bus_tracker/service.rb', line 10

def fetch_bus_stops!
  self.bus_stops = []

  document  = Nokogiri::HTML(open("#{BASE_URI}getServicePoints.php?serviceMnemo=#{self.number}"))
  bus_stops = document.xpath('//map/markers/busstop')
  bus_stops.each do |bus_stop|
    self.bus_stops << BusTracker::BusStop.new(
      :code            => bus_stop.xpath('sms')[0].content,
      :name            => bus_stop.xpath('nom')[0].content,
      :latitude        => bus_stop.xpath('x')[0].content,
      :longitude       => bus_stop.xpath('y')[0].content,
      :service_numbers => bus_stop.xpath('services/service/mnemo').map {|s| s.content}
    )
  end
end