Class: GTFS::Realtime

Inherits:
Object
  • Object
show all
Defined in:
lib/gtfs/realtime.rb,
lib/gtfs/realtime/stop.rb,
lib/gtfs/realtime/trip.rb,
lib/gtfs/realtime/route.rb,
lib/gtfs/realtime/shape.rb,
lib/gtfs/realtime/nearby.rb,
lib/gtfs/realtime/version.rb,
lib/gtfs/realtime/database.rb,
lib/gtfs/realtime/stop_time.rb,
lib/gtfs/realtime/trip_update.rb,
lib/gtfs/realtime/configuration.rb,
lib/gtfs/realtime/service_alert.rb,
lib/gtfs/realtime/stop_time_update.rb,
lib/gtfs/realtime/vehicle_position.rb

Defined Under Namespace

Modules: Nearby Classes: Configuration, Database, Model, Route, ServiceAlert, Shape, Stop, StopTime, StopTimeUpdate, Trip, TripUpdate, VehiclePosition

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



26
27
28
# File 'lib/gtfs/realtime.rb', line 26

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



32
33
34
35
36
37
# File 'lib/gtfs/realtime.rb', line 32

def configure
  yield(configuration)

  load_static_feed!
  refresh_realtime_feed!
end

.load_static_feed!(force: false) ⇒ Object



39
40
41
42
43
44
45
46
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
# File 'lib/gtfs/realtime.rb', line 39

def load_static_feed!(force: false)
  return if !force && GTFS::Realtime::Route.count > 0

  static_data = GTFS::Source.build(@configuration.static_feed)

  GTFS::Realtime::Model.db.transaction do
    GTFS::Realtime::Route.dataset.delete
    GTFS::Realtime::Route.multi_insert(
      static_data.routes.collect do |route|
        {
          id: route.id.strip,
          short_name: route.short_name,
          long_name: route.long_name,
          url: route.url
        }
      end
    )

    GTFS::Realtime::Shape.dataset.delete
    GTFS::Realtime::Shape.multi_insert(
      static_data.shapes.collect do |shape|
        {
          id: shape.id.strip,
          sequence: shape.pt_sequence,
          latitude: shape.pt_lat.to_f,
          longitude: shape.pt_lon.to_f
        }
      end
    )

    GTFS::Realtime::Stop.dataset.delete
    GTFS::Realtime::Stop.multi_insert(
      static_data.stops.collect do |stop|
        {
          id: stop.id.strip,
          name: stop.name,
          latitude: stop.lat.to_f,
          longitude: stop.lon.to_f
        }
      end
    )

    GTFS::Realtime::StopTime.dataset.delete
    GTFS::Realtime::StopTime.multi_insert(
      static_data.stop_times.collect do |stop_time|
        {
          stop_id: stop_time.stop_id.strip,
          trip_id: stop_time.trip_id.strip,
          arrival_time: stop_time.arrival_time,
          departure_time: stop_time.departure_time,
          stop_sequence: stop_time.stop_sequence.to_i
        }
      end
    )

    GTFS::Realtime::Trip.dataset.delete
    GTFS::Realtime::Trip.multi_insert(
      static_data.trips.collect do |trip|
        {
          id: trip.id.strip,
          headsign: trip.headsign.strip,
          route_id: trip.route_id.strip,
          service_id: trip.service_id.strip,
          shape_id: trip.shape_id.strip
        }
      end
    )
  end
end

.refresh_realtime_feed!Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gtfs/realtime.rb', line 109

def refresh_realtime_feed!
  trip_updates = get_entities(@configuration.trip_updates_feed)
  vehicle_positions = get_entities(@configuration.vehicle_positions_feed)
  service_alerts = get_entities(@configuration.service_alerts_feed)

  GTFS::Realtime::Model.db.transaction do
    GTFS::Realtime::TripUpdate.dataset.delete
    GTFS::Realtime::TripUpdate.multi_insert(
      trip_updates.collect do |trip_update|
        {
          id: trip_update.id.strip,
          trip_id: trip_update.trip_update.trip.trip_id.strip,
          route_id: trip_update.trip_update.trip.route_id.strip
        }
      end
    )

    GTFS::Realtime::StopTimeUpdate.dataset.delete
    GTFS::Realtime::StopTimeUpdate.multi_insert(
      trip_updates.collect do |trip_update|
        trip_update.trip_update.stop_time_update.collect do |stop_time_update|
          {
            trip_update_id: trip_update.id.strip,
            stop_id: stop_time_update.stop_id.strip,
            arrival_delay: stop_time_update.arrival ? stop_time_update.arrival.delay : nil,
            arrival_time: stop_time_update.arrival ? stop_time_update.arrival.time : nil,
            departure_delay: stop_time_update.departure ? stop_time_update.departure.delay : nil,
            departure_time: stop_time_update.departure ? stop_time_update.departure.time : nil,
          }
        end
      end.flatten
    )

    GTFS::Realtime::VehiclePosition.dataset.delete
    GTFS::Realtime::VehiclePosition.multi_insert(
      vehicle_positions.collect do |vehicle|
        {
          trip_id: vehicle.vehicle.trip.trip_id.strip,
          stop_id: vehicle.vehicle.stop_id.strip,
          latitude: vehicle.vehicle.position.latitude.to_f,
          longitude: vehicle.vehicle.position.longitude.to_f,
          bearing: vehicle.vehicle.position.bearing.to_f,
          timestamp: vehicle.vehicle.timestamp
        }
      end
    )

    GTFS::Realtime::ServiceAlert.dataset.delete
    GTFS::Realtime::ServiceAlert.multi_insert(
      service_alerts.collect do |service_alert|
        {
          stop_id: service_alert.alert.informed_entity.first.stop_id.strip,
          header_text: service_alert.alert.header_text.translation.first.text,
          description_text: service_alert.alert.description_text.translation.first.text,
          start_time: service_alert.alert.active_period.first.start,
          end_time: service_alert.alert.active_period.first.end
        }
      end
    )
  end
end