Class: MyBusTracker::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, appkey, mbt, service, number = '') ⇒ Service

Returns a new instance of Service.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mybustracker.rb', line 26

def initialize(client, appkey, mbt,  service, number='')
  
  if number.empty? then
    raise MyBusTrackerException::Service, 'provide a bus service number'
  end
  
  @client, @appkey, @mbt = client, appkey, mbt
  
  @number, @name, @ref, relative_dests, operator  = \
      i(mnemo name ref dests operator).map {|field| service[field]}      
  
  Thread.new{ fetch_bus_stops() }
  
end

Instance Attribute Details

#bus_stopsObject (readonly)

Returns the value of attribute bus_stops.



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

def bus_stops
  @bus_stops
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#operatorObject (readonly)

Returns the value of attribute operator.



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

def operator
  @operator
end

#refObject (readonly)

Returns the value of attribute ref.



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

def ref
  @ref
end

Instance Method Details

#inspectObject



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

def inspect()
  "<#<MyBusTracker::Service:%s @number=\"%s\">" % [self.object_id, @number]
end

#query(times: 'next hour', from: nil, to: nil) ⇒ Object

accepts a bus service number and returns the relative bus times



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mybustracker.rb', line 47

def query(times: 'next hour', from: nil, to: nil)
  
  #from ||=
  
  start_bus_stop1, start_bus_stop2 = find_bus_stop(from)
  end_bus_stop1, end_bus_stop2 = find_bus_stop(to)

  # get the bus times
  
  bus_times = get_bus_times(start_bus_stop1)            
  
  journey_times = get_stop_journeys(start_bus_stop1, bus_times, index: 0)
  return unless journey_times

  # select the bus stop end
  
  end_stop = journey_times.find do |x| 
    x[:stop_id] == end_bus_stop1[:stop_id] or 
        x[:stop_id] == end_bus_stop2[:stop_id]
  end
  
        
  if end_stop then
    start_bus_stop = start_bus_stop1
  else
    start_bus_stop = start_bus_stop2
    bus_times = get_bus_times(start_bus_stop2)
    
    unless end_stop
      journey_times = get_stop_journeys(start_bus_stop2, bus_times)
    end
    
    return unless journey_times
    # select the bus stop end

    end_stop = journey_times.find do |x|
      (x[:stop_id] == end_bus_stop1[:stop_id]) or 
          (x[:stop_id] == end_bus_stop2[:stop_id])
    end
    
  end
  
  stop_id = end_stop[:stop_id]      
  
  # get the other journeys
  # still todo
  journeys = [[journey_times[0][:time], end_stop[:time]]]
  journey = get_stop_journeys(start_bus_stop, bus_times, index: 1)      
  
  if journey then
    end_stop = journey.find {|x| x[:stop_id] == stop_id }

    journeys << [journey[0][:time], end_stop[:time]] 
  end
  
  # get the journeys for the given period
  #secs = journeys[1][0][:time] - journey_times[0][:time]
  
  from_times = journeys.map {|x| Time.strptime(x.first, "%H:%M").strftime("%-I:%M%P")}
  to_times = journeys.map {|x| Time.strptime(x.last, "%H:%M").strftime("%-I:%M%P")}

  tstart = Time.strptime(journeys[0][0],"%H:%M")
  tend = Time.strptime(journeys[0][-1],"%H:%M")
  travel_time = '~' + Subunit.new(units={minutes:60, hours:60}, \
                             seconds: tend - tstart).to_s(omit: [:seconds])
  
  index = journey_times.index journey_times.find {|x| x[:stop_id] == stop_id}
  stops = journey_times[0..index].map {|x| x[:stop_name] }
  
  {
    from: {bus_stop: start_bus_stop[:name], bus_stop_id: 
           start_bus_stop[:stop_id], times: from_times},
    to: {bus_stop: end_stop[:stop_name], bus_stop_id: end_stop[:stop_id], 
         times: to_times},
    stops: stops ,
    start: tstart.strftime("%-I:%M%P"), 
    end: tend.strftime("%-I:%M%P"),
    travel_time: travel_time
  }
  
end