Class: NJTransit::GTFS::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/njtransit/gtfs/importer.rb

Constant Summary collapse

REQUIRED_FILES =
%w[agency.txt routes.txt stops.txt].freeze
OPTIONAL_FILES =
%w[trips.txt stop_times.txt calendar_dates.txt shapes.txt].freeze
TABLE_CONFIGS =

Table configurations: [filename, table, batch_size, field_mapper]

{
  agencies: ["agency.txt", 1000, ->(r) { agency_fields(r) }],
  routes: ["routes.txt", 1000, ->(r) { route_fields(r) }],
  stops: ["stops.txt", 1000, ->(r) { stop_fields(r) }],
  trips: ["trips.txt", 1000, ->(r) { trip_fields(r) }],
  stop_times: ["stop_times.txt", 10_000, ->(r) { stop_time_fields(r) }],
  calendar_dates: ["calendar_dates.txt", 1000, ->(r) { calendar_date_fields(r) }],
  shapes: ["shapes.txt", 50_000, ->(r) { shape_fields(r) }]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path, db_path) ⇒ Importer

Returns a new instance of Importer.



24
25
26
27
# File 'lib/njtransit/gtfs/importer.rb', line 24

def initialize(source_path, db_path)
  @source_path = source_path
  @db_path = db_path
end

Instance Attribute Details

#db_pathObject (readonly)

Returns the value of attribute db_path.



22
23
24
# File 'lib/njtransit/gtfs/importer.rb', line 22

def db_path
  @db_path
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



22
23
24
# File 'lib/njtransit/gtfs/importer.rb', line 22

def source_path
  @source_path
end

Class Method Details

.agency_fields(row) ⇒ Object



43
44
45
46
# File 'lib/njtransit/gtfs/importer.rb', line 43

def agency_fields(row)
  { agency_id: row["agency_id"], agency_name: row["agency_name"],
    agency_url: row["agency_url"], agency_timezone: row["agency_timezone"] }
end

.calendar_date_fields(row) ⇒ Object



69
70
71
# File 'lib/njtransit/gtfs/importer.rb', line 69

def calendar_date_fields(row)
  { service_id: row["service_id"], date: row["date"], exception_type: row["exception_type"]&.to_i }
end

.route_fields(row) ⇒ Object



48
49
50
51
52
# File 'lib/njtransit/gtfs/importer.rb', line 48

def route_fields(row)
  { route_id: row["route_id"], agency_id: row["agency_id"],
    route_short_name: row["route_short_name"], route_long_name: row["route_long_name"],
    route_type: row["route_type"]&.to_i, route_color: row["route_color"] }
end

.shape_fields(row) ⇒ Object



73
74
75
76
# File 'lib/njtransit/gtfs/importer.rb', line 73

def shape_fields(row)
  { shape_id: row["shape_id"], shape_pt_lat: row["shape_pt_lat"]&.to_f,
    shape_pt_lon: row["shape_pt_lon"]&.to_f, shape_pt_sequence: row["shape_pt_sequence"]&.to_i }
end

.stop_fields(row) ⇒ Object



54
55
56
57
# File 'lib/njtransit/gtfs/importer.rb', line 54

def stop_fields(row)
  { stop_id: row["stop_id"], stop_code: row["stop_code"], stop_name: row["stop_name"],
    stop_lat: row["stop_lat"]&.to_f, stop_lon: row["stop_lon"]&.to_f, zone_id: row["zone_id"] }
end

.stop_time_fields(row) ⇒ Object



64
65
66
67
# File 'lib/njtransit/gtfs/importer.rb', line 64

def stop_time_fields(row)
  { trip_id: row["trip_id"], stop_id: row["stop_id"], arrival_time: row["arrival_time"],
    departure_time: row["departure_time"], stop_sequence: row["stop_sequence"]&.to_i }
end

.trip_fields(row) ⇒ Object



59
60
61
62
# File 'lib/njtransit/gtfs/importer.rb', line 59

def trip_fields(row)
  { trip_id: row["trip_id"], route_id: row["route_id"], service_id: row["service_id"],
    trip_headsign: row["trip_headsign"], direction_id: row["direction_id"]&.to_i, shape_id: row["shape_id"] }
end

Instance Method Details

#import(force: false) ⇒ Object



29
30
31
32
33
34
# File 'lib/njtransit/gtfs/importer.rb', line 29

def import(force: false)
  validate_can_import!(force)
  prepare_database(force)
  import_all_tables
  
end

#valid_gtfs_directory?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/njtransit/gtfs/importer.rb', line 36

def valid_gtfs_directory?
  return false unless File.directory?(source_path)

  REQUIRED_FILES.all? { |f| File.exist?(File.join(source_path, f)) }
end