Class: GTFS::Source

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

Direct Known Subclasses

LocalSource, URLSource

Constant Summary collapse

ENTITIES =
[GTFS::Agency, GTFS::Stop, GTFS::Route, GTFS::Trip, GTFS::StopTime, GTFS::Calendar, GTFS::CalendarDate,
GTFS::Shape, GTFS::FareAttribute, GTFS::FareRule, GTFS::Frequency, GTFS::Transfer, GTFS::FeedInfo,
GTFS::Attribution, GTFS::Pathway, GTFS::Translation, GTFS::Level]
REQUIRED_SOURCE_FILES =
ENTITIES.select(&:required_file?).map(&:filename)
OPTIONAL_SOURCE_FILES =
ENTITIES.reject(&:required_file?).map(&:filename)
SOURCE_FILES =
ENTITIES.map(&:filename)
DEFAULT_OPTIONS =
{strict: true, encoding: "utf-8"}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, opts = {}) ⇒ Source

Returns a new instance of Source.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gtfs/source.rb', line 20

def initialize(source, opts={})
  raise 'Source cannot be nil' if source.nil?

  @tmp_dir = Dir.mktmpdir
  ObjectSpace.define_finalizer(self, self.class.finalize(@tmp_dir))

  @source = source
  load_archive(@source)

  @options = DEFAULT_OPTIONS.merge(opts)
end

Instance Attribute Details

#archiveObject

Returns the value of attribute archive.



18
19
20
# File 'lib/gtfs/source.rb', line 18

def archive
  @archive
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/gtfs/source.rb', line 18

def options
  @options
end

#sourceObject

Returns the value of attribute source.



18
19
20
# File 'lib/gtfs/source.rb', line 18

def source
  @source
end

Class Method Details

.build(data_root, opts = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/gtfs/source.rb', line 48

def self.build(data_root, opts={})
  if File.exist?(data_root)
    src = LocalSource.new(data_root, opts)
  else
    src = URLSource.new(data_root, opts)
  end
end

.finalize(directory) ⇒ Object



32
33
34
# File 'lib/gtfs/source.rb', line 32

def self.finalize(directory)
  proc {FileUtils.rm_rf(directory)}
end

Instance Method Details

#entriesObject



56
57
58
# File 'lib/gtfs/source.rb', line 56

def entries
  Dir.entries(@tmp_dir)
end

#extract_to_cache(source_path) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/gtfs/source.rb', line 36

def extract_to_cache(source_path)
  Zip::File.open(source_path) do |zip|
    zip.entries.each do |entry|
      zip.extract(entry.name, File.join(@tmp_dir, '/', entry.name))
    end
  end
end

#filesObject



77
78
79
# File 'lib/gtfs/source.rb', line 77

def files
  @files ||= {}
end

#load_archive(source) ⇒ Object



44
45
46
# File 'lib/gtfs/source.rb', line 44

def load_archive(source)
  raise 'Cannot directly instantiate base GTFS::Source'
end

#parse_file(filename, options) ⇒ Object



81
82
83
84
85
86
# File 'lib/gtfs/source.rb', line 81

def parse_file(filename, options)
  raise_if_missing_source filename
  open File.join(@tmp_dir, '/', filename), "r:#{options[:encoding]}" do |f|
    files[filename] ||= yield f
  end
end

#raise_if_missing_source(filename) ⇒ Object



60
61
62
63
# File 'lib/gtfs/source.rb', line 60

def raise_if_missing_source(filename)
  file_missing = !entries.include?(filename)
  raise InvalidSourceException.new("Missing required source file: #{filename}") if file_missing
end