Class: Ratis::Timetable

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

Defined Under Namespace

Classes: Stop, Trip

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def direction
  @direction
end

#effectiveObject

Returns the value of attribute effective.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def effective
  @effective
end

#operatorObject

Returns the value of attribute operator.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def operator
  @operator
end

#route_short_nameObject

Returns the value of attribute route_short_name.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def route_short_name
  @route_short_name
end

#service_typeObject

Returns the value of attribute service_type.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def service_type
  @service_type
end

#timepointsObject

Returns the value of attribute timepoints.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def timepoints
  @timepoints
end

#tripsObject

Returns the value of attribute trips.



4
5
6
# File 'lib/ratis/timetable.rb', line 4

def trips
  @trips
end

Class Method Details

.where(conditions) ⇒ Object

Raises:

  • (ArgumentError)


6
7
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ratis/timetable.rb', line 6

def self.where(conditions)
  short_name   = conditions.delete :route_short_name
  direction    = conditions.delete :direction
  date         = conditions.delete :date
  service_type = conditions.delete :service_type

  raise ArgumentError.new('You must provide a route_short_name') unless short_name
  raise ArgumentError.new('You must provide a direction') unless direction
  raise ArgumentError.new('You must provide either date or service_type') if date.blank? && service_type.blank?

  Ratis.all_conditions_used? conditions

  request_params = { 'Route' => short_name, 'Direction' => direction }
  request_params.merge! date ? { 'Date' => date } : { 'Servicetype' => service_type }

  response = Request.get 'Timetable', request_params

  return nil unless response.success?

  headway = response.to_hash[:timetable_response][:headway]

  timetable = Timetable.new
  timetable.route_short_name = headway[:route]
  timetable.direction        = headway[:direction]
  timetable.service_type     = headway[:servicetype]
  timetable.operator         = headway[:operator]
  timetable.effective        = headway[:effective]

  timepoints_array = []

  headway[:timepoints][:stop].each_with_index do |tp, i|
    timepoints_array << Timetable::Stop.new(i, tp[:atisstopid], tp[:stopid], CGI.unescapeHTML(tp[:description]), tp[:area])
  end rescue []

  timetable.timepoints = timepoints_array

  trips_array = []

  headway[:times][:trip].each_with_index do |t,i|
    trips_array << Timetable::Trip.new(i, t[:time], t[:comment], t[:sign])
  end rescue []

  timetable.trips            =  trips_array

  timetable
end